TimeZoneInfo.CreateCustomTimeZone TimeZoneInfo.CreateCustomTimeZone TimeZoneInfo.CreateCustomTimeZone TimeZoneInfo.CreateCustomTimeZone Method

定义

定义在本地计算机上未找到的时区。Defines a time zone that is not found on the local computer.

重载

CreateCustomTimeZone(String, TimeSpan, String, String) CreateCustomTimeZone(String, TimeSpan, String, String) CreateCustomTimeZone(String, TimeSpan, String, String) CreateCustomTimeZone(String, TimeSpan, String, String)

创建带指定标识符的自定义时区、与协调世界时 (UTC) 的偏移量、显示名称以及标准时间显示名称。Creates a custom time zone with a specified identifier, an offset from Coordinated Universal Time (UTC), a display name, and a standard time display name.

CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[]) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[]) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[]) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[])

创建带指定标识符的自定义时区、与协调世界时 (UTC) 的偏移量、显示名称、标准时间名称、夏时制名称和夏时制规则。Creates a custom time zone with a specified identifier, an offset from Coordinated Universal Time (UTC), a display name, a standard time name, a daylight saving time name, and daylight saving time rules.

CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[], Boolean) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[], Boolean) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[], Boolean) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[], Boolean)

创建带指定标识符的自定义时区、与协调世界时 (UTC) 的偏移量、显示名称、标准时间名称、夏令制名称、夏令制规则以及指示返回的对象是否反映夏令制信息的值。Creates a custom time zone with a specified identifier, an offset from Coordinated Universal Time (UTC), a display name, a standard time name, a daylight saving time name, daylight saving time rules, and a value that indicates whether the returned object reflects daylight saving time information.

CreateCustomTimeZone(String, TimeSpan, String, String) CreateCustomTimeZone(String, TimeSpan, String, String) CreateCustomTimeZone(String, TimeSpan, String, String) CreateCustomTimeZone(String, TimeSpan, String, String)

创建带指定标识符的自定义时区、与协调世界时 (UTC) 的偏移量、显示名称以及标准时间显示名称。Creates a custom time zone with a specified identifier, an offset from Coordinated Universal Time (UTC), a display name, and a standard time display name.

public:
 static TimeZoneInfo ^ CreateCustomTimeZone(System::String ^ id, TimeSpan baseUtcOffset, System::String ^ displayName, System::String ^ standardDisplayName);
public static TimeZoneInfo CreateCustomTimeZone (string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName);
static member CreateCustomTimeZone : string * TimeSpan * string * string -> TimeZoneInfo
Public Shared Function CreateCustomTimeZone (id As String, baseUtcOffset As TimeSpan, displayName As String, standardDisplayName As String) As TimeZoneInfo

参数

id
String String String String

时区的标识符。The time zone's identifier.

baseUtcOffset
TimeSpan TimeSpan TimeSpan TimeSpan

一个对象,它表示此时区和协调世界时 (UTC) 之间的时差。An object that represents the time difference between this time zone and Coordinated Universal Time (UTC).

displayName
String String String String

新时区的显示名称。The display name of the new time zone.

standardDisplayName
String String String String

新时区的标准时间名称。The name of the new time zone's standard time.

返回

新时区。The new time zone.

异常

id 参数为空字符串 ("")。The id parameter is an empty string ("").

- 或 --or- baseUtcOffset 参数不表示整分钟数。The baseUtcOffset parameter does not represent a whole number of minutes.

baseUtcOffset 参数大于 14 小时或小于 -14 小时。The baseUtcOffset parameter is greater than 14 hours or less than -14 hours.

示例

下面的示例为南极洲的莫森和 Holme 托架区域创建自定义时区。The following example creates a custom time zone for the Mawson and Holme Bay regions of Antarctica. 然后, 它显示将本地时间转换为新时区中的时间的结果。It then displays the result of converting the local time to the time in the new time zone.

string displayName = "(GMT+06:00) Antarctica/Mawson Time";
string standardName = "Mawson Time"; 
TimeSpan offset = new TimeSpan(06, 00, 00);
TimeZoneInfo mawson = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName);
Console.WriteLine("The current time is {0} {1}", 
                  TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, mawson),
                  mawson.StandardName);      
