Export (0) Print
Expand All

TimeZoneInfo.CreateCustomTimeZone Method (String, TimeSpan, String, String, String, TimeZoneInfo.AdjustmentRule(), Boolean)

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.

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

'Declaration
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

Parameters

id
Type: System.String

The time zone's identifier.

baseUtcOffset
Type: System.TimeSpan

A TimeSpan object that represents the time difference between this time zone and Coordinated Universal Time (UTC).

displayName
Type: System.String

The display name of the new time zone.

standardDisplayName
Type: System.String

The standard time name of the new time zone.

daylightDisplayName
Type: System.String

The daylight saving time name of the new time zone.

adjustmentRules
Type: System.TimeZoneInfo.AdjustmentRule()

An array of TimeZoneInfo.AdjustmentRule objects that augment the base UTC offset for a particular period.

disableDaylightSavingTime
Type: System.Boolean

true to discard any daylight saving time-related information present in adjustmentRules with the new object; otherwise, false.

Return Value

Type: System.TimeZoneInfo
The new time zone. If the disableDaylightSavingTime parameter is true, the returned object has no daylight saving time data.

ExceptionCondition
ArgumentNullException

The id parameter is Nothing.

ArgumentException

The id parameter is an empty string ("").

-or-

The baseUtcOffset parameter does not represent a whole number of minutes.

ArgumentOutOfRangeException

The baseUtcOffset parameter is greater than 14 hours or less than -14 hours.

InvalidTimeZoneException

The adjustment rules specified in the adjustmentRules parameter overlap.

-or-

The adjustment rules specified in the adjustmentRules parameter are not in chronological order.

-or-

One or more elements in adjustmentRules are Nothing.

-or-

A date can have multiple adjustment rules applied to it.

-or-

The sum of the baseUtcOffset parameter and the TimeZoneInfo.AdjustmentRule.DaylightDelta value of one or more objects in the adjustmentRules array is greater than 14 hours or less than -14 hours.

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.

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 parameter

TimeZoneInfo property

id

Id

baseUtcOffset

BaseUtcOffset

displayName

DisplayName

standardDisplayName

StandardName

daylightDisplayName

DaylightName if disableDaylightSavingTime is false; String.Empty if disableDaylightSavingTime is true.

adjustmentRules

An 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

Not SupportsDaylightSavingTime.

Typically, the time zone's standard time name and its identifier are the same. However, the length of the time zone's identifier should not exceed 32 characters. 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. Calling either the CreateFloatingDateRule or the CreateFixedDateRule method to define the starting and ending transition rules for each adjustment rule.

  2. Calling the CreateAdjustmentRule method for each adjustment rule.

  3. Assigning the adjustment rules to an array that can be passed as the adjustmentRules parameter.

If disableDaylightSavingTime parameter is false, the operation of this method is identical to the TimeZoneInfo.CreateCustomTimeZone overload. If disableDaylightSavingTime is true, the returned object includes no adjustment rules and a DaylightName property whose value is an empty string.

The following example creates a custom time zone for the Palmer station and Anvers Island in Antarctica. It 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 
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)

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft