Editar

Partilhar via


TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule Method

Definition

Overloads

CreateAdjustmentRule(DateTime, DateTime, TimeSpan, TimeZoneInfo+TransitionTime, TimeZoneInfo+TransitionTime)

Creates a new adjustment rule for a particular time zone.

CreateAdjustmentRule(DateTime, DateTime, TimeSpan, TimeZoneInfo+TransitionTime, TimeZoneInfo+TransitionTime, TimeSpan)

Creates a new adjustment rule for a particular time zone.

CreateAdjustmentRule(DateTime, DateTime, TimeSpan, TimeZoneInfo+TransitionTime, TimeZoneInfo+TransitionTime)

Source:
TimeZoneInfo.AdjustmentRule.cs
Source:
TimeZoneInfo.AdjustmentRule.cs
Source:
TimeZoneInfo.AdjustmentRule.cs

Creates a new adjustment rule for a particular time zone.

public:
 static TimeZoneInfo::AdjustmentRule ^ CreateAdjustmentRule(DateTime dateStart, DateTime dateEnd, TimeSpan daylightDelta, TimeZoneInfo::TransitionTime daylightTransitionStart, TimeZoneInfo::TransitionTime daylightTransitionEnd);
public static TimeZoneInfo.AdjustmentRule CreateAdjustmentRule (DateTime dateStart, DateTime dateEnd, TimeSpan daylightDelta, TimeZoneInfo.TransitionTime daylightTransitionStart, TimeZoneInfo.TransitionTime daylightTransitionEnd);
static member CreateAdjustmentRule : DateTime * DateTime * TimeSpan * TimeZoneInfo.TransitionTime * TimeZoneInfo.TransitionTime -> TimeZoneInfo.AdjustmentRule
Public Shared Function CreateAdjustmentRule (dateStart As DateTime, dateEnd As DateTime, daylightDelta As TimeSpan, daylightTransitionStart As TimeZoneInfo.TransitionTime, daylightTransitionEnd As TimeZoneInfo.TransitionTime) As TimeZoneInfo.AdjustmentRule

Parameters

dateStart
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
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
TimeSpan

The time change that results from the adjustment. This value is added to the time zone's BaseUtcOffset property to obtain the correct daylight offset from Coordinated Universal Time (UTC). This value can range from -14 to 14.

daylightTransitionStart
TimeZoneInfo.TransitionTime

An object that defines the start of daylight saving time.

daylightTransitionEnd
TimeZoneInfo.TransitionTime

An object that defines the end of daylight saving time.

Returns

An object that represents the new adjustment rule.

Exceptions

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

-or-

The daylightTransitionStart parameter is equal to the daylightTransitionEnd parameter.

-or-

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

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.

Examples

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());
// Create alternate Central Standard Time to include historical time zone information
let delta = TimeSpan(1, 0, 0)
let adjustmentList = ResizeArray()

// Define end rule (for 1976-2006)
let transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 2, 0, 0), 10, 5, DayOfWeek.Sunday)
// Define rule (1976-1986)
let transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 2, 0, 0), 04, 05, DayOfWeek.Sunday)
TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime(1976, 1, 1), DateTime(1986, 12, 31), delta, transitionRuleStart, transitionRuleEnd)
|> adjustmentList.Add
// Define rule (1987-2006)  
let transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 2, 0, 0), 04, 01, DayOfWeek.Sunday)
TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime(1987, 1, 1), DateTime(2006, 12, 31), delta, transitionRuleStart, transitionRuleEnd)
|> adjustmentList.Add
// Define rule (2007- )  
let transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 2, 0, 0), 03, 02, DayOfWeek.Sunday)
let transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 2, 0, 0), 11, 01, DayOfWeek.Sunday)
TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime(2007, 01, 01), DateTime.MaxValue.Date, delta, transitionRuleStart, transitionRuleEnd)
|> adjustmentList.Add
            
// Create custom U.S. Central Standard Time zone         
TimeZoneInfo.CreateCustomTimeZone("Central Standard Time", TimeSpan(-6, 0, 0), 
                                  "(GMT-06:00) Central Time (US Only)", "Central Standard Time", 
                                  "Central Daylight Time", adjustmentList.ToArray())
