方法: 調整規則のあるタイム ゾーンを作成します。How to: Create time zones with adjustment rules

アプリケーションで必要とされる正確なタイム ゾーン情報は、いくつかの原因の特定のシステムに存在していない可能性があります。The precise time zone information that is required by an application may not be present on a particular system for several reasons:

  • タイム ゾーンはローカル システムのレジストリで定義されていません。The time zone has never been defined in the local system's registry.

  • タイム ゾーンに関するデータが変更されたか、レジストリから削除されました。Data about the time zone has been modified or removed from the registry.

  • タイム ゾーンには、過去の特定の期間のタイム ゾーンの調整に関する正確な情報はありません。The time zone does not have accurate information about time zone adjustments for a particular historic period.

このような場合を呼び出すことができます、CreateCustomTimeZoneメソッドをアプリケーションに必要なタイム ゾーンを定義します。In these cases, you can call the CreateCustomTimeZone method to define the time zone required by your application. このメソッドのオーバー ロードを使用して、調整規則の有無は、タイム ゾーンを作成することができます。You can use the overloads of this method to create a time zone with or without adjustment rules. タイム ゾーンが夏時間をサポートする場合は、いずれかの固定または浮動小数点の調整ルールの調整を定義できます。If the time zone supports daylight saving time, you can define adjustments with either fixed or floating adjustment rules. (これらの用語の定義、「タイム ゾーンの用語」のセクションを参照してください。タイム ゾーンの概要)。(For definitions of these terms, see the "Time Zone Terminology" section in Time zone overview.)

重要

呼び出して作成されたカスタムのタイム ゾーン、CreateCustomTimeZoneメソッドは、レジストリに追加されません。Custom time zones created by calling the CreateCustomTimeZone method are not added to the registry. 代わりに、によって返されるオブジェクトの参照を介してのみアクセスすることができます、CreateCustomTimeZoneメソッドの呼び出しです。Instead, they can be accessed only through the object reference returned by the CreateCustomTimeZone method call.

このトピックでは、調整規則のあるタイム ゾーンを作成する方法を示します。This topic shows how to create a time zone with adjustment rules. 夏時間の調整規則をサポートしていないタイム ゾーンを作成するを参照してください。する方法: 調整規則なしのタイム ゾーンを作成するです。To create a time zone that does not support daylight saving time adjustment rules, see How to: Create Time Zones Without Adjustment Rules.

