diff options
| author | 2023-06-03 17:06:43 -0400 | |
|---|---|---|
| committer | 2023-06-05 15:15:23 -0400 | |
| commit | de1fe66d816f1d28fb7c24cab53961bccd4b3da1 (patch) | |
| tree | 1ba642f314102d45070d882f5ecff1a0282c629c /src | |
| parent | time_zone_binary: Add zoneinfo data (diff) | |
| download | yuzu-de1fe66d816f1d28fb7c24cab53961bccd4b3da1.tar.gz yuzu-de1fe66d816f1d28fb7c24cab53961bccd4b3da1.tar.xz yuzu-de1fe66d816f1d28fb7c24cab53961bccd4b3da1.zip | |
time_zone: Handle offset time zones
time_zone: Remove maybe_unused
time_zone: Use s64 storages
time_zone: Catch by reference
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/time_zone.cpp | 64 |
1 files changed, 26 insertions, 38 deletions
diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp index 717d751ba..d8d7896c6 100644 --- a/src/common/time_zone.cpp +++ b/src/common/time_zone.cpp | |||
| @@ -2,8 +2,10 @@ | |||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include <chrono> | 4 | #include <chrono> |
| 5 | #include <exception> | ||
| 5 | #include <iomanip> | 6 | #include <iomanip> |
| 6 | #include <sstream> | 7 | #include <sstream> |
| 8 | #include <stdexcept> | ||
| 7 | #include <fmt/chrono.h> | 9 | #include <fmt/chrono.h> |
| 8 | #include <fmt/core.h> | 10 | #include <fmt/core.h> |
| 9 | 11 | ||
| @@ -59,56 +61,42 @@ std::chrono::seconds GetCurrentOffsetSeconds() { | |||
| 59 | return std::chrono::seconds{seconds}; | 61 | return std::chrono::seconds{seconds}; |
| 60 | } | 62 | } |
| 61 | 63 | ||
| 64 | // Key is [Hours * 100 + Minutes], multiplied by 100 if DST | ||
| 65 | const static std::map<s64, const char*> off_timezones = { | ||
| 66 | {530, "Asia/Calcutta"}, {930, "Australia/Darwin"}, {845, "Australia/Eucla"}, | ||
| 67 | {103000, "Australia/Adelaide"}, {1030, "Australia/Lord_Howe"}, {630, "Indian/Cocos"}, | ||
| 68 | {1245, "Pacific/Chatham"}, {134500, "Pacific/Chatham"}, {-330, "Canada/Newfoundland"}, | ||
| 69 | {-23000, "Canada/Newfoundland"}, {430, "Asia/Kabul"}, {330, "Asia/Tehran"}, | ||
| 70 | {43000, "Asia/Tehran"}, {545, "Asia/Kathmandu"}, {-930, "Asia/Marquesas"}, | ||
| 71 | }; | ||
| 72 | |||
| 62 | std::string FindSystemTimeZone() { | 73 | std::string FindSystemTimeZone() { |
| 63 | #if defined(MINGW) | 74 | #if defined(MINGW) |
| 64 | // MinGW has broken strftime -- https://sourceforge.net/p/mingw-w64/bugs/793/ | 75 | // MinGW has broken strftime -- https://sourceforge.net/p/mingw-w64/bugs/793/ |
| 65 | // e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400" | 76 | // e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400" |
| 66 | return timezones[0]; | 77 | return timezones[0]; |
| 67 | #else | 78 | #else |
| 68 | // Time zone offset in seconds from GMT | 79 | const s64 seconds = static_cast<s64>(GetCurrentOffsetSeconds().count()); |
| 69 | constexpr std::array offsets{ | ||
| 70 | 0, 0, 3600, -21600, -19768, 7200, 7509, -1521, -18000, -18000, -75, -75, | ||
| 71 | 0, 0, 0, 0, 0, 27402, -36000, -968, 12344, 8454, -18430, 33539, | ||
| 72 | 40160, 3164, 3600, -25200, -25200, -25196, 41944, 44028, 5040, -2205, 29143, -28800, | ||
| 73 | 29160, 30472, 24925, 6952, 0, 0, 0, 9017, 0, 0, | ||
| 74 | }; | ||
| 75 | |||
| 76 | // If the time zone recognizes Daylight Savings Time | ||
| 77 | constexpr std::array dst{ | ||
| 78 | false, false, true, true, true, true, true, true, false, true, true, true, | ||
| 79 | false, false, false, false, false, true, false, false, true, true, true, true, | ||
| 80 | false, true, true, false, true, true, true, true, true, true, true, true, | ||
| 81 | true, true, true, true, false, false, false, true, true, false, | ||
| 82 | }; | ||
| 83 | |||
| 84 | static std::string system_time_zone_cached{}; | ||
| 85 | if (!system_time_zone_cached.empty()) { | ||
| 86 | return system_time_zone_cached; | ||
| 87 | } | ||
| 88 | 80 | ||
| 89 | const auto now = std::time(nullptr); | 81 | const s64 minutes = seconds / 60; |
| 90 | const struct std::tm& local = *std::localtime(&now); | 82 | const s64 hours = minutes / 60; |
| 91 | 83 | ||
| 92 | const s64 system_offset = GetCurrentOffsetSeconds().count() - (local.tm_isdst ? 3600 : 0); | 84 | const s64 minutes_off = minutes - hours * 60; |
| 93 | 85 | ||
| 94 | int min = std::numeric_limits<int>::max(); | 86 | if (minutes_off != 0) { |
| 95 | int min_index = -1; | 87 | const auto the_time = std::time(nullptr); |
| 96 | for (u32 i = 2; i < offsets.size(); i++) { | 88 | const struct std::tm& local = *std::localtime(&the_time); |
| 97 | // Skip if system is celebrating DST but considered time zone does not | 89 | const bool is_dst = local.tm_isdst != 0; |
| 98 | if (local.tm_isdst && !dst[i]) { | 90 | |
| 99 | continue; | 91 | const s64 tz_index = (hours * 100 + minutes_off) * (is_dst ? 100 : 1); |
| 100 | } | ||
| 101 | 92 | ||
| 102 | const auto offset = offsets[i]; | 93 | try { |
| 103 | const int difference = static_cast<int>(std::abs(offset - system_offset)); | 94 | return off_timezones.at(tz_index); |
| 104 | if (difference < min) { | 95 | } catch (std::out_of_range&) { |
| 105 | min = difference; | 96 | LOG_ERROR(Common, "Time zone {} not handled, defaulting to hour offset.", tz_index); |
| 106 | min_index = i; | ||
| 107 | } | 97 | } |
| 108 | } | 98 | } |
| 109 | 99 | return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours)); | |
| 110 | system_time_zone_cached = GetTimeZoneStrings()[min_index]; | ||
| 111 | return system_time_zone_cached; | ||
| 112 | #endif | 100 | #endif |
| 113 | } | 101 | } |
| 114 | 102 | ||