diff options
Diffstat (limited to 'src/common/time_zone.cpp')
| -rw-r--r-- | src/common/time_zone.cpp | 63 |
1 files changed, 59 insertions, 4 deletions
diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp index 126836b01..d8d7896c6 100644 --- a/src/common/time_zone.cpp +++ b/src/common/time_zone.cpp | |||
| @@ -2,14 +2,33 @@ | |||
| 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> | ||
| 9 | #include <fmt/chrono.h> | ||
| 10 | #include <fmt/core.h> | ||
| 7 | 11 | ||
| 8 | #include "common/logging/log.h" | 12 | #include "common/logging/log.h" |
| 13 | #include "common/settings.h" | ||
| 9 | #include "common/time_zone.h" | 14 | #include "common/time_zone.h" |
| 10 | 15 | ||
| 11 | namespace Common::TimeZone { | 16 | namespace Common::TimeZone { |
| 12 | 17 | ||
| 18 | // Time zone strings | ||
| 19 | constexpr std::array timezones{ | ||
| 20 | "GMT", "GMT", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire", | ||
| 21 | "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0", | ||
| 22 | "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan", | ||
| 23 | "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT", | ||
| 24 | "Poland", "Portugal", "PRC", "PST8PDT", "ROC", "ROK", "Singapore", "Turkey", | ||
| 25 | "UCT", "Universal", "UTC", "W-SU", "WET", "Zulu", | ||
| 26 | }; | ||
| 27 | |||
| 28 | const std::array<const char*, 46>& GetTimeZoneStrings() { | ||
| 29 | return timezones; | ||
| 30 | } | ||
| 31 | |||
| 13 | std::string GetDefaultTimeZone() { | 32 | std::string GetDefaultTimeZone() { |
| 14 | return "GMT"; | 33 | return "GMT"; |
| 15 | } | 34 | } |
| @@ -18,10 +37,7 @@ static std::string GetOsTimeZoneOffset() { | |||
| 18 | const std::time_t t{std::time(nullptr)}; | 37 | const std::time_t t{std::time(nullptr)}; |
| 19 | const std::tm tm{*std::localtime(&t)}; | 38 | const std::tm tm{*std::localtime(&t)}; |
| 20 | 39 | ||
| 21 | std::stringstream ss; | 40 | 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 | } | 41 | } |
| 26 | 42 | ||
| 27 | static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) { | 43 | static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) { |
| @@ -45,4 +61,43 @@ std::chrono::seconds GetCurrentOffsetSeconds() { | |||
| 45 | return std::chrono::seconds{seconds}; | 61 | return std::chrono::seconds{seconds}; |
| 46 | } | 62 | } |
| 47 | 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 | |||
| 73 | std::string FindSystemTimeZone() { | ||
| 74 | #if defined(MINGW) | ||
| 75 | // MinGW has broken strftime -- https://sourceforge.net/p/mingw-w64/bugs/793/ | ||
| 76 | // e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400" | ||
| 77 | return timezones[0]; | ||
| 78 | #else | ||
| 79 | const s64 seconds = static_cast<s64>(GetCurrentOffsetSeconds().count()); | ||
| 80 | |||
| 81 | const s64 minutes = seconds / 60; | ||
| 82 | const s64 hours = minutes / 60; | ||
| 83 | |||
| 84 | const s64 minutes_off = minutes - hours * 60; | ||
| 85 | |||
| 86 | if (minutes_off != 0) { | ||
| 87 | const auto the_time = std::time(nullptr); | ||
| 88 | const struct std::tm& local = *std::localtime(&the_time); | ||
| 89 | const bool is_dst = local.tm_isdst != 0; | ||
| 90 | |||
| 91 | const s64 tz_index = (hours * 100 + minutes_off) * (is_dst ? 100 : 1); | ||
| 92 | |||
| 93 | try { | ||
| 94 | return off_timezones.at(tz_index); | ||
| 95 | } catch (std::out_of_range&) { | ||
| 96 | LOG_ERROR(Common, "Time zone {} not handled, defaulting to hour offset.", tz_index); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours)); | ||
| 100 | #endif | ||
| 101 | } | ||
| 102 | |||
| 48 | } // namespace Common::TimeZone | 103 | } // namespace Common::TimeZone |