Dim displayName As String = "(GMT+06:00) Antarctica/Mawson Time"
Dim standardName As String = "Mawson Time" 
Dim offset As TimeSpan = New TimeSpan(06, 00, 00)
Dim mawson As TimeZoneInfo = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName)
Console.WriteLine("The current time is {0} {1}", _ 
                  TimeZoneInfo.ConvertTime(Date.Now, TimeZoneInfo.Local, mawson), _
                  mawson.StandardName)      

注解

CreateCustomTimeZone(String, TimeSpan, String, String)方法的重载适用于创建没有调整 (即, 不支持夏令时的时区) 的时区的情况。This overload of the CreateCustomTimeZone(String, TimeSpan, String, String) method is suitable for creating a time zone that has no adjustments (that is, a time zone that does not support daylight saving time). 若要定义包括夏令时调整的时区, 请使用TimeZoneInfo.CreateCustomTimeZone TimeZoneInfo.CreateCustomTimeZone或方法。To define a time zone that includes adjustments for daylight saving time, use either the TimeZoneInfo.CreateCustomTimeZone or the TimeZoneInfo.CreateCustomTimeZone method.

下表显示了提供给TimeZoneInfo.CreateCustomTimeZone方法的参数与方法调用返回的TimeZoneInfo对象属性之间的关系。The following table shows the relationship between the parameters that are provided to the TimeZoneInfo.CreateCustomTimeZone method and the properties of the TimeZoneInfo object that are returned by the method call.

CreateCustomTimeZone 参数CreateCustomTimeZone parameter TimeZoneInfo 属性TimeZoneInfo property
id Id
baseUtcOffset BaseUtcOffset
displayName DisplayName
standardDisplayName StandardName

通常, 时区的标准时间名称及其标识符是相同的。Typically, the time zone's standard time name and its identifier are the same. 但是, 时区标识符的长度不应超过32个字符。However, the length of the time zone's identifier should not exceed 32 characters. 传递给displayName参数的字符串采用相当标准的格式。The string passed to the displayName parameter follows a fairly standard format. 显示名称的第一部分是时区相对于协调世界时的基本偏移量, 由括号括起来 (表示格林尼治标准时间)。The first portion of the display name is the time zone's base offset from Coordinated Universal Time, which is indicated by the acronym GMT (for Greenwich Mean Time), enclosed in parentheses. 后跟一个字符串, 该字符串标识时区本身, 或时区中的一个或多个城市、区域或国家/地区。This is followed by a string that identifies the time zone itself, or one or more of the cities, regions, or countries in the time zone, or both. 例如:For example:

(GMT+02:00) Athens, Beirut, Istanbul, Minsk  
(GMT-02:00) Mid-Atlantic  
(GMT-07:00) Mountain Time (US & Canada)  
另请参阅

CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[]) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[]) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[]) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[])

创建带指定标识符的自定义时区、与协调世界时 (UTC) 的偏移量、显示名称、标准时间名称、夏时制名称和夏时制规则。Creates a custom time zone with a specified identifier, an offset from Coordinated Universal Time (UTC), a display name, a standard time name, a daylight saving time name, and daylight saving time rules.

public:
 static TimeZoneInfo ^ CreateCustomTimeZone(System::String ^ id, TimeSpan baseUtcOffset, System::String ^ displayName, System::String ^ standardDisplayName, System::String ^ daylightDisplayName, cli::array <TimeZoneInfo::AdjustmentRule ^> ^ adjustmentRules);
public static TimeZoneInfo CreateCustomTimeZone (string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName, string daylightDisplayName, TimeZoneInfo.AdjustmentRule[] adjustmentRules);
static member CreateCustomTimeZone : string * TimeSpan * string * string * string * TimeZoneInfo.AdjustmentRule[] -> TimeZoneInfo
Public Shared Function CreateCustomTimeZone (id As String, baseUtcOffset As TimeSpan, displayName As String, standardDisplayName As String, daylightDisplayName As String, adjustmentRules As TimeZoneInfo.AdjustmentRule()) As TimeZoneInfo

