diff options
| author | 2023-05-21 15:43:01 -0400 | |
|---|---|---|
| committer | 2023-06-05 15:15:11 -0400 | |
| commit | 3e68a284ae885c6747b9eae6a13a80c86c3e4f2e (patch) | |
| tree | c6a1568324769862002be27d761645be0c87abc6 /src/common/settings.cpp | |
| parent | time_manager: Don't offset RTC by system time zone (diff) | |
| download | yuzu-3e68a284ae885c6747b9eae6a13a80c86c3e4f2e.tar.gz yuzu-3e68a284ae885c6747b9eae6a13a80c86c3e4f2e.tar.xz yuzu-3e68a284ae885c6747b9eae6a13a80c86c3e4f2e.zip | |
settings: Always report a valid time zone
Prevents needing to deduce the non-Switch setting in core. Instead, we
deduce the meaning of this setting where the heresy is committed, in
common.
settings: Remove strftime usage
GetTimeZoneString: Use standard features
Also forces GMT on MinGW due to broken strftime.
Diffstat (limited to 'src/common/settings.cpp')
| -rw-r--r-- | src/common/settings.cpp | 78 |
1 files changed, 76 insertions, 2 deletions
diff --git a/src/common/settings.cpp b/src/common/settings.cpp index ff53e80bb..ed15b8b7d 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp | |||
| @@ -1,7 +1,15 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project | 1 | // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #if __cpp_lib_chrono >= 201907L | ||
| 5 | #include <chrono> | ||
| 6 | #else | ||
| 7 | #include <ctime> | ||
| 8 | #include <limits> | ||
| 9 | #endif | ||
| 4 | #include <string_view> | 10 | #include <string_view> |
| 11 | #include <fmt/chrono.h> | ||
| 12 | #include <fmt/core.h> | ||
| 5 | 13 | ||
| 6 | #include "common/assert.h" | 14 | #include "common/assert.h" |
| 7 | #include "common/fs/path_util.h" | 15 | #include "common/fs/path_util.h" |
| @@ -15,7 +23,7 @@ static bool configuring_global = true; | |||
| 15 | 23 | ||
| 16 | std::string GetTimeZoneString() { | 24 | std::string GetTimeZoneString() { |
| 17 | static constexpr std::array timezones{ | 25 | static constexpr std::array timezones{ |
| 18 | "auto", "default", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire", | 26 | "GMT", "GMT", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire", |
| 19 | "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0", | 27 | "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0", |
| 20 | "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan", | 28 | "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan", |
| 21 | "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT", | 29 | "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT", |
| @@ -25,7 +33,73 @@ std::string GetTimeZoneString() { | |||
| 25 | 33 | ||
| 26 | const auto time_zone_index = static_cast<std::size_t>(values.time_zone_index.GetValue()); | 34 | const auto time_zone_index = static_cast<std::size_t>(values.time_zone_index.GetValue()); |
| 27 | ASSERT(time_zone_index < timezones.size()); | 35 | ASSERT(time_zone_index < timezones.size()); |
| 28 | return timezones[time_zone_index]; | 36 | std::string location_name; |
| 37 | switch (time_zone_index) { | ||
| 38 | case 0: { // Auto | ||
| 39 | #if __cpp_lib_chrono >= 201907L | ||
| 40 | 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(); | ||
| 42 | std::string_view current_zone_name = current_zone->name(); | ||
| 43 | 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 | ||
| 50 | static constexpr std::array offsets{ | ||
| 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 | ||
| 95 | break; | ||
| 96 | } | ||
| 97 | default: | ||
| 98 | location_name = timezones[time_zone_index]; | ||
| 99 | break; | ||
| 100 | } | ||
| 101 | |||
| 102 | return location_name; | ||
| 29 | } | 103 | } |
| 30 | 104 | ||
| 31 | void LogSettings() { | 105 | void LogSettings() { |