Search code examples
c#timezonedst

Grey zone of DST


does anybody knows how to handle grey zones of DST (when time doesn't exist): e.g.:

DateTime dt = new DateTime(2014,3,30,2,30,0);
TimeZoneInfo tziSV = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
DateTime dtSV =TimeZoneInfo.ConvertTimeToUtc(dt,tziSV);

gives an error,when

DateTime dt = new DateTime(2014,3,30,2,30,0);
dt = dt.ToUniversalTime();
dt = TimeZoneInfo.ConvertTimeFromUtc(dt,tziSV);

gives 1:30 and then 3:30.

Thanks


Solution

  • I found a way of handling this grey zones by .net facilities

        DateTime dt = new DateTime(2014,3,30,2,30,0);
        TimeZoneInfo tziSV = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
    
        if (tziSV.IsInvalidTime(dt))
        {
           dt = dt.Add(tziSV.GetUtcOffset(dt));
        }
        DateTime dtSV = TimeZoneInfo.ConvertTimeToUtc(dt,tziSV);