浮動調整規則のあるタイム ゾーンを作成するにはTo create a time zone with floating adjustment rules

  1. 調整 (つまりからの切り替えの各移行につきと標準時に一定の特定の期間) ごとに、次の操作を行います。For each adjustment (that is, for each transition away from and back to standard time over a particular time interval), do the following:

    1. タイム ゾーンの調整の切り替えの開始時刻を定義します。Define the starting transition time for the time zone adjustment.

      呼び出す必要があります、TimeZoneInfo.TransitionTime.CreateFloatingDateRuleメソッドに渡すと、 DateTime 、遷移、遷移の月を定義する整数値、遷移が発生する曜日を定義する整数値の時刻を定義する値とDayOfWeek遷移が発生する曜日を定義する値。You must call the TimeZoneInfo.TransitionTime.CreateFloatingDateRule method and pass it a DateTime value that defines the time of the transition, an integer value that defines the month of the transition, an integer value that defines the week on which the transition occurs, and a DayOfWeek value that defines the day of the week on which the transition occurs. このメソッドの呼び出しをインスタンス化、TimeZoneInfo.TransitionTimeオブジェクト。This method call instantiates a TimeZoneInfo.TransitionTime object.

    2. タイム ゾーンの調整の切り替えの終了時刻を定義します。Define the ending transition time for the time zone adjustment. 別の呼び出しが必要です、TimeZoneInfo.TransitionTime.CreateFloatingDateRuleメソッドです。This requires another call to the TimeZoneInfo.TransitionTime.CreateFloatingDateRule method. このメソッドの呼び出し、2 番目のインスタンスを作成TimeZoneInfo.TransitionTimeオブジェクト。This method call instantiates a second TimeZoneInfo.TransitionTime object.

    3. 呼び出す、CreateAdjustmentRuleメソッドの有効な開始と、調整の終了日を渡すことと、TimeSpanで、切り替え効果、および 2 つの時間を定義するオブジェクトTimeZoneInfo.TransitionTimeタイミングを定義するオブジェクト夏時間との間の遷移時間が発生します。Call the CreateAdjustmentRule method and pass it the effective start and end dates of the adjustment, a TimeSpan object that defines the amount of time in the transition, and the two TimeZoneInfo.TransitionTime objects that define when the transitions to and from daylight saving time occur. このメソッドの呼び出しをインスタンス化、TimeZoneInfo.AdjustmentRuleオブジェクト。This method call instantiates a TimeZoneInfo.AdjustmentRule object.

    4. 割り当てる、TimeZoneInfo.AdjustmentRuleオブジェクトの配列をTimeZoneInfo.AdjustmentRuleオブジェクト。Assign the TimeZoneInfo.AdjustmentRule object to an array of TimeZoneInfo.AdjustmentRule objects.

  2. タイム ゾーンの表示名を定義します。Define the time zone's display name. 表示名に依存して標準形式にタイム ゾーンのオフセット世界協定時刻 (UTC) からかっこで囲まれて、その後に、1 つまたは複数のタイム ゾーン、または 1 つの都市以上、cou のタイム ゾーンを識別する文字列ntries またはタイム ゾーン内の領域。The display name follows a fairly standard format in which the time zone's offset from Coordinated Universal Time (UTC) is enclosed in parentheses and is followed by a string that identifies the time zone, one or more of the cities in the time zone, or one or more of the countries or regions in the time zone.

  3. タイム ゾーンの標準時の名前を定義します。Define the name of the time zone's standard time. 通常、この文字列は、タイム ゾーンの識別子としても使用します。Typically, this string is also used as the time zone's identifier.

  4. タイム ゾーンの夏時間の名前を定義します。Define the name of the time zone's daylight time.

  5. タイム ゾーンの標準の名前とは異なる id を使用する場合は、タイム ゾーン id を定義します。If you want to use a different identifier than the time zone's standard name, define the time zone identifier.

  6. インスタンスを作成、 TimeSpan UTC からのタイム ゾーンのオフセットを定義するオブジェクト。Instantiate a TimeSpan object that defines the time zone's offset from UTC. タイム ゾーンの時刻 (utc) より後に、正の値のオフセットを持っています。Time zones with times that are later than UTC have a positive offset. タイム ゾーンの時刻は UTC よりも前に、負のオフセットを持っています。Time zones with times that are earlier than UTC have a negative offset.

  7. 呼び出す、TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[])新しいタイム ゾーンをインスタンス化するメソッド。Call the TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[]) method to instantiate the new time zone.

Example

次の例では、さまざまな 1918年から現在までの時間間隔の調整規則を含む、United states 中部標準時ゾーンを定義します。The following example defines a Central Standard Time zone for the United States that includes adjustment rules for a variety of time intervals from 1918 to the present.

TimeZoneInfo cst;
// 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 new Central Standard Time zone 6 hours earlier than UTC
// Define rule 1 (for 1918-1919)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 03, 05, DayOfWeek.Sunday);
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 10, 05, DayOfWeek.Sunday); 
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1918, 1, 1), new DateTime(1919, 12, 31), delta, 
                                                           transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment); 
// Define rule 2 (for 1942)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 2, 0, 0), 02, 09);
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1942, 1, 1), new DateTime(1942, 12, 31), 
                                                           delta, transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment);
// Define rule 3 (for 1945)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 23, 0, 0), 08, 14);
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 2, 0, 0), 09, 30);
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1945, 1, 1), new DateTime(1945, 12, 31), 
                                                           delta, transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment);
// Define end rule (for 1967-2006)
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 10, 5, DayOfWeek.Sunday);
// Define rule 4 (for 1967-73)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 04, 05, DayOfWeek.Sunday);
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1967, 1, 1), new DateTime(1973, 12, 31), 
                                                           delta, transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment);
