仅从日期计算夏令时 [英] Calculating daylight saving time from only date

查看:43
本文介绍了仅从日期计算夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Arduino 和实时时钟芯片.芯片补偿闰年等,所以它总是有正确的日期,但它不处理夏令时,我认为是由于区域复杂性.时钟可以给我日期、月份和年份(以 1 为基础)和星期几(星期日 = 0 到星期六 = 6).

I am working with an Arduino and a real time clock chip. The chip compensates for leap years and such, so it will always have the correct date, but it does not handle daylight saving time, I assume due to regional complications. The clock can give me the day, month, and year (1 based) and the day of the week (sunday = 0 to saturday = 6).

因为我需要与用户输入的日期和时间进行比较,所以我需要知道为夏令时调整的日期和时间.如果当前日期处于夏令时,我可以简单地从时钟的时间上增加一个小时,我就有了我需要的东西.

Because I need to compare with user entered dates and times, I need to know the date and time adjusted for daylight saving time. If the current date is in daylight saving time I can simply add an hour to the time from the clock and I have what I need.

难点在于确定我是否处于夏令时,因为它每年都在变化.我只关心它在我的位置(山地时间)是否有效.我的平台似乎没有任何全面的日期库,我觉得无论如何这都太过分了.是否有一个简单的公式可以确定我是否处于夏令时?

The hard part is determining whether I am in daylight saving time or not, because it changes from year to year. I only care that it works in my location (Mountain Time). There doesn't appear to be any comprehensive date libraries for my platform, and I feel like that would be overkill anyway. Is there a simple formula to determine if I am in DST or not?

推荐答案

这实际上看似简单.有一些事实可以帮助我们:

This is actually deceptively simple. There are a few facts that will help us:

  1. 在美国大部分地区,夏令时从 3 月的第二个星期日开始,并在 11 月的第一个星期日的凌晨 2 点结束.
  2. 3 月的第二个星期日总是在 8 号和 14 号之间.
  3. 11 月的第一个星期日始终介于 1 号和 7 号之间.
  4. 星期几编号非常方便,因为星期几将为您提供上一个星期日.

这些事实导致了以下代码(C#,但可以轻松移植到您的平台):

These facts lead to the following code (C#, but trivially portable to your platform):

    public bool IsDST(int day, int month, int dow)
    {
        //January, february, and december are out.
        if (month < 3 || month > 11) { return false; }
        //April to October are in
        if (month > 3 && month < 11) { return true; }
        int previousSunday = day - dow;
        //In march, we are DST if our previous sunday was on or after the 8th.
        if (month == 3) { return previousSunday >= 8; }
        //In november we must be before the first sunday to be dst.
        //That means the previous sunday must be before the 1st.
        return previousSunday <= 0;
    }

事实证明,您甚至不需要知道年份即可执行此操作,只要您可以信任您的星期几值即可.

It turns out you don't even need to know the year to do this, as long as you can trust your day of the week value.

我编写了一个快速单元测试并验证此代码与 TimeZone.IsDayLightSavingsTime() 一致,适用于从 1800 到 2200 的所有日期.我没有考虑凌晨 2 点规则,但您可以轻松地检查星期几是星期日,日期是 8 到 14(三月)还是 1 到 7(十一月).

I wrote a quick unit test and verified that this code agrees with TimeZone.IsDayLightSavingsTime() for all dates from 1800 to 2200. I did not account for the 2 am rule, but you could easily do that check if the day of week is Sunday and the date is between 8 and 14 (in March) or 1 and 7 (in November).

这篇关于仅从日期计算夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