参数

id
String String String String

时区的标识符。The time zone's identifier.

baseUtcOffset
TimeSpan TimeSpan TimeSpan TimeSpan

一个对象,它表示此时区和协调世界时 (UTC) 之间的时差。An object that represents the time difference between this time zone and Coordinated Universal Time (UTC).

displayName
String String String String

新时区的显示名称。The display name of the new time zone.

standardDisplayName
String String String String

新时区的标准时间名称。The new time zone's standard time name.

daylightDisplayName
String String String String

新时区的夏令制名称。The daylight saving time name of the new time zone.

adjustmentRules
TimeZoneInfo.AdjustmentRule[]

一个数组,它将基本 UTC 偏移量增加了特定的期间。An array that augments the base UTC offset for a particular period.

返回

一个表示新时区的 TimeZoneInfo 对象。A TimeZoneInfo object that represents the new time zone.

异常

id 参数为空字符串 ("")。The id parameter is an empty string ("").

-or- baseUtcOffset 参数不表示整分钟数。The baseUtcOffset parameter does not represent a whole number of minutes.

baseUtcOffset 参数大于 14 小时或小于 -14 小时。The baseUtcOffset parameter is greater than 14 hours or less than -14 hours.

adjustmentRules 参数中指定的调整规则重叠。The adjustment rules specified in the adjustmentRules parameter overlap.

-or- adjustmentRules 参数中指定的调整规则未按时间顺序排列。The adjustment rules specified in the adjustmentRules parameter are not in chronological order.

-or- adjustmentRules 中的一个或多个元素为 nullOne or more elements in adjustmentRules are null.

- 或 --or- 一个日期可能有多个应用于它的调整规则。A date can have multiple adjustment rules applied to it.

- 或 --or- baseUtcOffset 参数与 adjustmentRules 数组中一个或多个对象的 DaylightDelta 值的和大于 14 小时或小于 -14 小时。The sum of the baseUtcOffset parameter and the DaylightDelta value of one or more objects in the adjustmentRules array is greater than 14 hours or less than -14 hours.

示例

下面的示例为南极洲中的 Palmer 工作站和安特卫普岛创建自定义时区。The following example creates a custom time zone for the Palmer station and Anvers Island in Antarctica. 然后, 它将本地时间转换为新时区中的时间, 并显示结果。It then converts the local time to the time in the new time zone and displays the result.

// Define transition times to/from DST
TimeZoneInfo.TransitionTime startTransition, endTransition;
startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 4, 0, 0), 
                                                                  10, 2, DayOfWeek.Sunday); 
endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 3, 0, 0), 
                                                                3, 2, DayOfWeek.Sunday);
// Define adjustment rule
TimeSpan delta = new TimeSpan(1, 0, 0);
TimeZoneInfo.AdjustmentRule adjustment;
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1999, 10, 1), DateTime.MaxValue.Date, delta, startTransition, endTransition);
// Create array for adjustment rules
TimeZoneInfo.AdjustmentRule[] adjustments = {adjustment};
// Define other custom time zone arguments
string displayName = "(GMT-04:00) Antarctica/Palmer Time";
string standardName = "Palmer Time";
string daylightName = "Palmer Daylight Time";
TimeSpan offset = new TimeSpan(-4, 0, 0);
TimeZoneInfo palmer = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments);
Console.WriteLine("The current time is {0} {1}",  
                  TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, palmer), 
                  palmer.StandardName);
