Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TimeZoneInfo.FromSerializedString Method

Deserializes a string to re-create an original serialized TimeZoneInfo object.

Namespace:  System
Assembly:  System.Core (in System.Core.dll)
public static TimeZoneInfo FromSerializedString(
	string source
)

Parameters

source
Type: System.String

The string representation of the serialized TimeZoneInfo object.

Return Value

Type: System.TimeZoneInfo
A TimeZoneInfo object.
ExceptionCondition
ArgumentException

The source parameter is String.Empty.

ArgumentNullException

The source parameter is a null string.

SerializationException

The source parameter cannot be deserialized back into a TimeZoneInfo object.

There is an alternative to providing all the code required to create a time zone that is not found in the registry. You can define a custom time zone and either use the ToSerializedString method in a standalone executable or use an application's setup program to save the time zone as a string. The application can then retrieve this string from its storage location and instantiate it using the FromSerializedString method.

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 the time zone 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;
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.