summaryrefslogtreecommitdiff
path: root/src/common/settings.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/settings.cpp')
-rw-r--r--src/common/settings.cpp79
1 files changed, 7 insertions, 72 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
19namespace Settings { 15namespace Settings {
20 16
@@ -22,83 +18,22 @@ Values values;
22static bool configuring_global = true; 18static bool configuring_global = true;
23 19
24std::string GetTimeZoneString() { 20std::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