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

정의

현재 TimeZoneInfo 개체를 serialize된 문자열로 변환합니다.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 시스템의 레지스트리에서 일반적으로 정의 되지 않은 표준 시간대를 사용 하는 응용 프로그램 또는 Linux 및 macoscan의 ICU 라이브러리CreateCustomTimeZone 메서드를 사용 하 여 TimeZoneInfo 필요한 표준 시간대를 개체로 인스턴스화할 수 있습니다.Applications that rely on time zones that are not typically defined in the registry of Windows systems or the ICU Library on Linux and macOScan 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:

  • Windows 시스템의 레지스트리The registry on Windows systems.

  • 애플리케이션 리소스 파일입니다.An application resource file.

  • 텍스트 파일과 같은 외부 파일입니다.An external file, such as a text file.

적용 대상

추가 정보