// Define rule 5 (for 1974 only)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 2, 0, 0), 01, 06);
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1974, 1, 1), new DateTime(1974, 12, 31), 
                                                           delta, transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment);
// Define rule 6 (for 1975 only)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 2, 0, 0), 02, 23);
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1975, 1, 1), new DateTime(1975, 12, 31), 
                                                           delta, transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment);
// Define rule 7 (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 8 (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 9 (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, 1, 1), DateTime.MaxValue.Date, 
                                                           delta, transitionRuleStart, transitionRuleEnd);
adjustmentList.Add(adjustment);
              
// Convert list of adjustment rules to an array
TimeZoneInfo.AdjustmentRule[] adjustments = new TimeZoneInfo.AdjustmentRule[adjustmentList.Count];
adjustmentList.CopyTo(adjustments);
   
cst = TimeZoneInfo.CreateCustomTimeZone("Central Standard Time", new TimeSpan(-6, 0, 0), 
      "(GMT-06:00) Central Time (US Only)", "Central Standard Time", 
      "Central Daylight Time", adjustments);
Dim cst As TimeZoneInfo
' 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 new Central Standard Time zone 6 hours earlier than UTC
' Define rule 1 (for 1918-1919)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#02:00:00AM#, 03, 05, DayOfWeek.Sunday)
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#02:00:00AM#, 10, 05, DayOfWeek.Sunday) 
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#01/01/1918#, #12/31/1919#, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add(adjustment) 
' Define rule 2 (for 1942)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(#2:00:00AM#, 02, 09)
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#01/01/1942#, #12/31/1942#, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add(adjustment)
' Define rule 3 (for 1945)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(#11:00:00PM#, 08, 14)
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFixedDateRule(#2:00:00AM#, 09, 30)
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#01/01/1945#, #12/31/1945#, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add(adjustment)
' Define end rule (for 1967-2006)
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#02:00:00AM#, 10, 5, DayOfWeek.Sunday)
' Define rule 4 (for 1967-73)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#2:00:00AM#, 04, 05, DayOfWeek.Sunday)
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#01/01/1967#, #12/31/1973#, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add(adjustment)
' Define rule 5 (for 1974 only)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(#2:00:00AM#, 01, 06)
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#01/01/1974#, #12/31/1974#, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add(adjustment)
' Define rule 6 (for 1975 only)
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(#2:00:00AM#, 02, 23)
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#01/01/1975#, #12/31/1975#, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add(adjustment)
' Define rule 7 (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 8 (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 9 (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)
              
' Convert list of adjustment rules to an array
Dim adjustments(adjustmentList.Count - 1) As TimeZoneInfo.AdjustmentRule
adjustmentList.CopyTo(adjustments)
   
cst = TimeZoneInfo.CreateCustomTimeZone("Central Standard Time", New TimeSpan(-6, 0, 0), _
      "(GMT-06:00) Central Time (US Only)", "Central Standard Time", _
      "Central Daylight Time", adjustments)

この例で作成したタイム ゾーンには、複数の調整規則があります。The time zone created in this example has multiple adjustment rules. 有効な開始と調整規則の終了日と重複しない別の調整規則の日付を確認する注意する必要があります。Care must be taken to ensure that the effective start and end dates of any adjustment rule do not overlap with the dates of another adjustment rule. オーバー ラップがある場合、InvalidTimeZoneExceptionがスローされます。If there is an overlap, an InvalidTimeZoneException is thrown.

調整規則を浮動小数点型、値 5 に渡されます、weekのパラメーター、CreateFloatingDateRuleメソッドを特定の月の最後の週に、遷移が発生することを示します。For floating adjustment rules, the value 5 is passed to the week parameter of the CreateFloatingDateRule method to indicate that the transition occurs on the last week of a particular month.

配列を作成でTimeZoneInfo.AdjustmentRuleで使用するオブジェクト、TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[])メソッドを呼び出すコードは、初期化、調整の数とタイム ゾーンの作成に必要なサイズの配列。In creating the array of TimeZoneInfo.AdjustmentRule objects to use in the TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo+AdjustmentRule[]) method call, the code could initialize the array to the size required by the number of adjustments to be created for the time zone. 代わりに、このコード例では、Add汎用各調整規則を追加するメソッドをList<T>のコレクションTimeZoneInfo.AdjustmentRuleオブジェクト。Instead, this code example calls the Add method to add each adjustment rule to a generic List<T> collection of TimeZoneInfo.AdjustmentRule objects. コードを呼び出すし、CopyToにこのコレクションのメンバーを配列にコピーする方法です。The code then calls the CopyTo method to copy the members of this collection to the array.

