48

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:

<day = "1"
month = "5"
sunrise ="06:30"
sunset ="21:30"
/>
Megaoctane
  • 919
  • 3
  • 9
  • 10

8 Answers8

94

Think you need convert this xml to DateTime and then use TimeZoneInfo class.

If Denmark your local time:

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

Else you need to get Denmark TimeZone:

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);
Eugene
  • 1,699
  • 1
  • 12
  • 13
  • Why this does't work well with "E. South America Standard Time" for specific `new DateTime(1995, 10, 15)` date? You cant test it here https://dotnetfiddle.net/ilwIZu and in documentation history at https://pt.m.wikipedia.org/wiki/Lista_de_períodos_em_que_vigorou_o_horário_de_verão_no_Brasil 1995-10-15 really is a Day Light – Alexsandro Dec 21 '17 at 16:52
  • If I get TimeZoneInfo for local, it's called "Romance Standard Time". That's the id which works for CET/CEST with daylight saving time. "Romance" is - as far as I can tell - vulgar Latin (Italian?) as opposed to proper Latin. Living in Denmark, this does not make much sense to me, but I suppose they needed to call it something :) – iakob Oct 31 '22 at 08:02
10

When I coded as above - for New-York, I found in the debugger that the time was set correctly (including 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;

    }
Meir Schreiber
  • 113
  • 1
  • 8
7

You can use TimeZoneInfo.IsDaylightSavingTime

DateTime theDate = new DateTime(2012, 5, 1); // may 1st
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
bool isCurrentlyDaylightSavings = tzi.IsDaylightSavingTime(theDate);
Matthew
  • 24,703
  • 9
  • 76
  • 110
0

Here is a generic test and happy to be corrected if my math is incorrect. In my case I just needed to get the GMT offset for the timezone regardless of where it was in the world.

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

I simply found the current time and if it was in Day Light Savings period added +1 to the GMT offset.

This works with Visual Studio Express 2013.

timv
  • 3,346
  • 4
  • 34
  • 43
0

You need to do two things:

  1. call IsAmbiguous
  2. List item 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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
B.W
  • 57
  • 1
  • 5
  • What is `IsAmbiguous`? – Kiquenet Mar 01 '18 at 13:06
  • @Kiquenet If true, you can't say, only with the time, if it was DST or not, the time existed both in DST and standard time, Ex.: Say DST somewhere ends on 2018-12-23 00:00, so, on this place, 2018-12-22 23:30:00 will be ambiguous, there will be this time DST and at 00:00, clock delayed 1 hour and there will be this time again, now standard time. – fbiazi Dec 22 '18 at 15:25
0

this is my short solution which can be using in all timezones:

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;
}
Marc
  • 21
  • 3
0

Important

myDateTime.IsDaylightSavingTime DOES return the proper value... but... it is accurate down to at least the hour of the day, just passing the date alone isn't enough.

For instance this year (2019) 3/10/2019 02:00:00 passed as myDateTime will return false, but 3/10/2019 03:00:00 will return true.

0

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