共用方式為


TimeZoneInfo.FromSerializedString(String) 方法

定義

還原序列化字串,以重新建立原始的序列化 TimeZoneInfo 物件。

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

參數

source
String

已序列化的 TimeZoneInfo 物件之字串表示。

傳回

已序列化的原始物件。

例外狀況

source 參數為 Empty

source 參數是 null 字串。

來源參數無法還原序列化為 TimeZoneInfo 物件。

範例

下列範例會嘗試從本機系統擷取北極/南極時區。 如果失敗,程式碼會嘗試從應用程式目錄中的文字檔擷取時區的相關資訊。 如果此嘗試失敗,程式碼會建立時區,並將時區的相關資訊寫入文字檔。

private TimeZoneInfo InitializeTimeZone()
{
   TimeZoneInfo southPole = null;
   // Determine if South Pole time zone is defined in system
   try
   {
      southPole = TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");
   }
   // Time zone does not exist; create it, store it in a text file, and return it
   catch
   {
      const string filename = @".\TimeZoneInfo.txt";
      bool found = false;
      
      if (File.Exists(filename))
      {
         StreamReader reader = new StreamReader(filename);
         string timeZoneInfo;
         while (reader.Peek() >= 0)
         {
            timeZoneInfo = reader.ReadLine();
            if (timeZoneInfo.Contains("Antarctica/South Pole"))
            {
               southPole = TimeZoneInfo.FromSerializedString(timeZoneInfo);
               reader.Close();
               found = true;
               break;
            }   
         }
      }
      if (! found)
      {               
         // Define transition times to/from DST
         TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 10, 1, DayOfWeek.Sunday); 
         TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 3, 3, DayOfWeek.Sunday);
         // Define adjustment rule
         TimeSpan delta = new TimeSpan(1, 0, 0);
         TimeZoneInfo.AdjustmentRule adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1989, 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+12:00) Antarctica/South Pole";
         string standardName = "Antarctica/South Pole Standard Time";
         string daylightName = "Antarctica/South Pole Daylight Time";
         TimeSpan offset = new TimeSpan(12, 0, 0);
         southPole = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments);
         // Write time zone to the file
         StreamWriter writer = new StreamWriter(filename, true);
         writer.WriteLine(southPole.ToSerializedString());
         writer.Close();
      }
   }
   return southPole;
}
let initializeTimeZone () =
    // Determine if South Pole time zone is defined in system
    try
        TimeZoneInfo.FindSystemTimeZoneById "Antarctica/South Pole Standard Time"
    // Time zone does not exist create it, store it in a text file, and return it
    with _ ->
        let filename = @".\TimeZoneInfo.txt"
        let mutable southPole = Unchecked.defaultof<TimeZoneInfo>
        let mutable found = false
        
        if File.Exists filename then
            use reader = new StreamReader(filename)
            while reader.Peek() >= 0 && not found do
                let timeZoneInfo = reader.ReadLine()
                if timeZoneInfo.Contains "Antarctica/South Pole" then
                    southPole <- TimeZoneInfo.FromSerializedString timeZoneInfo
                    reader.Close()
                    found <- true
        if not found then
            // Define transition times to/from DST
            let startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 2, 0, 0), 10, 1, DayOfWeek.Sunday) 
            let endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 2, 0, 0), 3, 3, DayOfWeek.Sunday)
            // Define adjustment rule
            let delta = TimeSpan(1, 0, 0)
            let adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime(1989, 10, 1), DateTime.MaxValue.Date, delta, startTransition, endTransition)
            // Create array for adjustment rules
            let adjustments = [| adjustment |]
            // Define other custom time zone arguments
            let displayName = "(GMT+12:00) Antarctica/South Pole"
            let standardName = "Antarctica/South Pole Standard Time"
            let daylightName = "Antarctica/South Pole Daylight Time"
            let offset = TimeSpan(12, 0, 0)
            southPole <- TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments)
            // Write time zone to the file
            use writer = new StreamWriter(filename, true)
            writer.WriteLine(southPole.ToSerializedString())
        southPole
Private Function InitializeTimeZone() As TimeZoneInfo
   Dim southPole As TimeZoneInfo = Nothing
   ' Determine if South Pole time zone is defined in system
   Try
      southPole = TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time")
   ' Time zone does not exist; create it, store it in a text file, and return it
   Catch e As TimeZoneNotFoundException
      Dim found As Boolean
      Const filename As String = ".\TimeZoneInfo.txt"
      
      If File.Exists(filename) Then
         Dim reader As StreamReader = New StreamReader(fileName)
         Dim timeZoneString As String
         Do While reader.Peek() >= 0
            timeZoneString = reader.ReadLine()
            If timeZoneString.Contains("Antarctica/South Pole") Then
               southPole = TimeZoneInfo.FromSerializedString(timeZoneString)
               reader.Close()
               found = True
               Exit Do
            End If   
         Loop
      End If
      If Not found Then               
         ' Define transition times to/from DST
         Dim startTransition As TimeZoneInfo.TransitionTime = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#02:00:00#, 10, 1, DayOfWeek.Sunday) 
         Dim endTransition As TimeZoneInfo.TransitionTime = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#02:00:00#, 3, 3, DayOfWeek.Sunday)
         ' Define adjustment rule
         Dim delta As TimeSpan = New TimeSpan(1, 0, 0)
         Dim adjustment As TimeZoneInfo.AdjustmentRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#10/01/1989#, Date.MaxValue.Date, delta, startTransition, endTransition)
         ' Create array for adjustment rules
         Dim adjustments() As TimeZoneInfo.AdjustmentRule = {adjustment}
         ' Define other custom time zone arguments
         Dim displayName As String = "(GMT+12:00) Antarctica/South Pole"
         Dim standardName As String = "Antarctica/South Pole Standard Time"
         Dim daylightName As String = "Antarctica/South Pole Daylight Time"
         Dim offset As TimeSpan = New TimeSpan(12, 0, 0)
         southPole = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments)
         ' Write time zone to the file
         Dim writer As StreamWriter = New StreamWriter(filename, True)
         writer.WriteLine(southPole.ToSerializedString())
         writer.Close()
      End If
   End Try
   Return southPole
End Function

備註

除了提供建立 Windows 登錄或 Linux 和 macOS 上 ICU 程式庫中 找不到的時區所需的所有程式碼,還有一個替代方案。 您可以定義自訂時區,並在獨立可執行檔中使用 ToSerializedString 方法,或使用應用程式的安裝程式將時區儲存為字串。 然後,應用程式可以從其儲存位置擷取此字串,並使用 FromSerializedString 方法將其具現化。

適用於

另請參閱