また、例では、CreateFixedDateRule固定日付の調整を定義します。The example also uses the CreateFixedDateRule method to define fixed-date adjustments. これは、呼び出しに似ています、CreateFloatingDateRuleメソッドを除いてことは、時間、月、および遷移パラメーターの 1 日のみが必要です。This is similar to calling the CreateFloatingDateRule method, except that it requires only the time, month, and day of the transition parameters.

例は、次のようなコードを使用してテストできます。The example can be tested using code such as the following:

TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

DateTime pastDate1 = new DateTime(1942, 2, 11);
Console.WriteLine("Is {0} daylight saving time: {1}", pastDate1, 
                  cst.IsDaylightSavingTime(pastDate1));

DateTime pastDate2 = new DateTime(1967, 10, 29, 1, 30, 00);
Console.WriteLine("Is {0} ambiguous: {1}", pastDate2, 
                  cst.IsAmbiguousTime(pastDate2));

DateTime pastDate3 = new DateTime(1974, 1, 7, 2, 59, 00);
Console.WriteLine("{0} {1} is {2} {3}", pastDate3, 
                  est.IsDaylightSavingTime(pastDate3) ? 
                      est.DaylightName : est.StandardName, 
                  TimeZoneInfo.ConvertTime(pastDate3, est, cst),  
                  cst.IsDaylightSavingTime(TimeZoneInfo.ConvertTime(pastDate3, est, cst)) ?
                      cst.DaylightName : cst.StandardName);
//
// This code produces the following output to the console:
// 
//    Is 2/11/1942 12:00:00 AM daylight saving time: True
//    Is 10/29/1967 1:30:00 AM ambiguous: True
//    1/7/1974 2:59:00 AM Eastern Standard Time is 1/7/1974 2:59:00 AM Central Daylight Time                            
Dim est As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")

Dim pastDate1 As Date = #2/11/1942#
Console.WriteLine("Is {0} daylight saving time: {1}", pastDate1, _
                  cst.IsDaylightSavingTime(pastDate1))

Dim pastDate2 As Date = #10/29/1967 1:30AM#
Console.WriteLine("Is {0} ambiguous: {1}", pastDate2, _
                  cst.IsAmbiguousTime(pastDate2))

Dim pastDate3 As Date = #1/7/1974 2:59AM#
Console.WriteLine("{0} {1} is {2} {3}", pastDate3, _
                  IIf(est.IsDaylightSavingTime(pastDate3), _
                      est.DaylightName, est.StandardName), _
                  TimeZoneInfo.ConvertTime(pastDate3, est, cst), _ 
                  IIf(cst.IsDaylightSavingTime(TimeZoneInfo.ConvertTime(pastDate3, est, cst)), _
                      cst.DaylightName, cst.StandardName)) 
'
' This code produces the following output to the console:
' 
'    Is 2/11/1942 12:00:00 AM daylight saving time: True
'    Is 10/29/1967 1:30:00 AM ambiguous: True
'    1/7/1974 2:59:00 AM Eastern Standard Time is 1/7/1974 2:59:00 AM Central Daylight Time                            

コードのコンパイルCompiling the code

この例で必要な要素は次のとおりです。This example requires:

  • される System.Core.dll への参照をプロジェクトに追加します。That a reference to System.Core.dll be added to the project.

  • 次の名前空間は、インポートします。That the following namespaces be imported:

    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    
    Imports System.Collections.Generic
    Imports System.Collections.ObjectModel
    

関連項目See also

日付、時刻、およびタイム ゾーン タイム ゾーンの概要 する方法: 調整規則のないタイム ゾーンを作成Dates, times, and time zones Time zone overview How to: Create time zones without adjustment rules