【问题标题】:Check if daylight savings is in effect?检查夏令时是否有效?
【发布时间】:2012-05-26 19:09:26
【问题描述】:

如何检查丹麦夏令时是否已生效,如果是,则在我的数据中添加 1 小时,否则不是? 我有一个 xml 文件:

<day = "1"
month = "5"
sunrise ="06:30"
sunset ="21:30"
/>

【问题讨论】:

    标签: c# windows-phone-7 datetime dst


    【解决方案1】:

    认为您需要将此 xml 转换为 DateTime,然后使用 TimeZoneInfo 类。

    如果丹麦是您的当地时间:

    DateTime thisTime = DateTime.Now;
    bool isDaylight = TimeZoneInfo.Local.IsDaylightSavingTime(thisTime);
    

    否则您需要获得丹麦时区:

    DateTime thisTime = DateTime.Now;
    // get Denmark Standard Time zone - not sure about that
    TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Denmark Standard Time");
    bool isDaylight = tst.IsDaylightSavingTime(thisTime);
    

    【讨论】:

    【解决方案2】:

    当我按上述方式编码时 - 对于纽约,我在调试器中发现时间设置正确(包括 DST)

    TimeZoneInfo nyTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    
    DateTime nyTime = GetLocalDateTime(DateTime.UtcNow, nyTimeZone);
    
    if (nyTimeZone.IsDaylightSavingTime(nyTime))
        nyTime = nyTime.AddHours(1);
    
    public static DateTime GetLocalDateTime(DateTime utcDateTime, TimeZoneInfo timeZone)
        {
    
            utcDateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Utc);
    
            DateTime time = TimeZoneInfo.ConvertTime(utcDateTime, timeZone);
    
            return time;
    
        }
    

    【讨论】:

    • GetLocalDateTime 是什么?
    • 对不起,我忘记复制方法了。以上编辑
    【解决方案3】:

    您可以使用TimeZoneInfo.IsDaylightSavingTime

    DateTime theDate = new DateTime(2012, 5, 1); // may 1st
    TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
    bool isCurrentlyDaylightSavings = tzi.IsDaylightSavingTime(theDate);
    

    【讨论】:

      【解决方案4】:

      这是一个通用测试,如果我的数学不正确,很高兴得到纠正。就我而言,我只需要获取时区的 GMT 偏移量,无论它在世界的哪个地方。

        int timezone;
      
        TimeZoneInfo localZone = TimeZoneInfo.Local;
      
        DateTime myTime = DateTime.Now;
      
        bool isDayLight = TimeZoneInfo.Local.IsDaylightSavingTime(myTime);
      
        if (isDayLight)
                  timezone = Math.Abs(localZone.BaseUtcOffset.Hours) + 1;
        else
                  timezone = Math.Abs(localZone.BaseUtcOffset.Hours);
      
        Debug.WriteLine("timezone is " + timezone);
      

      我只是找到了当前时间,如果是在夏令时,则在 GMT 偏移量上加上 +1。

      这适用于 Visual Studio Express 2013。

      【讨论】:

        【解决方案5】:

        你需要做两件事:

        1. 致电IsAmbiguous
        2. 列表项IsDaylightSavingTime

        if (TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) || TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate)) Console.WriteLine("{0} may be daylight saving time in {1}.", unclearDate, TimeZoneInfo.Local.DisplayName);

        https://msdn.microsoft.com/en-us/library/bb460642(v=vs.110).aspx

        【讨论】:

        • 什么是IsAmbiguous
        • @Kiquenet 如果为真,你不能说,只有时间,如果它是 DST 与否,时间都存在于 DST 和标准时间,例如:说 DST 某处结束于 2018 -12-23 00:00,所以,在这个地方,2018-12-22 23:30:00 会有歧义,会有这个时间 DST 和 00:00,时钟延迟 1 小时就会有这个时间再次,现在是标准时间。
        【解决方案6】:

        这是我可以在所有时区使用的简短解决方案:

        DateTime utcTime = DateTime.Parse("30.10.2018 18:21:34")
        DateTime localtime = ConvertUTCToLocalTime(utcTime);
        
        
        public static DateTime ConvertUTCToLocalTime(DateTime UTCTime)
        {
            var localZone = TimeZone.CurrentTimeZone;
            var offset = localZone.GetUtcOffset(UTCTime);
            var localTime = UTCTime.AddHours(offset.Hours);
            return localTime;
        }
        

        【讨论】:

          【解决方案7】:

          重要的

          myDateTime.IsDaylightSavingTime 确实返回了正确的值......但是......它至少精确到一天中的小时,仅仅传递日期是不够的。

          例如今年 (2019) 3/10/2019 02:00:00 传递为 myDateTime 将返回 false,但 3/10/2019 03:00:00 将返回 true。

          【讨论】:

            【解决方案8】:

            基于上面提供的其他代码,我做了一个完整的代码来运行和测试。变量 cityTz 正在接收 IANA 时区名称示例。 IANA 时区模式用于 Mac 和 Linux(Windows 使用不同的时区样式)。 2020 年,纽约的夏令时(DST)于 11 月 1 日结束。如果您测试下面的代码,返回将为 FALSE,因为“theDate”是 DST 结束后的第二天 11 月 2 日。但是,如果您更改注释行并将 theDate 设置为 11 月 01 日(纽约的最后一个 DST 日期),则返回将为 TRUE。

            你可以在 Mac 或 Linux 终端输入编译这个程序:

            csc testDST.cs
            

            运行你的程序:

            mono testDST.exe
            

            完整代码:

            using System;
            using System.Collections.Generic;
            using System.Linq;
            
            class Program{
                static void Main(string[] args)
                {
                    string cityTz;
            
                    //cityTz = "America/Sao_Paulo";
                    cityTz = "America/New_York";
            
                    //DateTime theDate = new DateTime(2020, 11, 1); //returns TRUE
                    DateTime theDate = new DateTime(2020, 11, 2); //returns FALSE
            
                    Console.WriteLine("Data: "+theDate);
            
                    TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(cityTz);
                    bool isDaylight = tzi.IsDaylightSavingTime(theDate);
            
                    Console.WriteLine("isDaylight this date in "+ cityTz +"?: "+ isDaylight);
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2013-12-23
              • 2018-01-12
              • 2015-02-26
              • 2016-07-29
              • 2012-08-06
              • 1970-01-01
              • 2019-12-02
              • 2020-05-08
              相关资源
              最近更新 更多