Export (0) Print
Expand All

TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule Method

Creates a new adjustment rule for a particular time zone.

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

public static TimeZoneInfo..::..AdjustmentRule CreateAdjustmentRule(
	DateTime dateStart,
	DateTime dateEnd,
	TimeSpan daylightDelta,
	TimeZoneInfo..::..TransitionTime daylightTransitionStart,
	TimeZoneInfo..::..TransitionTime daylightTransitionEnd
)

Parameters

dateStart
Type: System.DateTime
The effective date of the adjustment rule. If the value of the dateStart parameter is DateTime.MinValue.Date, this is the first adjustment rule in effect for a time zone.
dateEnd
Type: System.DateTime
The last date that the adjustment rule is in force. If the value of the dateEnd parameter is DateTime.MaxValue.Date, the adjustment rule has no end date.
daylightDelta
Type: System.TimeSpan
The time change that results from the adjustment. This value is added to the time zone's TimeZoneInfo.BaseUtcOffset property to obtain the correct daylight offset from Coordinated Universal Time (UTC). This value can range from -14 to 14.
daylightTransitionStart
Type: System.TimeZoneInfo.TransitionTime
An object that defines the start of daylight saving time.
daylightTransitionEnd
Type: System.TimeZoneInfo.TransitionTime
An object that defines the end of daylight saving time.

Return Value

Type: System.TimeZoneInfo.AdjustmentRule
An object that represents the new adjustment rule.

ExceptionCondition
ArgumentException

The Kind property of the dateStart or dateEnd parameter does not equal DateTimeKind.Unspecified.

-or-

The daylightTransitionStart parameter is equal to the daylightTransitionEnd parameter.

-or-

The dateStart or dateEnd parameter includes a time of day value.

ArgumentOutOfRangeException

dateEnd is earlier than dateStart.

-or-

daylightDelta is less than -14 or greater than 14.

-or-

The Milliseconds property of the daylightDelta parameter is not equal to 0.

-or-

The Ticks property of the daylightDelta parameter does not equal a whole number of seconds.

You can use this method to create one or more adjustment rules for a custom time zone. An array of the TimeZoneInfo.AdjustmentRule objects returned by calls to this method can then be passed as the adjustmentRules parameter to two overloads of the CreateCustomTimeZone method. The example illustrates this procedure.

NoteNote

The CreateAdjustmentRule method can be used only to define an adjustment rule for a new time zone; it cannot be used to modify an adjustment rule for an existing time zone.

The dateStart and dateEnd parameters must be date values without a time component or an ArgumentException is thrown. The time component can be removed by retrieving a DateTime value from the date and time's Date property as shown in the following statements:

DateTime.MinValue.Date
DateTime.MaxValue.Date
DateTime.Now.Date 
dateVariable.Date

The Kind property of the dateStart and dateEnd parameters must also be DateTimeKind.Unspecified.

The value of the daylightDelta parameter can range from -14 to 14. The sum of the daylightDelta parameter and the baseUtcOffset parameter used in the call to the TimeZoneInfo.CreateCustomTimeZone method must also range from -14 to 14 or an InvalidTimeZoneException is thrown.

NoteNote

The daylightDelta parameter defines the difference between a time zone's standard time and its daylight saving time. It is not intended to define the time zone's standard time offset from Coordinated Universal Time (UTC). The TimeZoneInfo class assumes that this offset from UTC is constant throughout the life of the time zone. To reflect a change in a time zone's offset from UTC that is not caused by the application of an adjustment rule, you must use the CreateCustomTimeZone method to create a new custom time zone.

The following example creates an alternate Central Standard Time zone and defines three adjustment rules for the periods 1976-1986, 1987-2006, and 2007 and beyond. These rules are added to a generic List<T> object whose elements are then copied to a TimeZoneInfo.AdjustmentRule array. This array is then used in the call to the TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo.AdjustmentRule[]) method.


// Create alternate Central Standard Time to include historical time zone information
//
// Declare necessary TimeZoneInfo.AdjustmentRule objects for time zone
TimeSpan delta = new TimeSpan(1, 0, 0);
TimeZoneInfo.AdjustmentRule adjustment;
List<TimeZoneInfo.AdjustmentRule> adjustmentList = new List<TimeZoneInfo.AdjustmentRule>();
// Declare transition time variables to hold transition time information
TimeZoneInfo.TransitionTime transitionRuleStart, transitionRuleEnd;

// Define end rule (for 1976-2006)
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 10, 5, DayOfWeek.Sunday);
// Define rule (1976-1986)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 04, 05, DayOfWeek.Sunday);
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1976, 1, 1), new DateTime(1986, 12, 31), delta, transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment);
// Define rule (1987-2006)  
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 04, 01, DayOfWeek.Sunday);
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1987, 1, 1), new DateTime(2006, 12, 31), delta, transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment);
// Define rule (2007- )  
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 03, 02, DayOfWeek.Sunday);
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 11, 01, DayOfWeek.Sunday);
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(2007, 01, 01), DateTime.MaxValue.Date, delta, transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment);

// Create custom U.S. Central Standard Time zone         
TimeZoneInfo.CreateCustomTimeZone("Central Standard Time", new TimeSpan(-6, 0, 0), 
                "(GMT-06:00) Central Time (US Only)", "Central Standard Time", 
                "Central Daylight Time", adjustmentList.ToArray());       


.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft