算法来确定的日期夏令时?夏令时、算法、日期

2023-09-08 11:29:39 作者:归隐

本来我期待在ActionScript的解决方案。这个问题的关键是算法,其检测准确时刻,当时钟有切换夏令时。

Originally I am looking for a solution in Actionscript. The point of this question is the algorithm, which detects the exact Minute, when a clock has to switch the Daylight Saving Time.

因此​​,例如,25日和十月31日我们要检查的,如果实际日期是星期日,这是之前或之后2点...

So for example between the 25th and the 31th of October we have to check, if the actual date is a sunday, it is before or after 2 o'clock...

推荐答案

有没有真正的算法处理夏令时。基本上,每个国家都可以自行决定何时-and如果 - DST开始和结束。我们所能做的是开发商的唯一的事情是使用某种形式的表来关注一下吧。大多数计算机语言中的语言集成这样的表

There is no real algorithm for dealing with Daylight Saving Time. Basically every country can decide for themselves when -and if- DST starts and ends. The only thing we can do as developers is using some sort of table to look it up. Most computer languages integrate such a table in the language.

在Java中,你可以使用的时区类。如果你想知道确切的日期和时间DST开始或结束在某一年,我会建议使用乔达时间。我看不到只使用标准库发现这一点的清洁方式。

In Java you could use the inDaylightTime method of the TimeZone class. If you want to know the exact date and time when DST starts or ends in a certain year, I would recommend to use Joda Time. I can't see a clean way of finding this out using just the standard libraries.

下面的程序是一个例子:(请注意,这可能会产生意外的结果,如果在一定时间区域没有DST的某一年)

The following program is an example: (Note that it could give unexpected results if a certain time zone does not have DST for a certain year)

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class App {
    public static void main(String[] args) {
        DateTimeZone dtz = DateTimeZone.forID("Europe/Amsterdam");

        System.out.println(startDST(dtz, 2008));
        System.out.println(endDST(dtz, 2008));
    }

    public static DateTime startDST(DateTimeZone zone, int year) {
        return new DateTime(zone.nextTransition(new DateTime(year, 1, 1, 0, 0, 0, 0, zone).getMillis()));
    }

    public static DateTime endDST(DateTimeZone zone, int year) {
        return new DateTime(zone.previousTransition(new DateTime(year + 1, 1, 1, 0, 0, 0, 0, zone).getMillis()));
    }
}