共用方式為


TimeZoneInfo.GetSystemTimeZones 方法

定義

多載

GetSystemTimeZones()

傳回有關本機系統上哪些資訊可用之所有時區的排序集合。

GetSystemTimeZones(Boolean)

ReadOnlyCollection<T>從本機電腦傳回包含所有有效 TimeZone 的 。 此方法 不會 擲回 TimeZoneNotFoundException 或 InvalidTimeZoneException。

GetSystemTimeZones()

來源:
TimeZoneInfo.cs
來源:
TimeZoneInfo.cs
來源:
TimeZoneInfo.cs

傳回有關本機系統上哪些資訊可用之所有時區的排序集合。

public:
 static System::Collections::ObjectModel::ReadOnlyCollection<TimeZoneInfo ^> ^ GetSystemTimeZones();
public static System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones ();
[System.Security.SecurityCritical]
[System.Security.SecurityTreatAsSafe]
public static System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones ();
static member GetSystemTimeZones : unit -> System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo>
[<System.Security.SecurityCritical>]
[<System.Security.SecurityTreatAsSafe>]
static member GetSystemTimeZones : unit -> System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo>
Public Shared Function GetSystemTimeZones () As ReadOnlyCollection(Of TimeZoneInfo)

傳回

TimeZoneInfo 物件的唯讀集合。

屬性

例外狀況

沒有足夠記憶體來儲存所有的時區資訊。

使用者沒有權限可讀取包含時區資訊的登錄機碼。

範例

下列範例會擷取一組時區物件,這些物件代表電腦上定義的時區,並將相關資訊寫入文字檔。

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();
   }
}
open System
open System.Globalization
open System.IO
open System.Collections.ObjectModel

[<EntryPoint>]
let main _ =
    let OUTPUTFILENAME = @"C:\Temp\TimeZoneInfo.txt"

    let dateFormats = CultureInfo.CurrentCulture.DateTimeFormat
    let timeZones = TimeZoneInfo.GetSystemTimeZones() 
    use sw = new StreamWriter(OUTPUTFILENAME, false)

    for timeZone in timeZones do
        let hasDST = timeZone.SupportsDaylightSavingTime
        let offsetFromUtc = timeZone.BaseUtcOffset
    
        sw.WriteLine $"ID: {timeZone.Id}"
        sw.WriteLine $"   Display Name: {timeZone.DisplayName, 40}"
        sw.WriteLine $"   Standard Name: {timeZone.StandardName, 39}"
        sw.Write $"   Daylight Name: {timeZone.DaylightName, 39}"
        sw.Write(if hasDST then "   ***Has " else "   ***Does Not Have ")
        sw.WriteLine "Daylight Saving Time***"
        let offsetString = $"{offsetFromUtc.Hours} hours, {offsetFromUtc.Minutes} minutes"
        sw.WriteLine $"   Offset from UTC: {offsetString, 40}"
        let adjustRules = timeZone.GetAdjustmentRules()
        sw.WriteLine $"   Number of adjustment rules: {adjustRules.Length, 26}"
        if adjustRules.Length > 0 then
            sw.WriteLine "   Adjustment Rules:"
            for rule in adjustRules do
                let transTimeStart = rule.DaylightTransitionStart
                let transTimeEnd = rule.DaylightTransitionEnd 
            
                sw.WriteLine $"      From {rule.DateStart} to {rule.DateEnd}"
                sw.WriteLine $"      Delta: {rule.DaylightDelta}"
                if not transTimeStart.IsFixedDateRule then
                    sw.WriteLine $"      Begins at {transTimeStart.TimeOfDay:t} on {transTimeStart.DayOfWeek} of week {transTimeStart.Week} of {dateFormats.MonthNames[transTimeStart.Month - 1]}"
                    sw.WriteLine $"      Ends at {transTimeEnd.TimeOfDay:t} on {transTimeEnd.DayOfWeek} of week {transTimeEnd.Week} of {dateFormats.MonthNames[transTimeEnd.Month - 1]}"
                else
                    sw.WriteLine $"      Begins at {transTimeStart.TimeOfDay:t} on {transTimeStart.Day} {dateFormats.MonthNames[transTimeStart.Month - 1]}"
                    sw.WriteLine $"      Ends at {transTimeEnd.TimeOfDay:t} on {transTimeEnd.Day} {dateFormats.MonthNames[transTimeEnd.Month - 1]}"
    0
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.IO

