TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime, DateTime, TimeSpan, TimeZoneInfo+TransitionTime, TimeZoneInfo+TransitionTime) 方法

定義

為特定時區建立新的調整規則。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

參數

dateStart
DateTime

調整規則的生效日期。The effective date of the adjustment rule. 如果 dateStart 參數的值是 DateTime.MinValue.Date,則這是時區第一個生效的調整規則。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. 如果 dateEnd 參數的值是 DateTime.MaxValue.Date,則調整規則就沒有結束日期。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. 這個值會加入至時區的 BaseUtcOffset 屬性,用來取得正確的 Coordinated Universal Time (UTC) 日光節約時間位移。This value is added to the time zone's BaseUtcOffset property to obtain the correct daylight offset from Coordinated Universal Time (UTC). 這個值的範圍可從 -14 到 14。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.

傳回

物件,表示新的調整規則。An object that represents the new adjustment rule.

例外狀況

dateStartdateEnd 參數的 Kind 屬性不等於 UnspecifiedThe Kind property of the dateStart or dateEnd parameter does not equal Unspecified.

-或--or- daylightTransitionStart 參數等於 daylightTransitionEnd 參數。The daylightTransitionStart parameter is equal to the daylightTransitionEnd parameter.

-或--or- dateStartdateEnd 參數包括當天時間的值。The dateStart or dateEnd parameter includes a time of day value.

dateEnd 早於 dateStartdateEnd is earlier than dateStart.

-或--or- daylightDelta 小於 -14 或大於 14。daylightDelta is less than -14 or greater than 14.

-或--or- daylightDelta 參數的 Milliseconds 屬性不等於 0。The Milliseconds property of the daylightDelta parameter is not equal to 0.

-或--or- daylightDelta 參數的 Ticks 屬性不等於完整的秒數。The Ticks property of the daylightDelta parameter does not equal a whole number of seconds.

範例

下列範例會建立替代的中央標準時區, 並定義1976-1986、1987-2006 和2007等週期的三個調整規則。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. 這些規則會加入至泛型List<T>物件, 然後將其元素複製TimeZoneInfo.AdjustmentRule到陣列。These rules are added to a generic List<T> object whose elements are then copied to a TimeZoneInfo.AdjustmentRule array. 然後, 此陣列會在TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[])方法的呼叫中使用。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
'
' 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())     

備註

您可以使用這個方法來建立自訂時區的一或多個調整規則。You can use this method to create one or more adjustment rules for a custom time zone. 然後, 呼叫這個TimeZoneInfo.AdjustmentRule方法所傳回的物件陣列, 就可以adjustmentRules做為參數傳遞CreateCustomTimeZone至方法的兩個多載。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.

注意

CreateAdjustmentRule方法只能用來定義新時區的調整規則, 而不能用來修改現有時區的調整規則。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.

和參數dateEnd必須是不含時間元件的日期值, 否則ArgumentException會擲回。 dateStartThe dateStart and dateEnd parameters must be date values without a time component or an ArgumentException is thrown. 您可以從日期和時間的DateTime Date屬性中抓取值來移除時間元件, 如下列語句所示: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  

和參數的屬性也必須是DateTimeKind.UnspecifiedKind dateStart dateEndThe Kind property of the dateStart and dateEnd parameters must also be DateTimeKind.Unspecified.

daylightDelta參數值的範圍可從-14 到14。The value of the daylightDelta parameter can range from -14 to 14. daylightDelta參數的總和和TimeZoneInfo.CreateCustomTimeZone方法的呼叫baseUtcOffset中所使用的參數, 其InvalidTimeZoneException範圍也必須介於-14 到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.

注意

daylightDelta參數會定義時區標準時間和其日光節約時間之間的差異。The daylightDelta parameter defines the difference between a time zone's standard time and its daylight saving time. 它不是用來定義與國際標準時間 (UTC) 的時區標準時間時差。It is not intended to define the time zone's standard time offset from Coordinated Universal Time (UTC). TimeZoneInfo類別假設此 UTC 時差在時區的整個生命週期中都是常數。The TimeZoneInfo class assumes that this offset from UTC is constant throughout the life of the time zone. 若要反映時區時差的變更, 而不是由應用程式調整規則所造成, 您必須使用CreateCustomTimeZone方法來建立新的自訂時區。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.

適用於