C# TimeZoneInfo FromSerializedString

Description

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

Syntax

TimeZoneInfo.FromSerializedString has the following syntax.


public static TimeZoneInfo FromSerializedString(
  string source
)

Parameters

TimeZoneInfo.FromSerializedString has the following parameters.

  • source - The string representation of the serialized TimeZoneInfo object.

Returns

TimeZoneInfo.FromSerializedString method returns The original serialized object.

Example

The following example tries to retrieve the myTimeZone time zone from the local system.


using System.IO;/*ww w  .ja va 2  s  .  com*/
using System;
public class MainClass
{
    public static void Main(String[] argv)
    {

        TimeZoneInfo southPole = null;
        const string filename = @".\TimeZoneInfo.txt";
        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);
        TimeSpan delta = new TimeSpan(1, 0, 0);
        TimeZoneInfo.AdjustmentRule adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1989, 10, 1), DateTime.MaxValue.Date, delta, startTransition, endTransition);
        TimeZoneInfo.AdjustmentRule[] adjustments = { adjustment };

        string displayName = "(GMT+12:00) myTimeZone";
        string standardName = "myTimeZone Standard Time";
        string daylightName = "myTimeZone Daylight Time";
        TimeSpan offset = new TimeSpan(12, 0, 0);
        southPole = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments);
        StreamWriter writer = new StreamWriter(filename, true);
        writer.WriteLine(southPole.ToSerializedString());
        writer.Close();


        StreamReader reader = new StreamReader(filename);
        string timeZoneInfo;
        while (reader.Peek() >= 0)
        {
            timeZoneInfo = reader.ReadLine();
            if (timeZoneInfo.Contains("myTimeZone"))
            {
                southPole = TimeZoneInfo.FromSerializedString(timeZoneInfo);
                reader.Close();
                break;
            }
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version