Get the next daylight saving time

I would like to write (or use if it already exits) a function in C # that returns the date / time of the next DST transition using the System.TimeZoneInfo object and the specific “time” in this time zone. The returned time must be in the time zone provided. The function I want has this signature:

public DateTime GetNextTransition(DateTime asOfTime, TimeZoneInfo timeZone) { // Implement me! } 

For example, if I pass the TimeZoneInfo object “Eastern Standard Time” and 1/21/2011 @ 17:00 as “asOfTime”, I expect this function to return on 3/13/2011 @ 2: 00.

The structure of System.TimeZoneInfo.TransitionTime seems to contain all the information I need, but ideally a mechanism would be built to convert this rule into a real Date. Anyone have any suggestions?

+7
source share
3 answers

Take a look at the example on this page, I think it will get what you need.

MSDN - TransitionTime

+6
source

Hey. It may be too late, but I will post here the code that I used for this purpose. This may possibly save time for its implementation. I actually did this using the @Jamiegs answer link.

  public static DateTime? GetNextTransition(DateTime asOfTime, TimeZoneInfo timeZone) { TimeZoneInfo.AdjustmentRule[] adjustments = timeZone.GetAdjustmentRules(); if (adjustments.Length == 0) { // if no adjustment then no transition date exists return null; } int year = asOfTime.Year; TimeZoneInfo.AdjustmentRule adjustment = null; foreach (TimeZoneInfo.AdjustmentRule adj in adjustments) { // Determine if this adjustment rule covers year desired if (adj.DateStart.Year <= year && adj.DateEnd.Year >= year) { adjustment = adj; break; } } if (adjustment == null) { // no adjustment found so no transition date exists in the range return null; } DateTime dtAdjustmentStart = GetAdjustmentDate(adjustment.DaylightTransitionStart, year); DateTime dtAdjustmentEnd = GetAdjustmentDate(adjustment.DaylightTransitionEnd, year); if (dtAdjustmentStart >= asOfTime) { // if adjusment start date is greater than asOfTime date then this should be the next transition date return dtAdjustmentStart; } else if (dtAdjustmentEnd >= asOfTime) { // otherwise adjustment end date should be the next transition date return dtAdjustmentEnd; } else { // then it should be the next year DaylightTransitionStart year++; foreach (TimeZoneInfo.AdjustmentRule adj in adjustments) { // Determine if this adjustment rule covers year desired if (adj.DateStart.Year <= year && adj.DateEnd.Year >= year) { adjustment = adj; break; } } dtAdjustmentStart = GetAdjustmentDate(adjustment.DaylightTransitionStart, year); return dtAdjustmentStart; } } public static DateTime GetAdjustmentDate(TimeZoneInfo.TransitionTime transitionTime, int year) { if (transitionTime.IsFixedDateRule) { return new DateTime(year, transitionTime.Month, transitionTime.Day); } else { // For non-fixed date rules, get local calendar Calendar cal = CultureInfo.CurrentCulture.Calendar; // Get first day of week for transition // For example, the 3rd week starts no earlier than the 15th of the month int startOfWeek = transitionTime.Week * 7 - 6; // What day of the week does the month start on? int firstDayOfWeek = (int)cal.GetDayOfWeek(new DateTime(year, transitionTime.Month, 1)); // Determine how much start date has to be adjusted int transitionDay; int changeDayOfWeek = (int)transitionTime.DayOfWeek; if (firstDayOfWeek <= changeDayOfWeek) transitionDay = startOfWeek + (changeDayOfWeek - firstDayOfWeek); else transitionDay = startOfWeek + (7 - firstDayOfWeek + changeDayOfWeek); // Adjust for months with no fifth week if (transitionDay > cal.GetDaysInMonth(year, transitionTime.Month)) transitionDay -= 7; return new DateTime(year, transitionTime.Month, transitionDay, transitionTime.TimeOfDay.Hour, transitionTime.TimeOfDay.Minute, transitionTime.TimeOfDay.Second); } } 

An example of use would look like this:

 // This should give you DateTime object for date 26 March 2017 // because this date is first transition date after 1 January 2017 for Central Europe Standard Time zone DateTime nextTransitionDate = GetNextTransition(new DateTime(2017, 1, 1), TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time")) 

You can find the code that I played with here .

+2
source

System.TimeZoneInfo.TransitionTime looks like a structure that can hold such transition data, rather than a function that determines the actual values. To create such a function, I will find data on the Internet somewhere, and then create the values ​​using the static methods CreateFloatingDateRule or CreateFixedDateRule.

0
source

All Articles