diff options
Diffstat (limited to 'src/common/time_zone.cpp')
| -rw-r--r-- | src/common/time_zone.cpp | 75 |
1 files changed, 71 insertions, 4 deletions
diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp index 126836b01..717d751ba 100644 --- a/src/common/time_zone.cpp +++ b/src/common/time_zone.cpp | |||
| @@ -4,12 +4,29 @@ | |||
| 4 | #include <chrono> | 4 | #include <chrono> |
| 5 | #include <iomanip> | 5 | #include <iomanip> |
| 6 | #include <sstream> | 6 | #include <sstream> |
| 7 | #include <fmt/chrono.h> | ||
| 8 | #include <fmt/core.h> | ||
| 7 | 9 | ||
| 8 | #include "common/logging/log.h" | 10 | #include "common/logging/log.h" |
| 11 | #include "common/settings.h" | ||
| 9 | #include "common/time_zone.h" | 12 | #include "common/time_zone.h" |
| 10 | 13 | ||
| 11 | namespace Common::TimeZone { | 14 | namespace Common::TimeZone { |
| 12 | 15 | ||
| 16 | // Time zone strings | ||
| 17 | constexpr std::array timezones{ | ||
| 18 | "GMT", "GMT", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire", | ||
| 19 | "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0", | ||
| 20 | "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan", | ||
| 21 | "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT", | ||
| 22 | "Poland", "Portugal", "PRC", "PST8PDT", "ROC", "ROK", "Singapore", "Turkey", | ||
| 23 | "UCT", "Universal", "UTC", "W-SU", "WET", "Zulu", | ||
| 24 | }; | ||
| 25 | |||
| 26 | const std::array<const char*, 46>& GetTimeZoneStrings() { | ||
| 27 | return timezones; | ||
| 28 | } | ||
| 29 | |||
| 13 | std::string GetDefaultTimeZone() { | 30 | std::string GetDefaultTimeZone() { |
| 14 | return "GMT"; | 31 | return "GMT"; |
| 15 | } | 32 | } |
| @@ -18,10 +35,7 @@ static std::string GetOsTimeZoneOffset() { | |||
| 18 | const std::time_t t{std::time(nullptr)}; | 35 | const std::time_t t{std::time(nullptr)}; |
| 19 | const std::tm tm{*std::localtime(&t)}; | 36 | const std::tm tm{*std::localtime(&t)}; |
| 20 | 37 | ||
| 21 | std::stringstream ss; | 38 | return fmt::format("{:%z}", tm); |
| 22 | ss << std::put_time(&tm, "%z"); // Get the current timezone offset, e.g. "-400", as a string | ||
| 23 | |||
| 24 | return ss.str(); | ||
| 25 | } | 39 | } |
| 26 | 40 | ||
| 27 | static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) { | 41 | static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) { |
| @@ -45,4 +59,57 @@ std::chrono::seconds GetCurrentOffsetSeconds() { | |||
| 45 | return std::chrono::seconds{seconds}; | 59 | return std::chrono::seconds{seconds}; |
| 46 | } | 60 | } |
| 47 | 61 | ||
| 62 | std::string FindSystemTimeZone() { | ||
| 63 | #if defined(MINGW) | ||
| 64 | // 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" | ||
| 66 | return timezones[0]; | ||
| 67 | #else | ||
| 68 | // Time zone offset in seconds from GMT | ||
| 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 | |||
| 89 | const auto now = std::time(nullptr); | ||
| 90 | const struct std::tm& local = *std::localtime(&now); | ||
| 91 | |||
| 92 | const s64 system_offset = GetCurrentOffsetSeconds().count() - (local.tm_isdst ? 3600 : 0); | ||
| 93 | |||
| 94 | int min = std::numeric_limits<int>::max(); | ||
| 95 | int min_index = -1; | ||
| 96 | for (u32 i = 2; i < offsets.size(); i++) { | ||
| 97 | // Skip if system is celebrating DST but considered time zone does not | ||
| 98 | if (local.tm_isdst && !dst[i]) { | ||
| 99 | continue; | ||
| 100 | } | ||
| 101 | |||
| 102 | const auto offset = offsets[i]; | ||
| 103 | const int difference = static_cast<int>(std::abs(offset - system_offset)); | ||
| 104 | if (difference < min) { | ||
| 105 | min = difference; | ||
| 106 | min_index = i; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | system_time_zone_cached = GetTimeZoneStrings()[min_index]; | ||
| 111 | return system_time_zone_cached; | ||
| 112 | #endif | ||
| 113 | } | ||
| 114 | |||
| 48 | } // namespace Common::TimeZone | 115 | } // namespace Common::TimeZone |