Export (0) Print
Expand All

TimeZoneInfo.TransitionTime.IsFixedDateRule Property

Gets a value indicating whether the time change occurs at a fixed date and time (such as November 1) or a floating date and time (such as the last Sunday of October).

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public bool IsFixedDateRule { get; }

Property Value

Type: System.Boolean
true if the time change rule is fixed-date; false if the time change rule is floating-date.

A fixed-date rule indicates that the transition occurs on the same date and time of each year to which the adjustment rule applies. For example, a time change that occurs every November 3 follows a fixed-date rule. A floating-date rule indicates that the transition occurs on a specific day of a specific week of a specific month for each year to which the adjustment rule applies. For example, a time change that occurs on the first Sunday of November follows a floating-date rule.

The value of the IsFixedDateRule property determines which properties of a TimeZoneInfo.TransitionTime object have valid values. The following table indicates which properties are affected by the value of the IsFixedDateRule property.

TransitionTime property

IsFixedDateRule = true

IsFixedDateRule = false

Day

Valid

Unused

DayOfWeek

Unused

Valid

Week

Unused

Valid

Month

Valid

Valid

TimeOfDay

Valid

Valid

The following example lists the transition times to and from daylight saving time for all time zones that are available on the local system. For time zones with fixed-date rules, it displays transition time information from the properties of the TimeZoneInfo.TransitionTime object. For time zones without fixed-date rules, it uses a Calendar object that represents the current system calendar to determine the actual start and end dates of the transition. The example displays the results to the console.

private void GetTransitionTimes(int year)
{
   // Instantiate DateTimeFormatInfo object for month names
   DateTimeFormatInfo dateFormat = CultureInfo.CurrentCulture.DateTimeFormat;

   // Get and iterate time zones on local computer
   ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();
   foreach (TimeZoneInfo timeZone in timeZones)
   {
      Console.WriteLine("{0}:", timeZone.StandardName);
      TimeZoneInfo.AdjustmentRule[] adjustments = timeZone.GetAdjustmentRules();
      int startYear = year;
      int endYear = startYear;

      if (adjustments.Length == 0)
      {
         Console.WriteLine("   No adjustment rules.");
      }   
      else
      {   
         TimeZoneInfo.AdjustmentRule adjustment = GetAdjustment(adjustments, year);
         if (adjustment == null)
         {
            Console.WriteLine("   No adjustment rules available for this year.");
            continue;
         }
         TimeZoneInfo.TransitionTime startTransition, endTransition;

         // Determine if starting transition is fixed 
         startTransition = adjustment.DaylightTransitionStart;
         // Determine if starting transition is fixed and display transition info for year 
         if (startTransition.IsFixedDateRule)
            Console.WriteLine("   Begins on {0} {1} at {2:t}", 
                              dateFormat.GetMonthName(startTransition.Month), 
                              startTransition.Day, 
                              startTransition.TimeOfDay);
         else
            DisplayTransitionInfo(startTransition, startYear, "Begins on");

         // Determine if ending transition is fixed and display transition info for year
         endTransition = adjustment.DaylightTransitionEnd;

         // Does the transition back occur in an earlier month (i.e.,  
         // the following year) than the transition to DST? If so, make 
         // sure we have the right adjustment rule. 
         if (endTransition.Month < startTransition.Month)
         {
            endTransition = GetAdjustment(adjustments, year + 1).DaylightTransitionEnd;
            endYear++;
         }

         if (endTransition.IsFixedDateRule)
            Console.WriteLine("   Ends on {0} {1} at {2:t}", 
                              dateFormat.GetMonthName(endTransition.Month), 
                              endTransition.Day, 
                              endTransition.TimeOfDay);
         else
            DisplayTransitionInfo(endTransition, endYear, "Ends on");
      }      
   }   
} 


private static TimeZoneInfo.AdjustmentRule GetAdjustment(TimeZoneInfo.AdjustmentRule[] adjustments,
                                                  int year)
{                                                  
   // Iterate adjustment rules for time zone 
   foreach (TimeZoneInfo.AdjustmentRule adjustment in adjustments)
   {
      // Determine if this adjustment rule covers year desired 
      if (adjustment.DateStart.Year <= year && adjustment.DateEnd.Year >= year)
         return adjustment;
   }   
   return null;
}

private void DisplayTransitionInfo(TimeZoneInfo.TransitionTime transition, int year, string label)
{
   // 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 = transition.Week * 7 - 6;
   // What day of the week does the month start on? 
   int firstDayOfWeek = (int) cal.GetDayOfWeek(new DateTime(year, transition.Month, 1)); 
   // Determine how much start date has to be adjusted 
   int transitionDay; 
   int changeDayOfWeek = (int) transition.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, transition.Month))  
      transitionDay -= 7;

   Console.WriteLine("   {0} {1}, {2:d} at {3:t}", 
                     label, 
                     transition.DayOfWeek, 
                     new DateTime(year, transition.Month, transitionDay), 
                     transition.TimeOfDay);   
}   

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft