Skip to content

Commit

Permalink
fix: uses of ucal_getHostTimeZone on systems that don't have it (#36)
Browse files Browse the repository at this point in the history
The compile-time Windows version check is not a reliable way of checking
whether the system has the `ucal_getHostTimeZone` function in its
icu.dll. That function is only available since ICU 65, which only ships
on Windows 11 and newer. Supporting that is not worth the hassle, so we
keep the "manual" version (for now).
  • Loading branch information
domyd authored Aug 21, 2024
1 parent 67aab8e commit 529ebfd
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions windows/flutter_timezone_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,11 @@ namespace flutter_timezone {
/// </remarks>
void FlutterTimezonePlugin::GetLocalTimezone(
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>>& result) {
// This entire function body could be replaced with a call to `ucal_getHostTimeZone`.
// However, that function as only added in ICU 65, which is only available on Windows 11.
// Once we drop support for older Windows versions, this should be replaced.

// `ucal_getHostTimeZone` is only supported on Windows 10 21H2 or later, so we need to fetch
// the Windows time zone ourselves on older systems.
#if (NTDDI_VERSION >= 0x0A00000B) // NTDDI_WIN10_CO, 21H2
UErrorCode status = U_ZERO_ERROR;
UChar buffer[128];

// We ignore the status value and length result here; if this method fails, `buffer` will
// contain "Etc/Unknown", which is fine.
ucal_getHostTimeZone(buffer, ARRAYSIZE(buffer), &status);

std::wstring tz(buffer);
#pragma warning(suppress : 4244)
result->Success(std::string(tz.begin(), tz.end()));
#else
// Get the current Windows time zone
// Get the current Windows time zone
DYNAMIC_TIME_ZONE_INFORMATION tzInfo;
GetDynamicTimeZoneInformation(&tzInfo);

Expand Down Expand Up @@ -111,7 +100,6 @@ namespace flutter_timezone {
std::wstring tz(buffer);
#pragma warning(suppress : 4244)
result->Success(flutter::EncodableValue(std::string(tz.begin(), tz.end())));
#endif
}

/// <summary>
Expand Down

0 comments on commit 529ebfd

Please sign in to comment.