' Define transition times to/from DST
Dim startTransition As TimeZoneInfo.TransitionTime = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#04:00:00#, 10, 2, DayOfWeek.Sunday) 
Dim endTransition As TimeZoneInfo.TransitionTime = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#3:00:00#, 3, 2, DayOfWeek.Sunday)
' Define adjustment rule
Dim delta As TimeSpan = New TimeSpan(1, 0, 0)
Dim adjustment As TimeZoneInfo.AdjustmentRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#10/01/1999#, Date.MaxValue.Date, delta, startTransition, endTransition)
' Create array for adjustment rules
Dim adjustments() As TimeZoneInfo.AdjustmentRule = {adjustment}
' Define other custom time zone arguments
Dim DisplayName As String = "(GMT-04:00) Antarctica/Palmer Time"
Dim standardName As String = "Palmer Standard Time"
Dim daylightName As String = "Palmer Daylight Time"
Dim offset As TimeSpan = New TimeSpan(-4, 0, 0)
Dim palmer As TimeZoneInfo = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments)
Console.WriteLine("The current time is {0} {1}", _ 
                  TimeZoneInfo.ConvertTime(Date.Now, TimeZoneInfo.Local, palmer), _
                  palmer.StandardName)

注解

TimeZoneInfo.CreateCustomTimeZone方法的重载适用于创建支持夏令时的时区。This overload of the TimeZoneInfo.CreateCustomTimeZone method is suitable for creating a time zone that supports daylight saving time. 若要定义不支持夏令时的时区, 请使用TimeZoneInfo.CreateCustomTimeZone TimeZoneInfo.CreateCustomTimeZone或方法。To define a time zone that does not support daylight saving time, use either the TimeZoneInfo.CreateCustomTimeZone or the TimeZoneInfo.CreateCustomTimeZone method.

下表显示了提供给TimeZoneInfo.CreateCustomTimeZone方法的参数与方法调用返回的TimeZoneInfo对象成员之间的关系。The following table shows the relationship between the parameters that are provided to the TimeZoneInfo.CreateCustomTimeZone method and the members of the TimeZoneInfo object that are returned by the method call.

CreateCustomTimeZone 参数CreateCustomTimeZone parameter TimeZoneInfo 成员TimeZoneInfo member
id Id
baseUtcOffset BaseUtcOffset
displayName DisplayName
standardDisplayName StandardName
daylightDisplayName DaylightName
adjustmentRules 由方法返回System.TimeZoneInfo.AdjustmentRule的对象的数组。 GetAdjustmentRulesAn array of System.TimeZoneInfo.AdjustmentRule objects returned by the GetAdjustmentRules method.

通常, 时区的标准时间名称及其标识符是相同的。Typically, the time zone's standard time name and its identifier are the same. 但是, 时区标识符的长度不应超过32个字符。However, the length of the time zone's identifier should not exceed 32 characters. 传递给displayName参数的字符串采用相当标准的格式。The string passed to the displayName parameter follows a fairly standard format. 显示名称的第一部分是时区相对于协调世界时的基本偏移量, 由括号括起来 (表示格林尼治标准时间)。The first portion of the display name is the time zone's base offset from Coordinated Universal Time, which is indicated by the acronym GMT (for Greenwich Mean Time), enclosed in parentheses. 后跟一个字符串, 该字符串标识时区本身, 或时区中的一个或多个城市、区域或国家/地区。This is followed by a string that identifies the time zone itself, or one or more of the cities, regions, or countries in the time zone, or both. 例如:For example:

(GMT+02:00) Athens, Beirut, Istanbul, Minsk  
(GMT-02:00) Mid-Atlantic  
(GMT-07:00) Mountain Time (US & Canada)  

baseUtcOffset参数为时区的所有调整规则定义自定义时区相对于协调世界时 (UTC) 的偏移量。The baseUtcOffset parameter defines the custom time zone's offset from Coordinated Universal Time (UTC) for all of the time zone's adjustment rules. 换句话说, TimeZoneInfo对象模型假定时区与 UTC 的偏移量在时区的整个生命周期内都是固定的, 而与特定调整规则无关。In other words, the TimeZoneInfo object model assumes that the time zone's offset from UTC is constant throughout the life of the time zone, and exists independent of particular adjustment rules. 若要反映已更改其与 UTC 的偏移量的时区, 你必须创建一个新的时区对象。To reflect a time zone that has changed its offset from UTC, you must create a new time zone object.

