Gray zone DST

Does anyone know how to handle DST gray areas (when time does not exist): for example :.

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.

thank

+3
source share
2 answers

I found a way to handle gray areas with .net tools

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

Use Noda Time :)

This does not relieve you of the burden of thinking how you want to deal with it, but it allows you to specify how you want to deal with it. Essentially, you have been given a value that does not exist ... so you can choose an error, or perhaps start a transition, or the end of the transition, or some user behavior:

For example, a soft resolver will return the end of the transition:

using System;
using NodaTime;

class Test
{
    static void Main()
    {
        var local = new LocalDateTime(2014, 3, 30, 2, 30, 0);
        var zone = DateTimeZoneProviders.Tzdb["Europe/Paris"];

        var zoned = local.InZoneLeniently(zone);
        Console.WriteLine(zoned);  // 2014-03-30T03:00:00 Europe/Paris (+02)
    }
}

" " :

using System;
using NodaTime;
using NodaTime.TimeZones;

class Test
{
    static void Main()
    {
        var local = new LocalDateTime(2014, 3, 30, 2, 30, 0);
        var zone = DateTimeZoneProviders.Tzdb["Europe/Paris"];

        var resolver = Resolvers.CreateMappingResolver(
              ambiguousTimeResolver: Resolvers.ReturnEarlier,
              skippedTimeResolver: Resolvers.ReturnEndOfIntervalBefore);
        var zoned = local.InZone(zone, resolver);
        Console.WriteLine(zoned); // 2014-03-30T01:59:59 Europe/Paris (+01)
    }
}

( ).

, , . , . (, , /.)

+4

All Articles