diff options
| author | 2023-05-23 18:32:28 -0400 | |
|---|---|---|
| committer | 2023-06-05 15:15:11 -0400 | |
| commit | 3979c7daa478199cff7bc985fceabcf30fadbc3b (patch) | |
| tree | 52f24a4fed679dbac23aedfa29f2fc79e3d34870 /src | |
| parent | configure_system: Remove external offset on custom rtc (diff) | |
| download | yuzu-3979c7daa478199cff7bc985fceabcf30fadbc3b.tar.gz yuzu-3979c7daa478199cff7bc985fceabcf30fadbc3b.tar.xz yuzu-3979c7daa478199cff7bc985fceabcf30fadbc3b.zip | |
common: Move system time zone string detection
Moves it from Settings to Common::TimeZone, since this algorithm doesn't
depend on the setting. It also lets us use it in other libraries.
common: Various fixes
time_zone: Don't double up the std::abs
Too many absolute values were causing mirrored time zones to resolve
as the same.
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/settings.cpp | 79 | ||||
| -rw-r--r-- | src/common/time_zone.cpp | 75 | ||||
| -rw-r--r-- | src/common/time_zone.h | 6 |
3 files changed, 84 insertions, 76 deletions
diff --git a/src/common/settings.cpp b/src/common/settings.cpp index ed15b8b7d..115fba27d 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp | |||
| @@ -3,18 +3,14 @@ | |||
| 3 | 3 | ||
| 4 | #if __cpp_lib_chrono >= 201907L | 4 | #if __cpp_lib_chrono >= 201907L |
| 5 | #include <chrono> | 5 | #include <chrono> |
| 6 | #else | ||
| 7 | #include <ctime> | ||
| 8 | #include <limits> | ||
| 9 | #endif | 6 | #endif |
| 10 | #include <string_view> | 7 | #include <string_view> |
| 11 | #include <fmt/chrono.h> | ||
| 12 | #include <fmt/core.h> | ||
| 13 | 8 | ||
| 14 | #include "common/assert.h" | 9 | #include "common/assert.h" |
| 15 | #include "common/fs/path_util.h" | 10 | #include "common/fs/path_util.h" |
| 16 | #include "common/logging/log.h" | 11 | #include "common/logging/log.h" |
| 17 | #include "common/settings.h" | 12 | #include "common/settings.h" |
| 13 | #include "common/time_zone.h" | ||
| 18 | 14 | ||
| 19 | namespace Settings { | 15 | namespace Settings { |
| 20 | 16 | ||
| @@ -22,83 +18,22 @@ Values values; | |||
| 22 | static bool configuring_global = true; | 18 | static bool configuring_global = true; |
| 23 | 19 | ||
| 24 | std::string GetTimeZoneString() { | 20 | std::string GetTimeZoneString() { |
| 25 | static constexpr std::array timezones{ | ||
| 26 | "GMT", "GMT", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire", | ||
| 27 | "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0", | ||
| 28 | "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan", | ||
| 29 | "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT", | ||
| 30 | "Poland", "Portugal", "PRC", "PST8PDT", "ROC", "ROK", "Singapore", "Turkey", | ||
| 31 | "UCT", "Universal", "UTC", "W-SU", "WET", "Zulu", | ||
| 32 | }; | ||
| 33 | |||
| 34 | const auto time_zone_index = static_cast<std::size_t>(values.time_zone_index.GetValue()); | 21 | const auto time_zone_index = static_cast<std::size_t>(values.time_zone_index.GetValue()); |
| 35 | ASSERT(time_zone_index < timezones.size()); | 22 | ASSERT(time_zone_index < Common::TimeZone::GetTimeZoneStrings().size()); |
| 23 | |||
| 36 | std::string location_name; | 24 | std::string location_name; |
| 37 | switch (time_zone_index) { | 25 | if (time_zone_index == 0) { // Auto |
| 38 | case 0: { // Auto | ||
| 39 | #if __cpp_lib_chrono >= 201907L | 26 | #if __cpp_lib_chrono >= 201907L |
| 40 | const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb(); | 27 | const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb(); |
| 41 | const std::chrono::time_zone* current_zone = time_zone_data.current_zone(); | 28 | const std::chrono::time_zone* current_zone = time_zone_data.current_zone(); |
| 42 | std::string_view current_zone_name = current_zone->name(); | 29 | std::string_view current_zone_name = current_zone->name(); |
| 43 | location_name = current_zone_name; | 30 | location_name = current_zone_name; |
| 44 | #elif defined(MINGW) | ||
| 45 | // MinGW has broken strftime -- https://sourceforge.net/p/mingw-w64/bugs/793/ | ||
| 46 | // e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400" | ||
| 47 | location_name = timezones[0]; | ||
| 48 | break; | ||
| 49 | #else | 31 | #else |
| 50 | static constexpr std::array offsets{ | 32 | location_name = Common::TimeZone::FindSystemTimeZone(); |
| 51 | 0, 0, 3600, -21600, -19768, 7200, 7509, -1521, -18000, -18000, | ||
| 52 | -75, -75, 0, 0, 0, 0, 0, 27402, -36000, -968, | ||
| 53 | 12344, 8454, -18430, 33539, 40160, 3164, 3600, -25200, -25200, -25196, | ||
| 54 | 41944, 44028, 5040, -2205, 29143, -28800, 29160, 30472, 24925, 6952, | ||
| 55 | 0, 0, 0, 9017, 0, 0, | ||
| 56 | }; | ||
| 57 | |||
| 58 | static constexpr std::array dst{ | ||
| 59 | false, false, true, true, true, true, true, true, false, true, true, true, | ||
| 60 | false, false, false, false, false, true, false, false, true, true, true, true, | ||
| 61 | false, true, true, false, true, true, true, true, true, true, true, true, | ||
| 62 | true, true, true, true, false, false, false, true, true, false, | ||
| 63 | }; | ||
| 64 | |||
| 65 | const auto now = std::time(nullptr); | ||
| 66 | const struct std::tm& local = *std::localtime(&now); | ||
| 67 | const std::string clock_offset_s = fmt::format("{:%z}", local); | ||
| 68 | if (clock_offset_s.empty()) { | ||
| 69 | location_name = timezones[0]; | ||
| 70 | break; | ||
| 71 | } | ||
| 72 | const int hours_offset = std::stoi(clock_offset_s) / 100; | ||
| 73 | const int minutes_offset = std::stoi(clock_offset_s) - hours_offset * 100; | ||
| 74 | const int system_offset = | ||
| 75 | hours_offset * 3600 + minutes_offset * 60 - (local.tm_isdst ? 3600 : 0); | ||
| 76 | |||
| 77 | int min = std::numeric_limits<int>::max(); | ||
| 78 | int min_index = -1; | ||
| 79 | for (u32 i = 2; i < offsets.size(); i++) { | ||
| 80 | // Skip if system is celebrating DST but considered time zone does not | ||
| 81 | if (local.tm_isdst && !dst[i]) { | ||
| 82 | continue; | ||
| 83 | } | ||
| 84 | |||
| 85 | const auto offset = offsets[i]; | ||
| 86 | const int difference = std::abs(std::abs(offset) - std::abs(system_offset)); | ||
| 87 | if (difference < min) { | ||
| 88 | min = difference; | ||
| 89 | min_index = i; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | location_name = timezones[min_index]; | ||
| 94 | #endif | 33 | #endif |
| 95 | break; | 34 | } else { |
| 35 | location_name = Common::TimeZone::GetTimeZoneStrings()[time_zone_index]; | ||
| 96 | } | 36 | } |
| 97 | default: | ||
| 98 | location_name = timezones[time_zone_index]; | ||
| 99 | break; | ||
| 100 | } | ||
| 101 | |||
| 102 | return location_name; | 37 | return location_name; |
| 103 | } | 38 | } |
| 104 | 39 | ||
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 |
diff --git a/src/common/time_zone.h b/src/common/time_zone.h index 99cae6ef2..f574d5c04 100644 --- a/src/common/time_zone.h +++ b/src/common/time_zone.h | |||
| @@ -3,15 +3,21 @@ | |||
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <array> | ||
| 6 | #include <chrono> | 7 | #include <chrono> |
| 7 | #include <string> | 8 | #include <string> |
| 8 | 9 | ||
| 9 | namespace Common::TimeZone { | 10 | namespace Common::TimeZone { |
| 10 | 11 | ||
| 12 | [[nodiscard]] const std::array<const char*, 46>& GetTimeZoneStrings(); | ||
| 13 | |||
| 11 | /// Gets the default timezone, i.e. "GMT" | 14 | /// Gets the default timezone, i.e. "GMT" |
| 12 | [[nodiscard]] std::string GetDefaultTimeZone(); | 15 | [[nodiscard]] std::string GetDefaultTimeZone(); |
| 13 | 16 | ||
| 14 | /// Gets the offset of the current timezone (from the default), in seconds | 17 | /// Gets the offset of the current timezone (from the default), in seconds |
| 15 | [[nodiscard]] std::chrono::seconds GetCurrentOffsetSeconds(); | 18 | [[nodiscard]] std::chrono::seconds GetCurrentOffsetSeconds(); |
| 16 | 19 | ||
| 20 | /// Searches time zone offsets for the closest offset to the system time zone | ||
| 21 | [[nodiscard]] std::string FindSystemTimeZone(); | ||
| 22 | |||
| 17 | } // namespace Common::TimeZone | 23 | } // namespace Common::TimeZone |