Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Datetime 仅从日期计算夏令时_Datetime_Arduino_Dst - Fatal编程技术网

Datetime 仅从日期计算夏令时

Datetime 仅从日期计算夏令时,datetime,arduino,dst,Datetime,Arduino,Dst,我正在使用Arduino和实时时钟芯片。芯片补偿闰年等,所以它总是有正确的日期,但它不处理夏令时,我认为由于地区复杂。时钟可以告诉我日期、月份和年份(以1为基础)以及一周中的哪一天(星期日=0到星期六=6) 因为我需要与用户输入的日期和时间进行比较,所以我需要知道为夏令时调整的日期和时间。如果当前日期是夏令时,我只需在时钟上加一个小时,我就有了所需的时间 困难的部分是确定我是否在夏令时,因为它每年都在变化。我只关心它在我的位置(山区时间)起作用。对于我的平台,似乎没有任何全面的日期库,我觉得这样

我正在使用Arduino和实时时钟芯片。芯片补偿闰年等,所以它总是有正确的日期,但它不处理夏令时,我认为由于地区复杂。时钟可以告诉我日期、月份和年份(以1为基础)以及一周中的哪一天(星期日=0到星期六=6)

因为我需要与用户输入的日期和时间进行比较,所以我需要知道为夏令时调整的日期和时间。如果当前日期是夏令时,我只需在时钟上加一个小时,我就有了所需的时间


困难的部分是确定我是否在夏令时,因为它每年都在变化。我只关心它在我的位置(山区时间)起作用。对于我的平台,似乎没有任何全面的日期库,我觉得这样做太过分了。是否有一个简单的公式来确定我是否在DST中?

