4

I need to retrieve the information of the Daylight Saving Time (start date and end date) from a timezone.

You can do that with the class TimeZone but it's deprecated and it only returns the timezone of the computer/server.

You don't have these information in the class TimeZoneInfo, so how to get them ?

Ex:

var tzi = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
Console.WriteLine(tzi.DaylightName); // I have the name but no info

var tz = TimeZone.CurrentTimeZone;
DaylightTime dst = tz.GetDaylightChanges(DateTime.UtcNow.Year);
Console.WriteLine($"DST: start {dst.Start} - end {dst.End}"); // that's what I need but for a given timezone
Zysce
  • 1,200
  • 1
  • 10
  • 35

1 Answers1

1

As you've observed, TimeZoneInfo doesn't really expose much directly other than conversions of a single instant in time. The rules for any particular time zone are available (via TimeZoneInfo.GetAdjustmentRules, but they're hard to work with accurately.

The Noda Time project does expose this information: you ask a time zone for the sequence of "zone intervals" used between a start point and an end point (via DateTimeZone.ZoneIntervals). There can be transitions other than into and out of DST; a time zone's standard offset can change, for example. The zone intervals returned cover the interval of time you ask for.

Noda Time is mostly designed to work with the IANA time zones, but you can use the Windows time zones too in the full .NET framework version (not in the .NET Core version at the moment).

Let me know if you'd like a code example - if you can't use Noda Time, it doesn't make much sense to work up the example.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194