时区的调整规则是通过执行以下操作定义的:A time zone's adjustment rules are defined by doing the following:

  1. CreateFloatingDateRule 调用CreateFixedDateRule或方法, 以定义每个调整规则的开始和结束转换时间。Calling either the CreateFloatingDateRule or the CreateFixedDateRule method to define the starting and ending transition time for each adjustment rule.

  2. 为每个调整规则调用方法。CreateAdjustmentRuleCalling the CreateAdjustmentRule method for each adjustment rule.

  3. 将调整规则分配给可以作为adjustmentRules参数传递的数组。Assigning the adjustment rules to an array that can be passed as the adjustmentRules parameter.

另请参阅

CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[], Boolean) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[], Boolean) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[], Boolean) CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[], Boolean)

创建带指定标识符的自定义时区、与协调世界时 (UTC) 的偏移量、显示名称、标准时间名称、夏令制名称、夏令制规则以及指示返回的对象是否反映夏令制信息的值。Creates a custom time zone with a specified identifier, an offset from Coordinated Universal Time (UTC), a display name, a standard time name, a daylight saving time name, daylight saving time rules, and a value that indicates whether the returned object reflects daylight saving time information.

public:
 static TimeZoneInfo ^ CreateCustomTimeZone(System::String ^ id, TimeSpan baseUtcOffset, System::String ^ displayName, System::String ^ standardDisplayName, System::String ^ daylightDisplayName, cli::array <TimeZoneInfo::AdjustmentRule ^> ^ adjustmentRules, bool disableDaylightSavingTime);
public static TimeZoneInfo CreateCustomTimeZone (string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName, string daylightDisplayName, TimeZoneInfo.AdjustmentRule[] adjustmentRules, bool disableDaylightSavingTime);
static member CreateCustomTimeZone : string * TimeSpan * string * string * string * TimeZoneInfo.AdjustmentRule[] * bool -> TimeZoneInfo
Public Shared Function CreateCustomTimeZone (id As String, baseUtcOffset As TimeSpan, displayName As String, standardDisplayName As String, daylightDisplayName As String, adjustmentRules As TimeZoneInfo.AdjustmentRule(), disableDaylightSavingTime As Boolean) As TimeZoneInfo

参数

id
String String String String

时区的标识符。The time zone's identifier.

baseUtcOffset
TimeSpan TimeSpan TimeSpan TimeSpan

一个 TimeSpan 对象,它表示此时区和协调世界时 (UTC) 之间的时差。A TimeSpan object that represents the time difference between this time zone and Coordinated Universal Time (UTC).

displayName
String String String String

新时区的显示名称。The display name of the new time zone.

standardDisplayName
String String String String

新时区的标准时间名称。The standard time name of the new time zone.

daylightDisplayName
String String String String

新时区的夏令制名称。The daylight saving time name of the new time zone.

adjustmentRules
TimeZoneInfo.AdjustmentRule[]

一个 TimeZoneInfo.AdjustmentRule 对象的数组,这些对象增加特定期间的基本 UTC 偏移量。An array of TimeZoneInfo.AdjustmentRule objects that augment the base UTC offset for a particular period.

disableDaylightSavingTime
Boolean Boolean Boolean Boolean

如果为 true ,则丢弃包含新对象的 adjustmentRules 中与夏令制相关的任何信息;否则为 falsetrue to discard any daylight saving time-related information present in adjustmentRules with the new object; otherwise, false.

返回

新时区。The new time zone. 如果 disableDaylightSavingTime 参数为 true,则返回的对象没有夏令制数据。If the disableDaylightSavingTime parameter is true, the returned object has no daylight saving time data.

异常

id 参数为空字符串 ("")。The id parameter is an empty string ("").

-or- baseUtcOffset 参数不表示整分钟数。The baseUtcOffset parameter does not represent a whole number of minutes.

baseUtcOffset 参数大于 14 小时或小于 -14 小时。The baseUtcOffset parameter is greater than 14 hours or less than -14 hours.

adjustmentRules 参数中指定的调整规则重叠。The adjustment rules specified in the adjustmentRules parameter overlap.

-or- adjustmentRules 参数中指定的调整规则未按时间顺序排列。The adjustment rules specified in the adjustmentRules parameter are not in chronological order.

-or- adjustmentRules 中的一个或多个元素为 nullOne or more elements in adjustmentRules are null.