这实际上很简单。有几个事实可以帮助我们:

  • 在美国大部分地区,DST从3月的第二个星期日开始,到11月的第一个星期日结束,两次都是凌晨2点
  • 三月的第二个星期日总是在8号和14号之间,包括8号和14号
  • 11月的第一个星期日总是在1号和7号之间,包括1号和7号
  • 星期几的编号非常方便,因为星期几将为您提供上一个星期日
  • 这些事实导致了以下代码(C#,但可移植到您的平台):

    public bool IsDST(整数日、整数月、整数道指)
    {
    //一月、二月和十二月已经过去了。
    如果(月<3 | |月>11){返回false;}
    //4月到10月是一段时间
    如果(月>3&&month<11){return true;}
    int previousSunday=日-道指;
    //在三月份,如果我们的上一个周日是在8号或之后,我们就是DST。
    如果(月==3){返回上一个星期日>=8;}
    //在11月,我们必须赶在第一个周日之前进行dst。
    //这意味着前一个星期日必须在第一个星期日之前。
    
    return previousSunday虽然根据当前规则很容易计算特定日期是否在特定地点的DST中,但请注意DST是政客们一时兴起的,随时可能发生变化。我有一个2007年之前制造的时钟,可以自动调整夏令时,现在我必须将其更改为ur一年两次:实际变化发生时两次,现在在旧日期错误地变化时两次

    在这种情况下,您可以通过简单的权宜之计完全忽略DST,即让用户输入时区以及日期和时间。或者您也可以像大多数消费类设备一样,让用户每年两次将时间调整到本地时区

    但是,如果你真的需要处理DST,并且真的想把事情做好,请使用,并确保它可以以某种方式更新。如果你因为某种原因不能做到这一点,至少允许用户覆盖规则。如果这太难,至少给用户关闭自动调整的选项(不像我愚蠢的闹钟).

    中欧代码(2014-3000年期间每天测试)

    公共静态bool IsDst(整数日、整数月、整数道指)
    {
    如果(月<3 | |月>10)返回false;
    如果(月份>3和月份<10)返回true;
    int previousSunday=日-道指;
    如果(月==3)返回上一个星期日>=25;
    如果(月==10)返回上一个星期日<25;
    return false;//这行永远不会发生
    }
    
    测试功能

        static void Main(string[] args)
        {
            TimeZoneInfo tzf2 = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
    
            var date = new DateTime(2014, 01, 1, 5, 0,0);
            bool wasSummer = false;
    
            while (date <= new DateTime(3000,1,1))
            {                                         
                var dow = (int) date.DayOfWeek;
    
                var isDst = IsDst(date.Day, date.Month, dow);               
    
                DateTime f2 = TimeZoneInfo.ConvertTime(date, tzf2);
                var isSummer = f2.IsDaylightSavingTime();
    
                if (isSummer != isDst)
                {
                    Console.WriteLine("ERROR");
                    Console.WriteLine(date);
                }
    
                if (isSummer != wasSummer)
                {
                    Console.WriteLine(date.AddDays(-1).ToShortDateString());
                }
    
                date = date.AddDays(1);
                wasSummer = isSummer;
            }
    
            Console.ReadKey();
    
    static void Main(字符串[]args)
    {
    TimeZoneInfo tzf2=TimeZoneInfo.FindSystemTimeZoneById(“中欧标准时间”);
    var日期=新的日期时间(2014,01,1,5,0,0);
    bool wassammer=false;
    
    虽然(日期在美国,3月14日和11月7日始终是日间储蓄发生的一周的一部分。它们可能是星期日或星期六或一周中的一天,但它们始终是该周的一部分。下面的代码将发现这两个日期的星期日是该日期发生年份的一部分n、 然后在此日期上再加上2小时,以获得夏令时实际发生的时间。然后,您将讨论中的日期与夏令时开始和结束日期进行比较,并通过gmt偏移量调整时间。此过程可用于其他国家/地区的开始和结束日期。您可以设置一个包含每个国家/地区代码和p的表其中的ostal代码包含夏时制结束和开始日期以及这两个期间的gmt偏移量。对于一个月的第一个到第四个星期日,日期为第7、14、21和28日。您可以输入该月最后一个星期日(9月30日或10月31日)的最大天数

    三月的第二个星期日:

    cdate("3/14/" & format(now(),"yyyy"))-format(cdate("3/14/" & format(now(),"yyyy")),"W")+1+2/24
    
    11月1日星期日:

    cdate("11/7/" & format(now(),"yyyy"))-format(cdate("11/7/" & format(now(),"yyyy")),"W")+1+2/24
    

    If(now()
    t_SQL示例

    当[date2check]cdate("3/14/" & format(now(),"yyyy"))-format(cdate("3/14/" & format(now(),"yyyy")),"W")+1+2/24
    cdate("11/7/" & format(now(),"yyyy"))-format(cdate("11/7/" & format(now(),"yyyy")),"W")+1+2/24
    
    If(now() < cdate("3/14/" & format(now(),"yyyy"))-format(cdate("3/14/" & format(now(),"yyyy")),"W")+1+2/24, dateadd("H",-5,now()), if(now() < cdate("11/7/" & format(now(),"yyyy"))-format(cdate("11/7/" & format(now(),"yyyy")),"W")+1+2/24, dateadd("H",-6,now()), dateadd("H",-5,now())))
    
      int timezone = 0;  // Set to correct initial value depending on where you are (or via GPS if you like).
      // Calculate day of week for Daylight savings time.
      int day_of_week = (day_of_month + int(2.6 * (((month + 12 - 3) % 12) + 1) - 0.2) - 40 + 
                  (month < 3 ? year-1 : year) + int((month < 3 ? year-1 : year)/4) + 5) % 7;
      // Adjust timezone based on Daylight savings time for northern hemisphere, USA
      if ((month  >  3 && month < 11 ) || 
          (month ==  3 && day_of_month >= 8 && day_of_week == 0 && hour >= 2) ||  // DST starts 2nd Sunday of March;  2am
          (month == 11 && day_of_month <  8 && day_of_week >  0) ||
          (month == 11 && day_of_month <  8 && day_of_week == 0 && hour < 2)) {   // DST ends 1st Sunday of November; 2am
        timezone++;
      }
    
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <sys/timeb.h>
    
    
    int isDst(int month, int dayOfMonth, int hour, int dayOfWeek);
    
    int main(int argc, char *argv[])
    {
       int isdst, dayOfWeek;
       char buf[80];
       struct tm tmData;
    
       if( argc == 1 )
       {
          printf("\nsyntax: %s mm/dd/yyyy_hh:mm:00", argv[0]);
          return -1;
       }
    
       // 0123456789A12
       // 03/12/2018_12
       strcpy(buf, argv[1]);
       tmData.tm_mon   = atoi(&buf[0]) - 1;  //month -1
       tmData.tm_mday  = atoi(&buf[3]); //day of month
       tmData.tm_year  = atoi(&buf[6]) - 1900; // year - 1900
       tmData.tm_hour  = atoi(&buf[11]); // hour
       tmData.tm_min   = 0; //minutes (not used)
       tmData.tm_sec   = 0; //seconds (not used)
       //tmData.tm_min   = atoi(&buf[14]);
       //tmData.tm_sec   = atoi(&buf[27]);
    
       //day light saving time variable.
       //NOT used in this calculation.
       //Tells mktime the input date is in day light saving time
       tmData.tm_isdst = 0; //
    
       mktime(&tmData);
       dayOfWeek = tmData.tm_wday;
    
       printf("%02d/%02d/%2d_%02d dayWk=%d ",
          tmData.tm_mon+1, tmData.tm_mday, tmData.tm_year, tmData.tm_hour, dayOfWeek);
       isdst = isDst(tmData.tm_mon+1, tmData.tm_mday, tmData.tm_hour, dayOfWeek);
       printf("isdst=%d\n", isdst);
    
       return 0;
    }
    
    
    int isDst(int month, int dayOfMonth, int hour, int dayOfWeek)
    {
       int second_sunday, first_sunday;
    
       if( month  > 3 && month < 11  ) return 1; //4,5,6,7,8,9,10
       if( month  < 3 || month == 12 ) return 0; //1, 2 or 12
       if( month == 3 )
       {
          //The 2nd Sunday in March is 8,9,10,11,12,13,14
          if( dayOfMonth < 8  ) return 0;
          if( dayOfMonth > 14 ) return 1;
    
          //To get here dayOfMonth >= 8 && dayOfMonth <= 14
          second_sunday = dayOfMonth - dayOfWeek;
          if( second_sunday < 8 ) second_sunday += 7;
    printf("2nd_Sunday=%2d ", second_sunday);
          if( dayOfMonth > second_sunday ) return 1;
          if( dayOfMonth < second_sunday ) return 0;
    
          //To get here dayOfMonth = second_sunday
          if( hour >= 2 ) return 1;
          else return 0;
       }
    
       if( month == 11 )
       {
          //The 1st Sunday in Nov is 1,2,3,4,5,6,7
          if( dayOfMonth > 7 ) return 0;
    
          //To get here dayOfMonth >= 1 && dayOfMonth <= 7
          first_sunday = dayOfMonth - dayOfWeek;
          if( first_sunday < 1 ) first_sunday += 7;
    printf("1st_Sunday=%2d ", first_sunday);
          if( dayOfMonth > first_sunday ) return 0;
          if( dayOfMonth < first_sunday ) return 1;
    
          //To get here dayOfMonth = first_sunday
          if( hour >= 2 ) return 0;
          else return 1;
       }
       return -1;
    }
    /**************
       Compile via   cl.exe  isDst.c
    
       Begin and End dates for day light saving time
       03/11/2007_01:00:00    11/04/2007_01:00:00
       03/09/2008_01:00:00    11/02/2008_01:00:00
       03/08/2009_01:00:00    11/01/2009_01:00:00
       03/14/2010_01:00:00    11/07/2010_01:00:00
       03/13/2011_01:00:00    11/06/2011_01:00:00
       03/11/2012_01:00:00    11/04/2012_01:00:00
       03/10/2013_01:00:00    11/03/2013_01:00:00
       03/09/2014_01:00:00    11/02/2014_01:00:00
       03/08/2015_01:00:00    11/01/2015_01:00:00
       03/13/2016_01:00:00    11/06/2016_01:00:00
       03/12/2017_01:00:00    11/05/2017_01:00:00
       03/11/2018_01:00:00    11/04/2018_01:00:00
       03/10/2019_01:00:00    11/03/2019_01:00:00
       03/08/2020_01:00:00    11/01/2020_01:00:00
       03/14/2021_01:00:00    11/07/2021_01:00:00
       03/13/2022_01:00:00    11/06/2022_01:00:00
       03/12/2023_01:00:00    11/05/2023_01:00:00
       03/10/2024_01:00:00    11/03/2024_01:00:00
       03/09/2025_01:00:00    11/02/2025_01:00:00
       03/08/2026_01:00:00    11/01/2026_01:00:00
       03/14/2027_01:00:00    11/07/2027_01:00:00
       03/12/2028_01:00:00    11/05/2028_01:00:00
       03/11/2029_01:00:00    11/04/2029_01:00:00
       03/10/2030_01:00:00    11/03/2030_01:00:00
       03/09/2031_01:00:00    11/02/2031_01:00:00
       03/14/2032_01:00:00    11/07/2032_01:00:00
    
       isDst.exe 03/11/2007_02:00:00  >> dst.txt
       isDst.exe 03/09/2008_02:00:00  >> dst.txt
       isDst.exe 03/08/2009_02:00:00  >> dst.txt
       isDst.exe 03/14/2010_02:00:00  >> dst.txt
       isDst.exe 03/13/2011_02:00:00  >> dst.txt
       isDst.exe 03/11/2012_02:00:00  >> dst.txt
       isDst.exe 03/10/2013_02:00:00  >> dst.txt
       isDst.exe 03/09/2014_02:00:00  >> dst.txt
       isDst.exe 03/08/2015_02:00:00  >> dst.txt
       isDst.exe 03/13/2016_02:00:00  >> dst.txt
       isDst.exe 03/12/2017_02:00:00  >> dst.txt
       isDst.exe 03/11/2018_02:00:00  >> dst.txt
       isDst.exe 03/10/2019_02:00:00  >> dst.txt
       isDst.exe 03/08/2020_02:00:00  >> dst.txt
       isDst.exe 03/14/2021_02:00:00  >> dst.txt
       isDst.exe 03/13/2022_02:00:00  >> dst.txt
       isDst.exe 03/12/2023_02:00:00  >> dst.txt
       isDst.exe 03/10/2024_02:00:00  >> dst.txt
       isDst.exe 03/09/2025_02:00:00  >> dst.txt
       isDst.exe 03/08/2026_02:00:00  >> dst.txt
       isDst.exe 03/14/2027_02:00:00  >> dst.txt
       isDst.exe 03/12/2028_02:00:00  >> dst.txt
       isDst.exe 03/11/2029_02:00:00  >> dst.txt
       isDst.exe 03/10/2030_02:00:00  >> dst.txt
       isDst.exe 03/09/2031_02:00:00  >> dst.txt
       isDst.exe 03/14/2032_02:00:00  >> dst.txt
       isDst.exe 11/04/2007_02:00:00  >> dst.txt
       isDst.exe 11/02/2008_02:00:00  >> dst.txt
       isDst.exe 11/01/2009_02:00:00  >> dst.txt
       isDst.exe 11/07/2010_02:00:00  >> dst.txt
       isDst.exe 11/06/2011_02:00:00  >> dst.txt
       isDst.exe 11/04/2012_02:00:00  >> dst.txt
       isDst.exe 11/03/2013_02:00:00  >> dst.txt
       isDst.exe 11/02/2014_02:00:00  >> dst.txt
       isDst.exe 11/01/2015_02:00:00  >> dst.txt
       isDst.exe 11/06/2016_02:00:00  >> dst.txt
       isDst.exe 11/05/2017_02:00:00  >> dst.txt
       isDst.exe 11/04/2018_02:00:00  >> dst.txt
       isDst.exe 11/03/2019_02:00:00  >> dst.txt
       isDst.exe 11/01/2020_02:00:00  >> dst.txt
       isDst.exe 11/07/2021_02:00:00  >> dst.txt
       isDst.exe 11/06/2022_02:00:00  >> dst.txt
       isDst.exe 11/05/2023_02:00:00  >> dst.txt
       isDst.exe 11/03/2024_02:00:00  >> dst.txt
       isDst.exe 11/02/2025_02:00:00  >> dst.txt
       isDst.exe 11/01/2026_02:00:00  >> dst.txt
       isDst.exe 11/07/2027_02:00:00  >> dst.txt
       isDst.exe 11/05/2028_02:00:00  >> dst.txt
       isDst.exe 11/04/2029_02:00:00  >> dst.txt
       isDst.exe 11/03/2030_02:00:00  >> dst.txt
       isDst.exe 11/02/2031_02:00:00  >> dst.txt
       isDst.exe 11/07/2032_02:00:00  >> dst.txt
       https://stackoverflow.com/questions/5590429/calculating-daylight-saving-time-from-only-date
    ***************/
    
    
    
    /*****
    The previous programs used mktime to compute day_of_week.
    It used day_of_week to compute 2nd_sunday in march and
    1st_sunday in Nov.
    If you don't want to use mktime, you can use this program to
    compute 2nd_sunday.  The same technique will compute 1st_sunday.
    
    On 03/14/2007, the day of the week is Wed, or 3.
    Every year after 2007, the day of the week advances 1 day.
    on leap years, the day of the week advances 2 days.
    Must include the no. of leap years sinc 2004.
    ******/
    
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    int secondSunday(year);
    
    int main(int argc, char *argv[])
    {
       int year, second_sunday;
    
       if( argc == 1 )
       {
          printf("\nsyntax: %s year, with year >= 2007.\n", argv[0]);
          return -1;
       }
    
       year = atoi(argv[1]);
       if( year < 2007 )
       {
          printf("\nsyntax: %s year, with year >= 2007.\n", argv[0]);
          return -1;
       }
    
       second_sunday = secondSunday(year);
       printf("second_sunday=%d\n", second_sunday);
    
       return 0;
    }
    
    
    int secondSunday(year)
    {
       //On 03/14/2007, the day of the week is Wed, or 3.
       int no_years, no_leaps, day_of_week, second_sunday;
    
       no_years = year - 2007;
       no_leaps = (year - 2004)/4;
       day_of_week = 3 + (no_years + no_leaps) % 7;
       second_sunday = 14 - day_of_week;
       if( second_sunday < 8 ) second_sunday += 7;
    
       //printf("no_years=%d,no_leaps=%d,day_of_week=%d, second_sunday=%d\n",
       //no_years, no_leaps, day_of_week, second_sunday);
    
       return second_sunday;
    }
    /**************
       Compile via   cl.exe  second_sunday.c
    
       second_sunday.exe 2007
       second_sunday.exe 2008
       second_sunday.exe 2009
       second_sunday.exe 2010
       second_sunday.exe 2011
       second_sunday.exe 2012
       second_sunday.exe 2013
       second_sunday.exe 2014
       second_sunday.exe 2015
       second_sunday.exe 2016
       second_sunday.exe 2017
       second_sunday.exe 2018
       second_sunday.exe 2019
       second_sunday.exe 2020
       second_sunday.exe 2021
       second_sunday.exe 2022
       second_sunday.exe 2023
       second_sunday.exe 2024
       second_sunday.exe 2025
       second_sunday.exe 2026
       second_sunday.exe 2027
       second_sunday.exe 2028
       second_sunday.exe 2029
       second_sunday.exe 2030
       second_sunday.exe 2031
       second_sunday.exe 2032
    ***************/
    
    from calendar import monthrange
    
    def dow_date_finder(which_weekday_in_month=FIRST,day=MONDAY,month=JANUARY,year=2000):
        bom, days = monthrange(year, month)
        firstmatch = (day - bom) % 7 + 1
        return xrange(firstmatch, days+1, 7)[which_weekday_in_month]
    
    def find_dt_of_daylight_savings_time(yr=date.today().year):
        dst = {}
        spring = dow_date_finder(SECOND, SUNDAY, MARCH, yr)
        spring_dt = datetime(int(yr), 3, spring, 3, 0)
        dst['spring'] = spring_dt
        fall = dow_date_finder(FIRST, SUNDAY, NOVEMBER, yr)
        fall_dt = datetime(int(yr), 11, fall, 3, 0)
        dst['fall'] = fall_dt
        return dst