TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule 方法

定义

重载

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

为特定时区创建新调整规则。

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

为特定时区创建新调整规则。

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

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

为特定时区创建新调整规则。

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

调整规则的生效日期。 如果 dateStart 参数的值为 DateTime.MinValue.Date,那么这是时区的第一个有效的调整规则。

dateEnd
DateTime

执行调整规则的最后日期。 如果 dateEnd 参数的值为 DateTime.MaxValue.Date,则调整规则无结束日期。

daylightDelta
TimeSpan

调整所产生的时间更改。 将该值添加到时区的 BaseUtcOffset 属性,以便获取从协调世界时 (UTC) 开始的正确的夏令时偏移量。 该值的范围介于 -14 到 14。

daylightTransitionStart
TimeZoneInfo.TransitionTime

定义夏令时的开始时间的对象。

daylightTransitionEnd
TimeZoneInfo.TransitionTime

定义夏令时的结束时间的对象。

返回

表示新调整规则的对象。

例外

参数 dateStartdateEnd 的属性 Kind 的值不等于 Unspecified

- 或 -

daylightTransitionStart 参数等于 daylightTransitionEnd 参数。

- 或 -

dateStartdateEnd 参数包含一个日期的时间值。

dateEnd 早于 dateStart

- 或 -

daylightDelta 小于 -14 或大于 14。

- 或 -

daylightDelta 参数的 Milliseconds 属性不等于 0。

- 或 -

daylightDelta 参数的 Ticks 属性不等于整秒数。

示例

以下示例创建备用的中央标准时区,并为 1976-1986 年、1987-2006 年和 2007 年及以后的时间段定义三个调整规则。 这些规则将添加到一个泛型 List<T> 对象,该对象的元素随后将复制到 TimeZoneInfo.AdjustmentRule 数组中。 然后,在调用 TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[]) 方法时使用此数组。

// 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())

注解

可以使用此方法为自定义时区创建一个或多个调整规则。 然后,可以通过调用此方法返回的 对象的数组 TimeZoneInfo.AdjustmentRule 作为 adjustmentRules 参数传递给方法的 CreateCustomTimeZone 两个重载。 该示例演示了此过程。

备注

方法 CreateAdjustmentRule 只能用于定义新时区的调整规则;不能用于修改现有时区的调整规则。

dateStartdateEnd 参数必须是没有时间组件的日期值,否则ArgumentException会引发 。 可以通过从日期和时间的 属性中检索值 DateTime 来删除时间 Date 部分,如以下语句所示:

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

KinddateEnd 参数的 dateStart 属性也必须是 DateTimeKind.Unspecified

参数的值 daylightDelta 范围为 -14 到 14。 参数和调用方法中使用的TimeZoneInfo.CreateCustomTimeZone参数之和daylightDeltabaseUtcOffset也必须介于 -14 到 14 的范围内,否则InvalidTimeZoneException会引发 。

注意

daylightDelta参数定义时区的标准时间与其夏令时之间的差异。 它不打算定义时区的标准时间偏移量与协调世界时 (UTC) 。 类 TimeZoneInfo 假定与 UTC 的此偏移量在时区的整个生命周期内都是恒定的。 若要反映时区相对于 UTC 的偏移量(不是由应用调整规则引起的)的更改,必须使用 CreateCustomTimeZone 方法来创建新的自定义时区。

适用于

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

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

为特定时区创建新调整规则。

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

参数

dateStart
DateTime

调整规则的生效日期。 如果值为 DateTime.MinValue.Date,则这是对时区生效的第一个调整规则。

dateEnd
DateTime

执行调整规则的最后日期。 如果值为 DateTime.MaxValue.Date,则调整规则没有结束日期。

daylightDelta
TimeSpan

调整所产生的时间更改。 此值将添加到时区的 BaseUtcOffsetBaseUtcOffsetDelta 属性中,以从协调世界时 (UTC) 获取正确的夏令时偏移量。 该值的范围介于 -14 到 14。

daylightTransitionStart
TimeZoneInfo.TransitionTime

夏令时开始。

daylightTransitionEnd
TimeZoneInfo.TransitionTime

夏令时结束。

baseUtcOffsetDelta
TimeSpan

调整规则期间时区的基本 UTC 偏移量的时间差。

返回

新的调整规则。

适用于