英語で読む

次の方法で共有


TimeZoneInfo.ToSerializedString メソッド

定義

現在の TimeZoneInfo オブジェクトをシリアル化された文字列に変換します。

C#
public string ToSerializedString();

戻り値

現在の TimeZoneInfo オブジェクトを表す文字列。

次の例では、ローカル システムから南極/南極のタイム ゾーンを取得しようとします。 失敗した場合、コードはアプリケーション ディレクトリ内のテキスト ファイルからタイム ゾーンに関する情報の取得を試みます。 この試行が失敗した場合、コードはタイム ゾーンを作成し、それに関する情報をテキスト ファイルに書き込みます。

C#
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;
}

注釈

通常、Windows システムのレジストリまたは Linux および macOScan 上の ICU ライブラリ で定義されていないタイム ゾーンに依存するアプリケーションでは、 メソッドを CreateCustomTimeZone 使用して、必要なタイム ゾーンをオブジェクトとして TimeZoneInfo インスタンス化できます。 その後、アプリケーションは メソッドを ToSerializedString 呼び出して、タイム ゾーン オブジェクトを文字列に変換できます。

オブジェクトは TimeZoneInfo 、必要に応じてアプリケーションが取得できる場所にも格納する必要があります。 以下の場所が可能です。

  • Windows システム上のレジストリ。

  • アプリケーション リソース ファイル。

  • テキスト ファイルなどの外部ファイル。

適用対象

製品 バージョン
.NET 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 2.0, 2.1

こちらもご覧ください