Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Use timezone abbreviations as fallback if full names don't exist #45385

Merged
merged 10 commits into from
Mar 5, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@
<Compile Include="$(CommonPath)Interop\Interop.TimeZoneDisplayNameType.cs">
<Link>Common\Interop\Interop.TimeZoneDisplayNameType.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Interop.TimeZoneInfo.cs" Condition="'$(TargetsBrowser)' != 'true'">
<Compile Include="$(CommonPath)Interop\Interop.TimeZoneInfo.cs">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this changed needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we aren't using TimeZoneInfo.GetDisplayName.Invariant.cs anymore (will also remove that file in this PR) so Interop.Globalization is not defined for Browser

<Link>Common\Interop\Interop.TimeZoneInfo.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Interop.Utils.cs">
Expand Down Expand Up @@ -1865,7 +1865,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.Browser.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IO\DriveInfoInternal.Browser.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IO\PersistedFiles.Browser.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.GetDisplayName.Invariant.cs" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this file since it is no longer used? Also, TimeZoneInfo.GetDisplayName.cs can be merged back into TimeZoneInfo.GetDisplayName.Unix.cs.

<Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.GetDisplayName.cs" />
</ItemGroup>
<ItemGroup Condition="'$(IsOSXLike)' == 'true'">
<Compile Include="$(CommonPath)Interop\OSX\Interop.libobjc.cs">
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private unsafe void GetDisplayName(Interop.Globalization.TimeZoneDisplayNameType

// If there is an unknown error, don't set the displayName field.
// It will be set to the abbreviation that was read out of the tzfile.
if (result)
if (result && !string.IsNullOrEmpty(timeZoneDisplayName))
{
displayName = timeZoneDisplayName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ private TimeZoneInfo(byte[] data, string id, bool dstDisabled)
bool[] StandardTime;
bool[] GmtTime;
string? futureTransitionsPosixFormat;
string? standardAbbrevName = null;
string? daylightAbbrevName = null;

// parse the raw TZif bytes; this method can throw ArgumentException when the data is malformed.
TZif_ParseRaw(data, out t, out dts, out typeOfLocalTime, out transitionType, out zoneAbbreviations, out StandardTime, out GmtTime, out futureTransitionsPosixFormat);
Expand All @@ -52,11 +54,11 @@ private TimeZoneInfo(byte[] data, string id, bool dstDisabled)
if (!transitionType[type].IsDst)
{
_baseUtcOffset = transitionType[type].UtcOffset;
_standardDisplayName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[type].AbbreviationIndex);
standardAbbrevName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[type].AbbreviationIndex);
}
else
{
_daylightDisplayName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[type].AbbreviationIndex);
daylightAbbrevName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[type].AbbreviationIndex);
}
}

Expand All @@ -69,14 +71,18 @@ private TimeZoneInfo(byte[] data, string id, bool dstDisabled)
if (!transitionType[i].IsDst)
{
_baseUtcOffset = transitionType[i].UtcOffset;
_standardDisplayName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[i].AbbreviationIndex);
standardAbbrevName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[i].AbbreviationIndex);
}
else
{
_daylightDisplayName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[i].AbbreviationIndex);
daylightAbbrevName = TZif_GetZoneAbbreviation(zoneAbbreviations, transitionType[i].AbbreviationIndex);
}
}
}

// Use abbrev as the fallback
_standardDisplayName = standardAbbrevName;
_daylightDisplayName = daylightAbbrevName;
_displayName = _standardDisplayName;

string uiCulture = CultureInfo.CurrentUICulture.Name.Length == 0 ? FallbackCultureName : CultureInfo.CurrentUICulture.Name; // ICU doesn't work nicely with Invariant
Expand Down
22 changes: 22 additions & 0 deletions src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ public static void Names()
Assert.NotNull(utc.ToString());
}

// Due to ICU size limitations, full daylight/standard names are not included.
// name abbreviations, if available, are used instead
public static TheoryData<TimeZoneInfo, string, string, string> GetBrowser_TimeZoneNamesTestData()
{
return new TheoryData<TimeZoneInfo, string, string, string>
{
{ TimeZoneInfo.FindSystemTimeZoneById(s_strPacific), "(UTC-08:00) PST", "PST", "PDT" },
{ TimeZoneInfo.FindSystemTimeZoneById(s_strSydney), "(UTC+10:00) AEST", "AEST", "AEDT" },
{ TimeZoneInfo.FindSystemTimeZoneById(s_strPerth), "(UTC+08:00) AWST", "AWST", "AWDT" },
{ TimeZoneInfo.FindSystemTimeZoneById(s_strIran), "(UTC+03:30) +0330", "+0330", "+0430" },

{ s_NewfoundlandTz, "(UTC-03:30) NST", "NST", "NDT" },
{ s_catamarcaTz, "(UTC-03:00) -03", "-03", "-02" }
};
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsBrowser))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking PlatformDetection.IsBrowser here can you make a Platform_TimeZoneNamesTestData that returns different data for IsBrowser so that we are testing both cases and verify that we haven't regressed the normal case?

[MemberData(nameof(GetBrowser_TimeZoneNamesTestData))]
public static void Browser_TimeZoneNames(TimeZoneInfo tzi, string displayName, string standardName, string daylightName)
=> Assert.Equal($"DisplayName: {tzi.DisplayName}, StandardName: {tzi.StandardName}, DaylightName: {tzi.DaylightName}",
$"DisplayName: {displayName}, StandardName: {standardName}, DaylightName: {daylightName}");

[Fact]
public static void ConvertTime()
{
Expand Down