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

[iOS][non-icu] HybridGlobalization implement timezoneinfo functions #91458

Merged
merged 15 commits into from
Sep 7, 2023
Prev Previous commit
Next Next commit
Refactoring
  • Loading branch information
mkhamoyan committed Sep 1, 2023
commit cfd0f5ba6e910676cef53b0f867029fabf9add7d
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public sealed partial class TimeZoneInfo
private static string GetUtcStandardDisplayName()
{
System.Diagnostics.Debug.Write("TimeZoneInfo.GetUtcStandardDisplayName is called.\n");
#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
#if TARGET_BROWSER || TARGET_WASI
// For this target, be consistent with other time zone display names that use an abbreviation.
return "UTC";
#elif TARGET_IOS || TARGET_TVOS
if (!GlobalizationMode.Hybrid)
{
// For this target, be consistent with other time zone display names that use an abbreviation.
Expand All @@ -56,7 +59,10 @@ private static string GetUtcStandardDisplayName()
// Helper function to get the full display name for the UTC static time zone instance
private static string GetUtcFullDisplayName(string timeZoneId, string standardDisplayName)
{
#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
#if TARGET_BROWSER || TARGET_WASI
// For this target, be consistent with other time zone display names that use the ID.
return $"(UTC) {timeZoneId}";
#elif TARGET_IOS || TARGET_TVOS
if (!GlobalizationMode.Hybrid)
{
// For this target, be consistent with other time zone display names that use the ID.
Expand Down Expand Up @@ -104,24 +110,23 @@ private static unsafe void GetDisplayName(string timeZoneId, Interop.Globalizati
return;
}

string? timeZoneDisplayName;
bool result = Interop.CallStringMethod(
(buffer, locale, id, type) =>
{
fixed (char* bufferPtr = buffer)
string? timeZoneDisplayName;
bool result = Interop.CallStringMethod(
(buffer, locale, id, type) =>
{
#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
if (GlobalizationMode.Hybrid)
return Interop.Globalization.GetTimeZoneDisplayNameNative(locale, locale.Length, id, id.Length, type, bufferPtr, buffer.Length);
fixed (char* bufferPtr = buffer)
{
#if TARGET_IOS || TARGET_TVOS || TARGET_BROWSER || TARGET_WASI
if (GlobalizationMode.Hybrid)
return Interop.Globalization.GetTimeZoneDisplayNameNative(locale, locale.Length, id, id.Length, type, bufferPtr, buffer.Length);
#endif
return Interop.Globalization.GetTimeZoneDisplayName(locale, id, type, bufferPtr, buffer.Length);
}
},
uiCulture,
timeZoneId,
nameType,
out timeZoneDisplayName);
System.Diagnostics.Debug.Write("After First call TimeZoneInfo.GetDisplayName: timeZoneDisplayName = " + timeZoneDisplayName + "\n");
return Interop.Globalization.GetTimeZoneDisplayName(locale, id, type, bufferPtr, buffer.Length);
}
},
uiCulture,
timeZoneId,
nameType,
out timeZoneDisplayName);

if (!result && uiCulture != FallbackCultureName)
{
Expand All @@ -131,7 +136,7 @@ private static unsafe void GetDisplayName(string timeZoneId, Interop.Globalizati
{
fixed (char* bufferPtr = buffer)
{
#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
#if TARGET_IOS || TARGET_TVOS || TARGET_BROWSER || TARGET_WASI
if (GlobalizationMode.Hybrid)
return Interop.Globalization.GetTimeZoneDisplayNameNative(locale, locale.Length, id, id.Length, type, bufferPtr, buffer.Length);
#endif
Expand All @@ -143,6 +148,7 @@ private static unsafe void GetDisplayName(string timeZoneId, Interop.Globalizati
nameType,
out timeZoneDisplayName);
}

// 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 && !string.IsNullOrEmpty(timeZoneDisplayName))
Expand Down Expand Up @@ -262,7 +268,7 @@ private static void GetFullValueForDisplayNameField(string timeZoneId, TimeSpan

// Helper function that gets an exmplar city name either from ICU or from the IANA time zone ID itself
private static string GetExemplarCityName(string timeZoneId, string uiCultureName)
{//
{
// First try to get the name through the localization data.
string? exemplarCityName = null;
GetDisplayName(timeZoneId, Interop.Globalization.TimeZoneDisplayNameType.ExemplarCity, uiCultureName, ref exemplarCityName);
Expand All @@ -279,13 +285,10 @@ private static string GetExemplarCityName(string timeZoneId, string uiCultureNam
// Helper function that returns an alternative ID using ICU data. Used primarily for converting from Windows IDs.
private static unsafe string? GetAlternativeId(string id, out bool idIsIana)
{
#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
//if (!GlobalizationMode.Hybrid)
//{
// No alternative IDs in this target.
idIsIana = false;
return null;
//}
#if TARGET_IOS || TARGET_TVOS || TARGET_BROWSER || TARGET_WASI
// No alternative IDs in this target.
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved
idIsIana = false;
return null;
#else
idIsIana = false;
return TryConvertWindowsIdToIanaId(id, null, out string? ianaId) ? ianaId : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public sealed partial class TimeZoneInfo
{
private static unsafe bool TryConvertIanaIdToWindowsId(string ianaId, bool allocate, out string? windowsId)
{
#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS || TARGET_BROWSER
#if TARGET_IOS || TARGET_TVOS || TARGET_BROWSER || TARGET_WASI
windowsId = null;
return false;
#else
Expand Down Expand Up @@ -37,9 +37,9 @@ ianaId is null ||

private static unsafe bool TryConvertWindowsIdToIanaId(string windowsId, string? region, bool allocate, out string? ianaId)
{
#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS || TARGET_BROWSER
ianaId = null;
return false;
#if TARGET_IOS || TARGET_TVOS || TARGET_BROWSER || TARGET_WASI
ianaId = null;
return false;
#else
// This functionality is not enabled in the browser for the sake of size reduction.
if (GlobalizationMode.Invariant || GlobalizationMode.UseNls || windowsId is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,8 @@ public AdjustmentRule[] GetAdjustmentRules()
if (GlobalizationMode.Invariant)
return displayName;

#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS || TARGET_BROWSER
#if TARGET_IOS || TARGET_TVOS || TARGET_BROWSER || TARGET_WASI
return displayName;
// if (GlobalizationMode.Hybrid)
// GetFullValueForDisplayNameField(Id, BaseUtcOffset, ref displayName);
#else
Copy link
Member

Choose a reason for hiding this comment

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

aren't we skipping the new native code completely if we return before calling GetFullValueForDisplayNameField here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use NSTimeZone.name for hybrid mode

GetFullValueForDisplayNameField(Id, BaseUtcOffset, ref displayName);
return displayName;
Expand All @@ -250,11 +248,12 @@ public AdjustmentRule[] GetAdjustmentRules()
if (GlobalizationMode.Invariant)
return standardDisplayName;

#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS || TARGET_BROWSER
#if TARGET_IOS || TARGET_TVOS || TARGET_BROWSER || TARGET_WASI
if (!GlobalizationMode.Hybrid)
return standardDisplayName;
#endif
GetStandardDisplayName(Id, ref standardDisplayName);

return standardDisplayName;
}

Expand All @@ -267,7 +266,7 @@ public AdjustmentRule[] GetAdjustmentRules()
if (GlobalizationMode.Invariant)
return daylightDisplayName;

#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
#if TARGET_IOS || TARGET_TVOS || TARGET_BROWSER || TARGET_WASI
if (!GlobalizationMode.Hybrid)
return daylightDisplayName;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<HybridGlobalization>true</HybridGlobalization>
</PropertyGroup>

<!--
Add special trait for running local timezone validation tests on certain devices e.g. in CI to make sure the local timezone is correct and not UTC. Disable these tests otherwise.
-->
<PropertyGroup>
<WithoutCategories Condition="'$(EnableAdditionalTimezoneChecks)' != 'true'">$(WithoutCategories);AdditionalTimezoneChecks</WithoutCategories>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,9 @@ public static void Platform_TimeZoneNames(TimeZoneInfo tzi, string displayName,
displayName = displayName.Replace(c.ToString(), "", StringComparison.Ordinal);
}
}
System.Diagnostics.Debug.Write("Platform_TimeZoneNames: Display Name: " + tzi.DisplayName + "\n"+ alternativeDisplayName + "\n" + displayName + "\n" + standardName + "\n" + daylightName + "\n");

// Assert.True(displayName == tzi.DisplayName || alternativeDisplayName == tzi.DisplayName,
// $"Display Name: Neither '{displayName}' nor '{alternativeDisplayName}' equal to '{tzi.DisplayName}'");
Assert.True(displayName == tzi.DisplayName || alternativeDisplayName == tzi.DisplayName,
$"Display Name: Neither '{displayName}' nor '{alternativeDisplayName}' equal to '{tzi.DisplayName}'");
Assert.Equal(standardName, tzi.StandardName);
Assert.Equal(daylightName, tzi.DaylightName);
}
Expand Down Expand Up @@ -2803,7 +2802,7 @@ public static void TimeZoneInfo_DisplayNameStartsWithOffset(TimeZoneInfo tzi)
if (PlatformDetection.IsNotWindowsNanoServer && !PlatformDetection.IsWindows7)
{
string offset = (match.Groups["sign"].Value == "-" ? "-" : "") + match.Groups["amount"].Value;
TimeSpan ts = TimeSpan.Parse(offset);// this throws exception
TimeSpan ts = TimeSpan.Parse(offset);
if (PlatformDetection.IsWindows &&
tzi.BaseUtcOffset != ts &&
(tzi.Id.Contains("Morocco") || tzi.Id.Contains("Volgograd")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ PALEXPORT ResultCode GlobalizationNative_GetTimeZoneDisplayName(const UChar* loc

#ifdef __APPLE__
PALEXPORT int32_t GlobalizationNative_GetTimeZoneDisplayNameNative(const uint16_t* localeName, int32_t lNameLength, const uint16_t* timeZoneId, int32_t timeZoneIdLength,
TimeZoneDisplayNameType type, uint16_t* result, int32_t resultLength);
#endif
TimeZoneDisplayNameType type, uint16_t* result, int32_t resultLength);
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ int32_t GlobalizationNative_GetTimeZoneDisplayNameNative(const uint16_t* localeN
NSString *locName = [NSString stringWithCharacters: localeName length: lNameLength];
currentLocale = [NSLocale localeWithLocaleIdentifier:locName];
}
//NSTimeZone* timeZone = [NSTimeZone localTimeZone];
NSString* tzName = [NSString stringWithCharacters: timeZoneId length: timeZoneIdLength];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:tzName];
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved
NSTimeZoneNameStyle style;
Expand Down