Check if daylight savings is in effect?

前端 未结 8 1808
悲哀的现实
悲哀的现实 2020-12-29 02:14

How to check if in Denmark daylight time savings has taken effect, if so, then add 1 hour to my data, else not? I have a xml file:



        
8条回答
  •  梦谈多话
    2020-12-29 03:05

    Based on other codes provided above, I made a complete code to run and make tests. The variable cityTz is receiving an IANA timezone name example. IANA timezone pattern is used in Mac and Linux (Windows uses different timezone style). In 2020, daylight-saving (DST) in New York ends on November 01. If you test the code below, the return will be FALSE, because "theDate" is November 02, the next day after the end of DST. But if you change the line commented and set theDate to November 01 (last DST date in NY), the return will be TRUE.

    You can compile this program in Mac or Linux terminal typing:

    csc testDST.cs
    

    To run your program:

    mono testDST.exe
    

    Complete code:

    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);
        }
    }
    

提交回复
热议问题