' Create alternate Central Standard Time to include historical time zone information
'
' Declare necessary TimeZoneInfo.AdjustmentRule objects for time zone
Dim delta As New TimeSpan(1, 0, 0)
Dim adjustment As TimeZoneInfo.AdjustmentRule
Dim adjustmentList As New List(Of TimeZoneInfo.AdjustmentRule)
' Declare transition time variables to hold transition time information
Dim transitionRuleStart, transitionRuleEnd As TimeZoneInfo.TransitionTime

' Define end rule (for 1976-2006)
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#02:00:00AM#, 10, 5, DayOfWeek.Sunday)
' Define rule (1976-1986)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#2:00:00AM#, 04, 05, DayOfWeek.Sunday)
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#01/01/1976#, #12/31/1986#, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add(adjustment)
' Define rule (1987-2006)  
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#2:00:00AM#, 04, 01, DayOfWeek.Sunday)
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#01/01/1987#, #12/31/2006#, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add(adjustment)
' Define rule (2007- )  
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#2:00:00AM#, 03, 02, DayOfWeek.Sunday)
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#2:00:00AM#, 11, 01, DayOfWeek.Sunday)
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#01/01/2007#, Date.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())

Remarks

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.

Note

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.

Note

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.

Applies to

CreateAdjustmentRule(DateTime, DateTime, TimeSpan, TimeZoneInfo+TransitionTime, TimeZoneInfo+TransitionTime, TimeSpan)

Source:
TimeZoneInfo.AdjustmentRule.cs
Source:
TimeZoneInfo.AdjustmentRule.cs
Source:
TimeZoneInfo.AdjustmentRule.cs

Creates a new adjustment rule for a particular time zone.

public:
 static TimeZoneInfo::AdjustmentRule ^ CreateAdjustmentRule(DateTime dateStart, DateTime dateEnd, TimeSpan daylightDelta, TimeZoneInfo::TransitionTime daylightTransitionStart, TimeZoneInfo::TransitionTime daylightTransitionEnd, TimeSpan baseUtcOffsetDelta);
public static TimeZoneInfo.AdjustmentRule CreateAdjustmentRule (DateTime dateStart, DateTime dateEnd, TimeSpan daylightDelta, TimeZoneInfo.TransitionTime daylightTransitionStart, TimeZoneInfo.TransitionTime daylightTransitionEnd, TimeSpan baseUtcOffsetDelta);
static member CreateAdjustmentRule : DateTime * DateTime * TimeSpan * TimeZoneInfo.TransitionTime * TimeZoneInfo.TransitionTime * TimeSpan -> TimeZoneInfo.AdjustmentRule
Public Shared Function CreateAdjustmentRule (dateStart As DateTime, dateEnd As DateTime, daylightDelta As TimeSpan, daylightTransitionStart As TimeZoneInfo.TransitionTime, daylightTransitionEnd As TimeZoneInfo.TransitionTime, baseUtcOffsetDelta As TimeSpan) As TimeZoneInfo.AdjustmentRule

Parameters

dateStart
DateTime

The effective date of the adjustment rule. If the value is DateTime.MinValue.Date, this is the first adjustment rule in effect for a time zone.

dateEnd
DateTime

The last date that the adjustment rule is in force. If the value is DateTime.MaxValue.Date, the adjustment rule has no end date.

daylightDelta
TimeSpan

The time change that results from the adjustment. This value is added to the time zone's BaseUtcOffset and BaseUtcOffsetDelta properties to obtain the correct daylight offset from Coordinated Universal Time (UTC). This value can range from -14 to 14.

daylightTransitionStart
TimeZoneInfo.TransitionTime

The start of daylight saving time.

daylightTransitionEnd
TimeZoneInfo.TransitionTime

The end of daylight saving time.

baseUtcOffsetDelta
TimeSpan

The time difference with the base UTC offset for the time zone during the adjustment-rule period.

Returns

The new adjustment rule.

Applies to