英語で読む

次の方法で共有


TimeZoneInfo.FromSerializedString(String) メソッド

定義

文字列を逆シリアル化して、シリアル化された元の TimeZoneInfo オブジェクトを再作成します。

C#
public static TimeZoneInfo FromSerializedString(string source);

パラメーター

source
String

シリアル化された TimeZoneInfo オブジェクトの文字列表現。

戻り値

シリアル化された元のオブジェクト。

例外

source パラメーターが Empty です。

source パラメーターが null 文字列です。

ソース パラメーターを逆シリアル化して 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 および macOS 上の ICU ライブラリ で見つからないタイム ゾーンを作成するために必要なすべてのコードを提供する代わりに方法があります。 カスタム タイム ゾーンを定義し、スタンドアロンの実行可能ファイルで メソッドを ToSerializedString 使用するか、アプリケーションのセットアップ プログラムを使用してタイム ゾーンを文字列として保存できます。 その後、アプリケーションはストレージの場所からこの文字列を取得し、 メソッドを FromSerializedString 使用してインスタンス化できます。

適用対象

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

こちらもご覧ください