|
26 | 26 | #elif FIREBASE_PLATFORM_WINDOWS |
27 | 27 | #include <time.h> |
28 | 28 | #include <windows.h> |
| 29 | +// To convert Windows time zone names to IANA time zone names: |
| 30 | +#define UCHAR_TYPE wchar_t |
| 31 | +#include <icu.h> |
29 | 32 | #elif FIREBASE_PLATFORM_LINUX |
30 | 33 | #include <clocale> |
31 | 34 | #include <ctime> |
32 | | -#include <locale> |
33 | 35 | #else |
34 | 36 | #error "Unknown platform." |
35 | 37 | #endif // platform selector |
36 | 38 |
|
37 | 39 | #include <algorithm> |
| 40 | +#include <codecvt> |
| 41 | +#include <locale> |
| 42 | +#include <string> |
38 | 43 | #include <vector> |
39 | 44 |
|
40 | 45 | namespace firebase { |
@@ -90,22 +95,90 @@ std::string GetLocale() { |
90 | 95 | // Get the current time zone, e.g. "US/Pacific" |
91 | 96 | std::string GetTimezone() { |
92 | 97 | #if FIREBASE_PLATFORM_WINDOWS |
93 | | - // If "TZ" environment variable is defined, use it, else use _get_tzname. |
94 | | - int tz_bytes = GetEnvironmentVariable("TZ", nullptr, 0); |
95 | | - if (tz_bytes > 0) { |
96 | | - std::vector<char> buffer(tz_bytes); |
97 | | - GetEnvironmentVariable("TZ", &buffer[0], tz_bytes); |
98 | | - return std::string(&buffer[0]); |
| 98 | + static bool tz_was_set = false; |
| 99 | + if (!tz_was_set) { |
| 100 | + _tzset(); // Set the time zone used by the below functions, based on OS |
| 101 | + // settings or the TZ variable, as appropriate. |
| 102 | + tz_was_set = true; |
99 | 103 | } |
100 | | - int daylight; // daylight savings time? |
101 | | - if (_get_daylight(&daylight) != 0) return ""; |
102 | | - size_t length = 0; // get the needed string length |
103 | | - if (_get_tzname(&length, nullptr, 0, daylight ? 1 : 0) != 0) return ""; |
104 | | - std::vector<char> namebuf(length); |
105 | | - if (_get_tzname(&length, &namebuf[0], length, daylight ? 1 : 0) != 0) |
106 | | - return ""; |
107 | | - std::string name_str(&namebuf[0]); |
108 | | - return name_str; |
| 104 | + // Get the standard time zone name. |
| 105 | + std::string windows_tz_utf8; |
| 106 | + { |
| 107 | + size_t length = 0; // get the needed string length |
| 108 | + if (_get_tzname(&length, nullptr, 0, 0) != 0) return ""; |
| 109 | + std::vector<char> namebuf(length); |
| 110 | + if (_get_tzname(&length, &namebuf[0], length, 0) != 0) return ""; |
| 111 | + windows_tz_utf8 = std::string(&namebuf[0]); |
| 112 | + } |
| 113 | + |
| 114 | + // Convert time zone name to wide string |
| 115 | + std::wstring_convert<std::codecvt_utf8<wchar_t>> to_utf16; |
| 116 | + std::wstring windows_tz_utf16 = to_utf16.from_bytes(windows_tz_utf8); |
| 117 | + |
| 118 | + std::string locale_name = GetLocale(); |
| 119 | + wchar_t iana_time_zone_buffer[128]; |
| 120 | + bool got_time_zone = false; |
| 121 | + if (locale_name.size() >= 5) { |
| 122 | + wcscpy(iana_time_zone_buffer, L""); |
| 123 | + UErrorCode error_code = (UErrorCode)0; |
| 124 | + int32_t size = 0; |
| 125 | + // Try time zone first with the region code returned above, assuming it's at |
| 126 | + // least 5 characters. For example, "en_US" -> "US" |
| 127 | + std::string region_code = std::string(&locale_name[3], 2); |
| 128 | + size = ucal_getTimeZoneIDForWindowsID( |
| 129 | + windows_tz_utf16.c_str(), -1, region_code.c_str(), |
| 130 | + iana_time_zone_buffer, |
| 131 | + sizeof(iana_time_zone_buffer) / sizeof(iana_time_zone_buffer[0]), |
| 132 | + &error_code); |
| 133 | + got_time_zone = (U_SUCCESS(error_code) && size > 0); |
| 134 | + if (!got_time_zone) { |
| 135 | + LogDebug( |
| 136 | + "Couldn't convert Windows time zone '%s' with region '%s' to IANA: " |
| 137 | + "%s (%x). Falling back to non-region time zone conversion.", |
| 138 | + windows_tz_utf8.c_str(), region_code.c_str(), u_errorName(error_code), |
| 139 | + error_code); |
| 140 | + } |
| 141 | + } |
| 142 | + if (!got_time_zone) { |
| 143 | + wcscpy(iana_time_zone_buffer, L""); |
| 144 | + UErrorCode error_code = (UErrorCode)0; |
| 145 | + int32_t size = 0; |
| 146 | + // Try without specifying a region |
| 147 | + size = ucal_getTimeZoneIDForWindowsID( |
| 148 | + windows_tz_utf16.c_str(), -1, nullptr, iana_time_zone_buffer, |
| 149 | + sizeof(iana_time_zone_buffer) / sizeof(iana_time_zone_buffer[0]), |
| 150 | + &error_code); |
| 151 | + got_time_zone = (U_SUCCESS(error_code) && size > 0); |
| 152 | + if (!got_time_zone) { |
| 153 | + // Couldn't convert to IANA |
| 154 | + LogDebug( |
| 155 | + "Couldn't convert time zone '%s' to IANA: %s (%x). Falling back to " |
| 156 | + "Windows time zone name.", |
| 157 | + windows_tz_utf8.c_str(), u_errorName(error_code), error_code); |
| 158 | + } |
| 159 | + } |
| 160 | + if (!got_time_zone) { |
| 161 | + // Return the Windows time zone ID as a backup. |
| 162 | + // In this case, we need to get the correct daylight savings time |
| 163 | + // setting to get the right name. |
| 164 | + int daylight = 0; // daylight savings time? |
| 165 | + if (_get_daylight(&daylight) != 0) return windows_tz_utf8; |
| 166 | + if (daylight) { |
| 167 | + size_t length = 0; // get the needed string length |
| 168 | + if (_get_tzname(&length, nullptr, 0, 1) != 0) return windows_tz_utf8; |
| 169 | + std::vector<char> namebuf(length); |
| 170 | + if (_get_tzname(&length, &namebuf[0], length, 1) != 0) |
| 171 | + return windows_tz_utf8; |
| 172 | + windows_tz_utf8 = std::string(&namebuf[0]); |
| 173 | + } |
| 174 | + return windows_tz_utf8; |
| 175 | + } |
| 176 | + |
| 177 | + std::wstring iana_tz_utf16(iana_time_zone_buffer); |
| 178 | + std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> to_utf8; |
| 179 | + std::string iana_tz_utf8 = to_utf8.to_bytes(iana_tz_utf16); |
| 180 | + return iana_tz_utf8; |
| 181 | + |
109 | 182 | #elif FIREBASE_PLATFORM_LINUX |
110 | 183 | // If TZ environment variable is defined and not empty, use it, else use |
111 | 184 | // tzname. |
|
0 commit comments