-or- 一个日期可能有多个应用于它的调整规则。A date can have multiple adjustment rules applied to it.

-or- baseUtcOffset 参数与 adjustmentRules 数组中一个或多个对象的 DaylightDelta 值的和大于 14 小时或小于 -14 小时。The sum of the baseUtcOffset parameter and the DaylightDelta value of one or more objects in the adjustmentRules array is greater than 14 hours or less than -14 hours.

示例

下面的示例为南极洲中的 Palmer 工作站和安特卫普岛创建自定义时区。The following example creates a custom time zone for the Palmer station and Anvers Island in Antarctica. 它将对disableDaylightSavingTime TimeZoneInfo.CreateCustomTimeZone方法的调用中的参数设置为trueIt sets the disableDaylightSavingTime parameter in the call to the TimeZoneInfo.CreateCustomTimeZone method to true. 然后, 它显示新时区的夏时制名称 (如果有), 以及用于确认新时区没有夏令时信息的调整规则数。It then displays the new time zone's daylight saving time name, if one is present, and the number of adjustment rules to confirm that the new time zone has no daylight saving time information.

// Define transition times to/from DST
TimeZoneInfo.TransitionTime startTransition, endTransition;
startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 4, 0, 0),
                                                                  10, 2, DayOfWeek.Sunday); 
endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1,3, 0, 0), 
                                                                3, 2, DayOfWeek.Sunday);
// Define adjustment rule
TimeSpan delta = new TimeSpan(1, 0, 0);
TimeZoneInfo.AdjustmentRule adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1999, 10, 1), 
                                      DateTime.MaxValue.Date, delta, startTransition, endTransition);
// Create array for adjustment rules
TimeZoneInfo.AdjustmentRule[] adjustments = {adjustment};
// Define other custom time zone arguments
string displayName = "(GMT-04:00) Antarctica/Palmer Time";
string standardName = "Palmer Standard Time";
string daylightName = "Palmer Daylight Time";
TimeSpan offset = new TimeSpan(-4, 0, 0);
// Create custom time zone without copying DST information
TimeZoneInfo palmer = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, 
                                                  daylightName, adjustments, true);
// Indicate whether new time zone//s adjustment rules are present
Console.WriteLine("{0} {1}has {2} adjustment rules.", 
                  palmer.StandardName, 
                  ! (string.IsNullOrEmpty(palmer.DaylightName)) ?  "(" + palmer.DaylightName + ") ": "" , 
                  palmer.GetAdjustmentRules().Length);
// Indicate whether new time zone supports DST
Console.WriteLine("{0} supports DST: {1}", palmer.StandardName, palmer.SupportsDaylightSavingTime);
' Define transition times to/from DST
Dim startTransition As TimeZoneInfo.TransitionTime = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#04:00:00#, 10, 2, DayOfWeek.Sunday) 
Dim endTransition As TimeZoneInfo.TransitionTime = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#3:00:00#, 3, 2, DayOfWeek.Sunday)
' Define adjustment rule
Dim delta As TimeSpan = New TimeSpan(1, 0, 0)
Dim adjustment As TimeZoneInfo.AdjustmentRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#10/01/1999#, Date.MaxValue.Date, delta, startTransition, endTransition)
' Create array for adjustment rules
Dim adjustments() As TimeZoneInfo.AdjustmentRule = {adjustment}
' Define other custom time zone arguments
Dim displayName As String = "(GMT-04:00) Antarctica/Palmer Time"
Dim standardName As String = "Palmer Standard Time"
Dim daylightName As String = "Palmer Daylight Time"
Dim offset As TimeSpan = New TimeSpan(-4, 0, 0)
Dim palmer As TimeZoneInfo = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments, True)
' Indicate whether new time zone's adjustment rules are present
Console.WriteLine("{0} {1}has {2} adjustment rules.", _
                  palmer.StandardName, _
                  IIf(Not String.IsNullOrEmpty(palmer.DaylightName), "(" & palmer.DaylightName & ") ", ""), _
                  palmer.GetAdjustmentRules().Length)
