ToSerializedString Method

TimeZoneInfo.ToSerializedString Method

Converts the current TimeZoneInfo object to a serialized string.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Public Function ToSerializedString As String

Return Value

Type: System.String
A string that represents the current TimeZoneInfo object.

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. The application can then call the ToSerializedString method to convert the time zone object to a string.

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.

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 Function InitializeTimeZone() As TimeZoneInfo
   Dim southPole As TimeZoneInfo = Nothing
   ' Determine if South Pole time zone is defined in system
      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)
               found = True
               Exit Do
            End If   
      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)
      End If
   End Try
   Return southPole
End Function

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2016 Microsoft