英語で読む

次の方法で共有


TimeZoneInfo.GetSystemTimeZones メソッド

定義

オーバーロード

GetSystemTimeZones()

ローカル システムで情報を使用できるすべてのタイム ゾーンのコレクションを、並べ替えられた状態で返します。

GetSystemTimeZones(Boolean)

ReadOnlyCollection<T>ローカル コンピューターから有効なすべての TimeZone を含む を返します。 このメソッドは、TimeZoneNotFoundException または InvalidTimeZoneException をスロー しません

GetSystemTimeZones()

ソース:
TimeZoneInfo.cs
ソース:
TimeZoneInfo.cs
ソース:
TimeZoneInfo.cs

ローカル システムで情報を使用できるすべてのタイム ゾーンのコレクションを、並べ替えられた状態で返します。

C#
public static System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones();
C#
[System.Security.SecurityCritical]
[System.Security.SecurityTreatAsSafe]
public static System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones();

戻り値

TimeZoneInfo オブジェクトの読み取り専用のコレクション。

属性

例外

すべてのタイム ゾーン情報を格納するにはメモリが不足しています。

ユーザーには、タイム ゾーン情報が含まれているレジストリ キーから読み取る権限がありません。

次の例では、コンピューターで定義されているタイム ゾーンを表すタイム ゾーン オブジェクトのコレクションを取得し、それらに関する情報をテキスト ファイルに書き込みます。

C#
using System;
using System.Globalization;
using System.IO;
using System.Collections.ObjectModel;

public class Example
{
   public static void Main()
   {
      const string OUTPUTFILENAME = @"C:\Temp\TimeZoneInfo.txt";
   
      DateTimeFormatInfo dateFormats = CultureInfo.CurrentCulture.DateTimeFormat;
      ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones(); 
      StreamWriter sw = new StreamWriter(OUTPUTFILENAME, false);
   
      foreach (TimeZoneInfo timeZone in timeZones)
      {
         bool hasDST = timeZone.SupportsDaylightSavingTime;
         TimeSpan offsetFromUtc = timeZone.BaseUtcOffset;
         TimeZoneInfo.AdjustmentRule[] adjustRules;
         string offsetString;
      
         sw.WriteLine("ID: {0}", timeZone.Id);
         sw.WriteLine("   Display Name: {0, 40}", timeZone.DisplayName);
         sw.WriteLine("   Standard Name: {0, 39}", timeZone.StandardName);
         sw.Write("   Daylight Name: {0, 39}", timeZone.DaylightName);
         sw.Write(hasDST ? "   ***Has " : "   ***Does Not Have ");
         sw.WriteLine("Daylight Saving Time***");
         offsetString = String.Format("{0} hours, {1} minutes", offsetFromUtc.Hours, offsetFromUtc.Minutes);
         sw.WriteLine("   Offset from UTC: {0, 40}", offsetString);
         adjustRules = timeZone.GetAdjustmentRules();
         sw.WriteLine("   Number of adjustment rules: {0, 26}", adjustRules.Length);  
         if (adjustRules.Length > 0)
         {
            sw.WriteLine("   Adjustment Rules:");
            foreach (TimeZoneInfo.AdjustmentRule rule in adjustRules)
            {
               TimeZoneInfo.TransitionTime transTimeStart = rule.DaylightTransitionStart;
               TimeZoneInfo.TransitionTime transTimeEnd = rule.DaylightTransitionEnd; 
            
               sw.WriteLine("      From {0} to {1}", rule.DateStart, rule.DateEnd);
               sw.WriteLine("      Delta: {0}", rule.DaylightDelta);
               if (!transTimeStart.IsFixedDateRule)
               {
                  sw.WriteLine("      Begins at {0:t} on {1} of week {2} of {3}", transTimeStart.TimeOfDay, 
                                                                                transTimeStart.DayOfWeek,                                                                                 
                                                                                transTimeStart.Week, 
                                                                                dateFormats.MonthNames[transTimeStart.Month - 1]);
                  sw.WriteLine("      Ends at {0:t} on {1} of week {2} of {3}", transTimeEnd.TimeOfDay,
                                                                                transTimeEnd.DayOfWeek, 
                                                                                transTimeEnd.Week,
                                                                                dateFormats.MonthNames[transTimeEnd.Month - 1]);
               }
               else
               {
                  sw.WriteLine("      Begins at {0:t} on {1} {2}", transTimeStart.TimeOfDay, 
                                                                 transTimeStart.Day, 
                                                                 dateFormats.MonthNames[transTimeStart.Month - 1]);
                  sw.WriteLine("      Ends at {0:t} on {1} {2}", transTimeEnd.TimeOfDay, 
                                                               transTimeEnd.Day, 
                                                               dateFormats.MonthNames[transTimeEnd.Month - 1]);
               }
            }
         }            
      }
      sw.Close();
   }
}
PowerShell
#  Get timezone/date details and open a stream writer
$DateFormats    = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat
$TimeZones      = [System.TimeZoneInfo]::GetSystemTimeZones()
$OutputFileName = 'C:\Temp\TimeZoneInfo.txt'
$Sw             = New-Object -TypeName System.IO.StreamWriter -ArgumentList $OutputFileName,$false
# Write overview information
$Sw.WriteLine('{0} Time zones on this system' -f $TimeZones.Count)