' Indicate whether new time zone supports DST
Console.WriteLine("{0} supports DST: {1}", palmer.StandardName, palmer.SupportsDaylightSavingTime)

注解

您可以使用TimeZoneInfo.CreateCustomTimeZone方法的此重载来创建自定义时区, 该时区的支持夏令时可在运行时通过条件来确定。You can use this overload of the TimeZoneInfo.CreateCustomTimeZone method to create a custom time zone whose support for daylight saving time can be determined by conditions at run time.

下表显示了提供给TimeZoneInfo.CreateCustomTimeZone方法的参数与方法调用返回的TimeZoneInfo对象成员之间的关系。The following table shows the relationship between the parameters that are provided to the TimeZoneInfo.CreateCustomTimeZone method and the members of the TimeZoneInfo object that are returned by the method call.

CreateCustomTimeZone 参数CreateCustomTimeZone parameter TimeZoneInfo 属性TimeZoneInfo property
id Id
baseUtcOffset BaseUtcOffset
displayName DisplayName
standardDisplayName StandardName
daylightDisplayName DaylightName如果disableDaylightSavingTimefalse, 则为;String.Empty 如果disableDaylightSavingTime为。trueDaylightName if disableDaylightSavingTime is false; String.Empty if disableDaylightSavingTime is true.
adjustmentRules TimeZoneInfo.AdjustmentRule GetAdjustmentRules disableDaylightSavingTime如果为,则true为由方法返回的对象的数组; 如果为, 则为方法返回的空数组。GetAdjustmentRules false disableDaylightSavingTimeAn array of TimeZoneInfo.AdjustmentRule objects returned by the GetAdjustmentRules method if disableDaylightSavingTime is false; an empty array returned by the GetAdjustmentRules method if disableDaylightSavingTime is true.
disableDaylightSavingTime SupportsDaylightSavingTime是。Not SupportsDaylightSavingTime.

通常, 时区的标准时间名称及其标识符是相同的。Typically, the time zone's standard time name and its identifier are the same. 但是, 时区标识符的长度不应超过32个字符。However, the length of the time zone's identifier should not exceed 32 characters. 传递给displayName参数的字符串采用相当标准的格式。The string passed to the displayName parameter follows a fairly standard format. 显示名称的第一部分是时区相对于协调世界时的基本偏移量, 由括号括起来 (表示格林尼治标准时间)。The first portion of the display name is the time zone's base offset from Coordinated Universal Time, which is indicated by the acronym GMT (for Greenwich Mean Time), enclosed in parentheses. 后跟一个字符串, 该字符串标识时区本身, 或时区中的一个或多个城市、区域或国家/地区。This is followed by a string that identifies the time zone itself, or one or more of the cities, regions, or countries in the time zone, or both. 例如:For example:

(GMT+02:00) Athens, Beirut, Istanbul, Minsk  
(GMT-02:00) Mid-Atlantic  
(GMT-07:00) Mountain Time (US & Canada)  

时区的调整规则是通过执行以下操作定义的:A time zone's adjustment rules are defined by doing the following:

  1. CreateFloatingDateRule 调用CreateFixedDateRule或方法以定义每个调整规则的起始和结束转换规则。Calling either the CreateFloatingDateRule or the CreateFixedDateRule method to define the starting and ending transition rules for each adjustment rule.

  2. 为每个调整规则调用方法。CreateAdjustmentRuleCalling the CreateAdjustmentRule method for each adjustment rule.

  3. 将调整规则分配给可以作为adjustmentRules参数传递的数组。Assigning the adjustment rules to an array that can be passed as the adjustmentRules parameter.

如果disableDaylightSavingTime参数为false, 则此TimeZoneInfo.CreateCustomTimeZone方法的操作与重载相同。If disableDaylightSavingTime parameter is false, the operation of this method is identical to the TimeZoneInfo.CreateCustomTimeZone overload. 如果disableDaylightSavingTime DaylightNametrue, 则返回的对象不包含调整规则和值为空字符串的属性。If disableDaylightSavingTime is true, the returned object includes no adjustment rules and a DaylightName property whose value is an empty string.

另请参阅

适用于