Module Example
   Public Sub Main()
      Const OUTPUTFILENAME As String = "C:\Temp\TimeZoneInfo.txt"
      
      Dim timeZones As ReadOnlyCollection(Of TimeZoneInfo) = TimeZoneInfo.GetSystemTimeZones() 
      Dim sw As StreamWriter = New StreamWriter(OUTPUTFILENAME, False)
      
      For Each timeZone As TimeZoneInfo in timeZones
         Dim hasDST As Boolean = timeZone.SupportsDaylightSavingTime
         Dim offsetFromUtc As TimeSpan = timeZone.BaseUtcOffset
         Dim adjustRules() As System.TimeZoneInfo.AdjustmentRule
         Dim offsetString As String
         
         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(iif(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 Then
            sw.WriteLine("   Adjustment Rules:")
            For Each rule As TimeZoneInfo.AdjustmentRule In adjustRules
               Dim transTimeStart As TimeZoneInfo.TransitionTime = rule.DaylightTransitionStart
               Dim transTimeEnd As TimeZoneInfo.TransitionTime = rule.DaylightTransitionEnd 
               
               sw.WriteLine("      From {0} to {1}", rule.DateStart, rule.DateEnd)
               sw.WriteLine("      Delta: {0}", rule.DaylightDelta)
               If Not transTimeStart.IsFixedDateRule
                  sw.WriteLine("      Begins at {0:t} on {1} of week {2} of {3}", transTimeStart.TimeOfDay, _
                                                                                transTimeStart.DayOfWeek, _
                                                                                transTimeStart.Week, _
                                                                                MonthName(transTimeStart.Month))
                  sw.WriteLine("      Ends at {0:t} on {1} of week {2} of {3}", transTimeEnd.TimeOfDay, _
                                                                                transTimeEnd.DayOfWeek, _
                                                                                transTimeEnd.Week, _
                                                                                MonthName(transTimeEnd.Month))
               Else
                  sw.WriteLine("      Begins at {0:t} on {1} {2}", transTimeStart.TimeOfDay, _
                                                                 transTimeStart.Day, _
                                                                 MonthName(transTimeStart.Month))
                  sw.WriteLine("      Ends at {0:t} on {1} {2}", transTimeEnd.TimeOfDay, _
                                                               transTimeEnd.Day, _
                                                               MonthName(transTimeEnd.Month))
               End If
            Next
         End If            
      Next
      sw.Close()
#  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> 介面,這表示可以使用 foreach C#) 中的 (逐一查看,或在 For Each…Next Visual Basic) 語句中使用 (。 迴圈的每個反復專案都會提供集合中的下一個 TimeZoneInfo 物件。

物件的集合 TimeZoneInfo 代表在本機電腦上定義的時區;它不一定為所有時段內的所有時區提供完整的資訊。 如果您的應用程式需要在本機電腦上找不到時區,您可以使用 方法的多 CreateCustomTimeZone 載來建立自訂時區。 如需詳細資訊,請參閱 如何:建立時區而不調整規則如何:使用調整規則建立時區

您也可以呼叫 FindSystemTimeZoneById 方法,並提供您想要擷取為參數的時區識別碼,以判斷本機電腦上是否已定義個別時區。

適用於

GetSystemTimeZones(Boolean)

來源:
TimeZoneInfo.cs
來源:
TimeZoneInfo.cs

ReadOnlyCollection<T>從本機電腦傳回包含所有有效 TimeZone 的 。 此方法 不會 擲回 TimeZoneNotFoundException 或 InvalidTimeZoneException。

public:
 static System::Collections::ObjectModel::ReadOnlyCollection<TimeZoneInfo ^> ^ GetSystemTimeZones(bool skipSorting);
public static System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones (bool skipSorting);
static member GetSystemTimeZones : bool -> System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo>
Public Shared Function GetSystemTimeZones (skipSorting As Boolean) As ReadOnlyCollection(Of TimeZoneInfo)

參數

skipSorting
Boolean

如果 true 為 ,則傳回的集合不一定會排序。

傳回

備註

將 skipSorting 參數設定為 true ,此方法會嘗試避免排序傳回的集合。 當呼叫端不需要已排序的清單,且旨在增強效能時,此選項會很有説明。

適用於