# Process each timezone on the system
# Write details to the streamwriter
foreach ($TimeZone in $TimeZones) {
 $HasDST        = $TimeZone.SupportsDaylightSavingTime
 $OffsetFromUtc = $TimeZone.BaseUtcOffset
 $Sw.WriteLine("ID: {0}" -f $TimeZone.Id)
 $Sw.WriteLine("   Display Name:  {0}" -f $TimeZone.DisplayName)
 $Sw.WriteLine("   Standard Name: {0}" -f $TimeZone.StandardName)
 $Sw.Write("   Daylight Name: {0}" -f $TimeZone.DaylightName)
 $Sw.Write( $(If ($HasDST) {"  ***Has: "} Else {"  ***Does Not Have: "}))
 $Sw.WriteLine("Daylight Saving Time***")
 $OffsetString = "{0} hours, {1} minutes" -f $OffsetFromUtc.Hours, $OffsetFromUtc.Minutes
 $Sw.WriteLine("   Offset from UTC: {0}"  -f $offsetString)
 $AdjustRules = $timeZone.GetAdjustmentRules()
 $Sw.WriteLine("   Number of adjustment rules: {0}" -f $adjustRules.Count)
 If ($AdjustRules.Count -gt 0)
 {
    $Sw.WriteLine("   Adjustment Rules:")
    foreach ($Rule in $AdjustRules)
    {
       $TransTimeStart = $Rule.DaylightTransitionStart
       $TransTimeEnd   = $Rule.DaylightTransitionEnd
       $Sw.WriteLine(("     From   {0} to {1}" -f $Rule.DateStart, $Rule.DateEnd))
       $Sw.WriteLine(("       Delta: {0}" -f $Rule.DaylightDelta))
       if (-Not  $TransTimeStart.IsFixedDateRule)
       {
          $Sw.WriteLine(("       Begins at {0:t} on {1} of week {2} of {3}" -f $TransTimeStart.TimeOfDay, 
                                                                             $TransTimeStart.DayOfWeek,                                                                                 
                                                                             $TransTimeStart.Week, 
                                                                             $DateFormats.MonthNames[$TransTimeStart.Month - 1]))
          $Sw.WriteLine(("       Ends at {0:t} on {1} of week {2} of {3}" -f $TransTimeEnd.TimeOfDay,
                                                                             $TransTimeEnd.DayOfWeek, 
                                                                             $TransTimeEnd.Week,
                                                                             $DateFormats.MonthNames[[int] $TransTimeEnd.Month - 1]))
       }
     else
       {
         $Sw.WriteLine(("       Begins at {0:t} on {1} {2}" -f $TransTimeStart.TimeOfDay, 
                                                             $TransTimeStart.Day, 
                                                             $DateFormats.MonthNames[$transTimeStart.Month - 1]))
         $Sw.WriteLine(("       Ends at {0:t} on {1} {2}"   -f $TransTimeEnd.TimeOfDay, 
                                                             $TransTimeEnd.Day, 
                                                             $DateFormats.MonthNames[$transTimeEnd.Month - 1]))        
       }
    } # End of processing each adjustment rule
 } # End of checking adjustment rules
} # End of processing Time Zones

# Close stream writer then display output in notepad
$Sw.Close() 

