3

How can I get daylight saving time boundaries for every single time zone using C#?

1

2 Answers 2

5

Take a look at the TimeZone.GetDaylightChanges method.

EDIT:

Looking at the TimeZone class, it seems like it will only support the local time zone, and only the current year. Take a look atTimeZoneInfo.GetAdjustmentRules, which will let you get the adjustment rules for arbitrary time zones. The example on that page even seems pretty similar to your task here.

7
  • 1
    Would this take into account the change to the date that was made a few years go?
    – aserwin
    Oct 17, 2012 at 18:36
  • 1
    "The TimeZone class supports only a single daylight saving time adjustment rule for the local time zone." - so no historical changes. Also, it's a pain to get time zones other than the local one.
    – Jon Skeet
    Oct 17, 2012 at 18:39
  • 1
    @aserwin It ought to, although if you mean what happens if you try to get the date for previous years, I think that that will not work based on the notes on the page. Actually, it looks like TimeZone can only be the time zone of the local system, so I may need to edit my answer to reflect that. TimeZoneInfo.GetAdjustmentRules may be the better way here.
    – Trying
    Oct 17, 2012 at 18:42
  • 1
    @JakeP: No, it's documented not to take account of changes to daylight saving transitions. TimeZoneInfo.GetAdjustmentRules will work, but it's a pain to use.
    – Jon Skeet
    Oct 17, 2012 at 18:47
  • 1
    @JakeP: Yes, exactly - which I doubt is what the OP wants, given the title.
    – Jon Skeet
    Oct 17, 2012 at 18:57
4

Disclaimer: I'm the main author of the Noda Time library referenced below :)

It's relatively tricky using just the vanilla .NET code:

  • TimeZone doesn't use historical data
  • TimeZoneInfo doesn't make it easy to find transitions; you have to interpret the rule data yourself, basically. TimeZoneInfo.GetAdjustmentRules does give you all the information, but from experience I'd advise you to avoid trying to code against it unless you really have to.

Using Noda Time you can do this fairly easily, however. The code below uses the wrappers around the BCL time zones; change Bcl to Tzdb to use the TZDB time zones ("Europe/London" etc).

using System;
using NodaTime;

class Test
{
    static readonly Instant Start = Instant.FromUtc(2002, 10, 17, 0, 0);
    static readonly Instant End = Instant.FromUtc(2012, 10, 17, 0, 0);

    static void Main()
    {
        var provider = DateTimeZoneProviders.Bcl;
        foreach (var id in provider.Ids)
        {
            var zone = provider[id];
            ShowTransitions(zone);
        }        
    }

    static void ShowTransitions(DateTimeZone zone)
    {
        Console.WriteLine("{0}:", zone.Id);
        var zoneInterval = zone.GetZoneInterval(Start);
        while (zoneInterval.End < End)
        {
            Console.WriteLine("{0} - {1}: {2}",
                              zoneInterval.Start,
                              zoneInterval.End,
                              zoneInterval.WallOffset);
            zoneInterval = zone.GetZoneInterval(zoneInterval.End);
        }
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.