語言

TimeZoneInfo.FindSystemTimeZoneById(String) 方法

定義

根據物件識別碼回傳 TimeZoneInfo 物件。

public:
 static TimeZoneInfo ^ FindSystemTimeZoneById(System::String ^ id);
public static TimeZoneInfo FindSystemTimeZoneById(string id);
static member FindSystemTimeZoneById : string -> TimeZoneInfo
Public Shared Function FindSystemTimeZoneById (id As String) As TimeZoneInfo

參數

id
String

時區識別碼,對應於該 Id 屬性。

傳回

一個識別碼為參數值 id 的物件。

例外狀況

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

參數 id 為 null。

未找到所 id 指定的時區識別碼。 這表示名稱相符 id 的時區識別碼不存在,或該識別碼存在但不包含任何時區資料。

該程序沒有讀取包含時區資訊的登錄檔金鑰所需的權限。

找到時區識別碼,但登錄資料已損壞。

範例

以下範例使用該 FindSystemTimeZoneById 方法來取得東京標準時區。 接著用這個 TimeZoneInfo 物件將當地時間換算成東京時間,並判斷是東京標準時間還是東京夏令時間。

using System;

public class Example
{
   public static void Main()
   {
      // Get time in local time zone 
      DateTime thisTime = DateTime.Now;
      Console.WriteLine("Time in {0} zone: {1}", TimeZoneInfo.Local.IsDaylightSavingTime(thisTime) ?
                        TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName, thisTime);
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local));
      // Get Tokyo Standard Time zone
      TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
      DateTime tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst);      
      Console.WriteLine("Time in {0} zone: {1}", tst.IsDaylightSavingTime(tstTime) ?
                        tst.DaylightName : tst.StandardName, tstTime);
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst));
   }
}
// The example displays output like the following when run on a system in the
// U.S. Pacific Standard Time zone:
//       Time in Pacific Standard Time zone: 12/6/2013 10:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
//       Time in Tokyo Standard Time zone: 12/7/2013 3:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
open System

// Get time in local time zone 
let thisTime = DateTime.Now
printfn $"Time in {if TimeZoneInfo.Local.IsDaylightSavingTime thisTime then TimeZoneInfo.Local.DaylightName else TimeZoneInfo.Local.StandardName} zone: {thisTime}"
printfn $"   UTC Time: {TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local)}"
// Get Tokyo Standard Time zone
let tst = TimeZoneInfo.FindSystemTimeZoneById "Tokyo Standard Time"
let tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst)      
printfn $"Time in {if tst.IsDaylightSavingTime tstTime then tst.DaylightName else tst.StandardName} zone: {tstTime}"
printfn $"   UTC Time: {TimeZoneInfo.ConvertTimeToUtc(tstTime, tst)}"
// The example displays output like the following when run on a system in the
// U.S. Pacific Standard Time zone:
//       Time in Pacific Standard Time zone: 12/6/2013 10:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
//       Time in Tokyo Standard Time zone: 12/7/2013 3:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
Module Example
   Public Sub Main()
      ' Get time in local time zone 
      Dim thisTime As Date = Date.Now
      Console.WriteLine("Time in {0} zone: {1}", IIf(TimeZoneInfo.Local.IsDaylightSavingTime(thisTime), 
                        TimeZoneInfo.Local.DaylightName, TimeZoneInfo.Local.StandardName), thisTime)
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local))
      ' Get Tokyo Standard Time zone
      Dim tst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")
      Dim tstTime As Date = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst)      
      Console.WriteLine("Time in {0} zone: {1}", IIf(tst.IsDaylightSavingTime(tstTime), 
                        tst.DaylightName, tst.StandardName), tstTime)
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst))
   End Sub
End Module
' The example displays output like the following when run on a system in the U.S.
' Pacific Standard Time zone:
'    Time in Pacific Standard Time zone: 12/6/2013 10:57:51 AM
'       UTC Time: 12/6/2013 6:57:51 PM
'    Time in Tokyo Standard Time zone: 12/7/2013 3:57:51 AM
'       UTC Time: 12/6/2013 6:57:51 PM

備註

id參數必須在長度上完全對應時區識別碼,但不對應於事件,才能成功匹配;也就是說,與id時區識別碼的比較是不區分大小寫的。 如果你想根據部分匹配來檢索時區物件,可以寫自訂程序來處理該方法回傳TimeZoneInfo的唯讀物件集合GetSystemTimeZones。

在 Windows 系統中,會 FindSystemTimeZoneById 嘗試 id 匹配登錄檔 HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones 分支的子金鑰名稱。 從 .NET 6 開始,Windows 系統也支援 IANA 時區識別碼(例如「America/Los_Angeles」或「Pacific/Auckland」),提供跨平台時區解析。 只有在未啟用 NLS 或未啟用全域化不變模式時,Windows 才支援 IANA ID。 在 Linux 和 macOS 上,它使用了 ICU 函式庫中可用的時區資訊。 如果登錄檔或函式庫沒有你想要的時區資訊,你可以透過呼叫方法的 CreateCustomTimeZone 多載之一,或 FromSerializedString 呼叫一個代表所需時區的物件來 TimeZoneInfo 建立特定的時區。 然而,這些方法呼叫所產生的時區並非系統定義的時間,無法透過該 FindSystemTimeZoneById 方法取得。 這些自訂時區只能透過 or CreateCustomTimeZone 方法呼叫回傳FromSerializedString的物件參考存取。

在 .NET 7 及更早版本中,此方法會為每個方法呼叫回傳一個新 TimeZoneInfo 實例。 這可能會影響在重複呼叫相同識別碼的 FindSystemTimeZoneById 應用程式中效能。 (在 .NET 8 及更新版本中,此方法總是回傳快 TimeZoneInfo 取實例。)

適用於

另請參閱