#  Output of 1st three time zones:
#  135 Time zones on this system
#  ID: Dateline Standard Time
#     Display Name:  (UTC-12:00) International Date Line West
#     Standard Name: Dateline Standard Time
#     Daylight Name: Dateline Summer Time  ***Does Not Have: Daylight Saving Time***
#     Offset from UTC: -12 hours, 0 minutes
#     Number of adjustment rules: 0
#  ID: UTC-11
#     Display Name:  (UTC-11:00) Co-ordinated Universal Time-11
#     Standard Name: UTC-11
#     Daylight Name: UTC-11  ***Does Not Have: Daylight Saving Time***
#     Offset from UTC: -11 hours, 0 minutes
#     Number of adjustment rules: 0
#  ID: Aleutian Standard Time
#     Display Name:  (UTC-10:00) Aleutian Islands
#     Standard Name: Aleutian Standard Time
#     Daylight Name: Aleutian Summer Time  ***Has: Daylight Saving Time***
#     Offset from UTC: -10 hours, 0 minutes
#     Number of adjustment rules: 2
#     Adjustment Rules:
#       From   01/01/0001 00:00:00 to 31/12/2006 00:00:00
#         Delta: 01:00:00
#         Begins at 02:00 on Sunday of week 1 of April
#         Ends at 02:00 on Sunday of week 5 of October
#       From   01/01/2007 00:00:00 to 31/12/9999 00:00:00
#         Delta: 01:00:00
#         Begins at 02:00 on Sunday of week 2 of March
#         Ends at 02:00 on Sunday of week 1 of November

注釈

メソッドは GetSystemTimeZones 、Windows システムのレジストリの HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones キーのサブキーと、Linux および macOS 上の ICU ライブラリ から、使用可能なすべてのタイム ゾーン情報を取得します。 個々 TimeZoneInfo のオブジェクトの特定の文字列プロパティの値を正常に取得および解析できない場合、このメソッドは値を空の文字列 ("") に設定します。

重要

メソッドは GetSystemTimeZones 、Windows レジストリまたは ICU ライブラリで定義されているタイム ゾーンに対してのみオブジェクトのコレクション TimeZoneInfo を返します。 メソッドのオーバーロードを使用して作成されたタイム ゾーンは CreateCustomTimeZone 含まれません。 これらは、タイム ゾーンの作成方法によって返されるオブジェクト参照を介してのみアクセスできます。

このメソッドによって返されるコレクションは、UTC オフセットで並べ替えられます。同じ UTC オフセットを持つタイム ゾーンの場合は、現在のカルチャを使用して表示名で並べ替えられます。 表示名の詳細については、「」を参照してください DisplayName

このメソッドによって返される オブジェクトはReadOnlyCollection<T>、 インターフェイスをIEnumerable<T>サポートしています。つまり、 (C#では) または For Each…Next (Visual Basic では) ステートメントをforeach使用して反復処理できます。 ループの各反復は、コレクション内の次 TimeZoneInfo のオブジェクトを提供します。

オブジェクトの TimeZoneInfo コレクションは、ローカル コンピューターで定義されているタイム ゾーンを表します。すべての期間のすべてのタイム ゾーンに関する完全な情報を提供するとは限りません。 アプリケーションでローカル コンピューターにタイム ゾーンが見つからない必要がある場合は、 メソッドのオーバーロードを使用してカスタム タイム ゾーンを CreateCustomTimeZone 作成できます。 詳細については、「 方法: 調整規則のないタイム ゾーンを作成する 」および 「方法: 調整規則を使用してタイム ゾーンを作成する」を参照してください。

メソッドを呼び出し FindSystemTimeZoneById 、パラメーターとして取得するタイム ゾーンの識別子を指定することで、ローカル コンピューターで個々のタイム ゾーンが定義されているかどうかを判断することもできます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

GetSystemTimeZones(Boolean)

ソース:
TimeZoneInfo.cs
ソース:
TimeZoneInfo.cs

ReadOnlyCollection<T>ローカル コンピューターから有効なすべての TimeZone を含む を返します。 このメソッドは、TimeZoneNotFoundException または InvalidTimeZoneException をスロー しません

C#
public static System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones(bool skipSorting);

パラメーター

skipSorting
Boolean

の場合 true、返されるコレクションは必ずしも並べ替えられるとは限りません。

戻り値

注釈

skipSorting パラメーターを に true設定すると、メソッドは返されたコレクションの並べ替えを回避しようとします。 このオプションは、呼び出し元が並べ替えられたリストを必要とせず、パフォーマンスの向上を目的としている場合に役立ちます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET 8, 9, 10