TimeZoneInfo.ToSerializedString TimeZoneInfo.ToSerializedString TimeZoneInfo.ToSerializedString TimeZoneInfo.ToSerializedString Method

定義

現在の TimeZoneInfo オブジェクトをシリアル化された文字列に変換します。Converts the current TimeZoneInfo object to a serialized string.

public:
 System::String ^ ToSerializedString();
public string ToSerializedString ();
member this.ToSerializedString : unit -> string
Public Function ToSerializedString () As String

戻り値

現在の TimeZoneInfo オブジェクトを表す文字列。A string that represents the current TimeZoneInfo object.

次の例では、/南極を取得しようとしました。 ローカル システムからのタイム ゾーン。The following example tries to retrieve the Antarctica/South Pole time zone from the local system. 失敗した場合、コードは、アプリケーション ディレクトリ内のテキスト ファイルからタイム ゾーンに関する情報を取得しようとします。If it fails, the code tries to retrieve information about the time zone from a text file in the application directory. この試行が失敗した場合、コードは、タイム ゾーンを作成し、それに関する情報をテキスト ファイルに書き込みます。If this attempt fails, the code creates the time zone and writes information about it to the text file.

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;
}
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 システムのレジストリで定義されていないタイム ゾーンに依存するアプリケーションを使用できる、CreateCustomTimeZoneとして必要なタイム ゾーンをインスタンス化するメソッドTimeZoneInfoオブジェクト。Applications that rely on time zones that are not typically defined in the registry of Windows systems can use the CreateCustomTimeZone method to instantiate the necessary time zones as TimeZoneInfo objects. アプリケーションを呼び出すことができますし、ToSerializedStringタイム ゾーン オブジェクトを文字列に変換するメソッド。The application can then call the ToSerializedString method to convert the time zone object to a string.

TimeZoneInfoオブジェクトは、アプリケーションが必要なときにどのように取得場所、場所にも格納する必要があります。The TimeZoneInfo object should also be stored in a location where the application can retrieve it when needed. 使用可能な場所は次のとおりです。Possible locations include:

  • レジストリ。The registry.

  • アプリケーション リソース ファイル。An application resource file.

  • 外部ファイル、テキスト ファイルなどです。An external file, such as a text file.

適用対象

こちらもご覧ください