Skip to content

TimeZoneInfo.ToSerializedString/FromSerializedString do not round trip on Unix #19794

Description

@justinvp

The following test passes on Windows but fails on Unix:

public static IEnumerable<object[]> SystemTimeZonesTestData()
{
    foreach (TimeZoneInfo tz in TimeZoneInfo.GetSystemTimeZones())
    {
        yield return new object[] { tz };
    }
}

[Theory]
[MemberData(nameof(SystemTimeZonesTestData))]
public static void ToSerializedString_FromSerializedString_RoundTrips(TimeZoneInfo timeZone)
{
    string serialized = timeZone.ToSerializedString();
    TimeZoneInfo deserializedTimeZone = TimeZoneInfo.FromSerializedString(serialized);
    Assert.Equal(timeZone, deserializedTimeZone);
    Assert.Equal(serialized, deserializedTimeZone.ToSerializedString());
}

I believe it's due to how GetAdjustmentRules is implemented on Unix: https://github.com/dotnet/coreclr/blob/0a11492d52faa85c551761f8390be5de9d74e5ec/src/mscorlib/src/System/TimeZoneInfo.Unix.cs#L131-L153

vs. on Windows: https://github.com/dotnet/coreclr/blob/0a11492d52faa85c551761f8390be5de9d74e5ec/src/mscorlib/src/System/TimeZoneInfo.Win32.cs#L87

See dotnet/corefx#14795

Also, when you pass an AdjustmentRule[] array to TimeZoneInfo.CreateCustomTimeZone to create a custom instance of TimeZoneInfo, calling GetAdjustmentRules on Windows will always give you a cloned copy of the same adjustment rules, whereas on Unix it looks like it will modify the adjustment rules that are returned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.DateTimebugdisabled-testThe test is disabled in source code against the issuein-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions