Export (0) Print
Expand All

TimeZoneInfo.CreateCustomTimeZone Method (String, TimeSpan, String, String, String, TimeZoneInfo.AdjustmentRule[])

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.

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

public static TimeZoneInfo CreateCustomTimeZone(
	string id,
	TimeSpan baseUtcOffset,
	string displayName,
	string standardDisplayName,
	string daylightDisplayName,
	TimeZoneInfo.AdjustmentRule[] adjustmentRules
)

Parameters

id
Type: System.String

The time zone's identifier.

baseUtcOffset
Type: System.TimeSpan

An 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 new time zone's standard time name.

daylightDisplayName
Type: System.String

The daylight saving time name of the new time zone.

adjustmentRules
Type: System.TimeZoneInfo.AdjustmentRule[]

An array that augments the base UTC offset for a particular period.

Return Value

Type: System.TimeZoneInfo
A TimeZoneInfo object that represents the new time zone.

ExceptionCondition
ArgumentNullException

The id parameter is null.

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 null.

-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.

This overload of the TimeZoneInfo.CreateCustomTimeZone method is suitable for creating a time zone that supports daylight saving time. To define a time zone that does not support daylight saving time, use either the TimeZoneInfo.CreateCustomTimeZone or the TimeZoneInfo.CreateCustomTimeZone method.

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 member

id

Id

baseUtcOffset

BaseUtcOffset

displayName

DisplayName

standardDisplayName

StandardName

daylightDisplayName

DaylightName

adjustmentRules

An 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. 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)

The baseUtcOffset parameter defines the custom time zone's offset from Coordinated Universal Time (UTC) for all of the time zone's adjustment rules. 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. 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. Calling either the CreateFloatingDateRule or the CreateFixedDateRule method to define the starting and ending transition time 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.

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);

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft