From c90126cebaf1a2b09442a761fca92c25cf1bbc88 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Tue, 23 May 2023 20:28:53 -0400 Subject: [PATCH] common: Various fixes --- src/common/settings.cpp | 10 ++++++---- src/common/time_zone.cpp | 40 ++++++++++++++++++++++++++++++++++++---- src/common/time_zone.h | 26 +------------------------- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 0ea1302db8..0082fb4e18 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -19,20 +19,22 @@ static bool configuring_global = true; std::string GetTimeZoneString() { const auto time_zone_index = static_cast(values.time_zone_index.GetValue()); - ASSERT(time_zone_index < Common::TimeZone::timezones.size()); + ASSERT(time_zone_index < Common::TimeZone::GetTimeZoneStrings().size()); + std::string location_name; if (time_zone_index == 0) { // Auto #if __cpp_lib_chrono >= 201907L const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb(); const std::chrono::time_zone* current_zone = time_zone_data.current_zone(); std::string_view current_zone_name = current_zone->name(); - return current_zone_name; + location_name = current_zone_name; #else - return Common::TimeZone::FindSystemTimeZone(); + location_name = Common::TimeZone::FindSystemTimeZone(); #endif } else { - return Common::TimeZone::timezones[time_zone_index]; + location_name = Common::TimeZone::GetTimeZoneStrings()[time_zone_index]; } + return location_name; } void LogSettings() { diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp index 3ee0ea6277..7cd4431e5f 100644 --- a/src/common/time_zone.cpp +++ b/src/common/time_zone.cpp @@ -8,10 +8,25 @@ #include #include "common/logging/log.h" +#include "common/settings.h" #include "common/time_zone.h" namespace Common::TimeZone { +// Time zone strings +constexpr std::array timezones{ + "GMT", "GMT", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire", + "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0", + "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan", + "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT", + "Poland", "Portugal", "PRC", "PST8PDT", "ROC", "ROK", "Singapore", "Turkey", + "UCT", "Universal", "UTC", "W-SU", "WET", "Zulu", +}; + +const std::array& GetTimeZoneStrings() { + return timezones; +} + std::string GetDefaultTimeZone() { return "GMT"; } @@ -50,6 +65,22 @@ std::string FindSystemTimeZone() { // e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400" return timezones[0]; #else + // Time zone offset in seconds from GMT + constexpr std::array offsets{ + 0, 0, 3600, -21600, -19768, 7200, 7509, -1521, -18000, -18000, -75, -75, + 0, 0, 0, 0, 0, 27402, -36000, -968, 12344, 8454, -18430, 33539, + 40160, 3164, 3600, -25200, -25200, -25196, 41944, 44028, 5040, -2205, 29143, -28800, + 29160, 30472, 24925, 6952, 0, 0, 0, 9017, 0, 0, + }; + + // If the time zone recognizes Daylight Savings Time + constexpr std::array dst{ + false, false, true, true, true, true, true, true, false, true, true, true, + false, false, false, false, false, true, false, false, true, true, true, true, + false, true, true, false, true, true, true, true, true, true, true, true, + true, true, true, true, false, false, false, true, true, false, + }; + static std::string system_time_zone_cached{}; if (!system_time_zone_cached.empty()) { return system_time_zone_cached; @@ -58,7 +89,7 @@ std::string FindSystemTimeZone() { const auto now = std::time(nullptr); const struct std::tm& local = *std::localtime(&now); - const auto system_offset = GetCurrentOffsetSeconds().count() - (local.tm_isdst ? 3600 : 0); + const s64 system_offset = GetCurrentOffsetSeconds().count() - (local.tm_isdst ? 3600 : 0); int min = std::numeric_limits::max(); int min_index = -1; @@ -69,15 +100,16 @@ std::string FindSystemTimeZone() { } const auto offset = offsets[i]; - const int difference = std::abs(std::abs(offset) - std::abs(system_offset)); + const int difference = + static_cast(std::abs(std::abs(offset) - std::abs(system_offset))); if (difference < min) { min = difference; min_index = i; } } - system_time_zone_cached = timezones[min_index]; - return timezones[min_index]; + system_time_zone_cached = GetTimeZoneStrings()[min_index]; + return system_time_zone_cached; #endif } diff --git a/src/common/time_zone.h b/src/common/time_zone.h index e526ab48df..f574d5c046 100644 --- a/src/common/time_zone.h +++ b/src/common/time_zone.h @@ -9,31 +9,7 @@ namespace Common::TimeZone { -// Time zone strings -constexpr std::array timezones{ - "GMT", "GMT", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire", - "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0", - "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan", - "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT", - "Poland", "Portugal", "PRC", "PST8PDT", "ROC", "ROK", "Singapore", "Turkey", - "UCT", "Universal", "UTC", "W-SU", "WET", "Zulu", -}; - -// Time zone offset in seconds from GMT -constexpr std::array offsets{ - 0, 0, 3600, -21600, -19768, 7200, 7509, -1521, -18000, -18000, -75, -75, - 0, 0, 0, 0, 0, 27402, -36000, -968, 12344, 8454, -18430, 33539, - 40160, 3164, 3600, -25200, -25200, -25196, 41944, 44028, 5040, -2205, 29143, -28800, - 29160, 30472, 24925, 6952, 0, 0, 0, 9017, 0, 0, -}; - -// If the time zone recognizes Daylight Savings Time -constexpr std::array dst{ - false, false, true, true, true, true, true, true, false, true, true, true, - false, false, false, false, false, true, false, false, true, true, true, true, - false, true, true, false, true, true, true, true, true, true, true, true, - true, true, true, true, false, false, false, true, true, false, -}; +[[nodiscard]] const std::array& GetTimeZoneStrings(); /// Gets the default timezone, i.e. "GMT" [[nodiscard]] std::string GetDefaultTimeZone();