common: Various fixes

This commit is contained in:
lat9nq 2023-05-23 20:28:53 -04:00
commit c90126ceba
3 changed files with 43 additions and 33 deletions

View file

@ -19,20 +19,22 @@ static bool configuring_global = true;
std::string GetTimeZoneString() { std::string GetTimeZoneString() {
const auto time_zone_index = static_cast<std::size_t>(values.time_zone_index.GetValue()); const auto time_zone_index = static_cast<std::size_t>(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 (time_zone_index == 0) { // Auto
#if __cpp_lib_chrono >= 201907L #if __cpp_lib_chrono >= 201907L
const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb(); const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb();
const std::chrono::time_zone* current_zone = time_zone_data.current_zone(); const std::chrono::time_zone* current_zone = time_zone_data.current_zone();
std::string_view current_zone_name = current_zone->name(); std::string_view current_zone_name = current_zone->name();
return current_zone_name; location_name = current_zone_name;
#else #else
return Common::TimeZone::FindSystemTimeZone(); location_name = Common::TimeZone::FindSystemTimeZone();
#endif #endif
} else { } else {
return Common::TimeZone::timezones[time_zone_index]; location_name = Common::TimeZone::GetTimeZoneStrings()[time_zone_index];
} }
return location_name;
} }
void LogSettings() { void LogSettings() {

View file

@ -8,10 +8,25 @@
#include <fmt/core.h> #include <fmt/core.h>
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/settings.h"
#include "common/time_zone.h" #include "common/time_zone.h"
namespace Common::TimeZone { 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<const char*, 46>& GetTimeZoneStrings() {
return timezones;
}
std::string GetDefaultTimeZone() { std::string GetDefaultTimeZone() {
return "GMT"; return "GMT";
} }
@ -50,6 +65,22 @@ std::string FindSystemTimeZone() {
// e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400" // e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400"
return timezones[0]; return timezones[0];
#else #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{}; static std::string system_time_zone_cached{};
if (!system_time_zone_cached.empty()) { if (!system_time_zone_cached.empty()) {
return system_time_zone_cached; return system_time_zone_cached;
@ -58,7 +89,7 @@ std::string FindSystemTimeZone() {
const auto now = std::time(nullptr); const auto now = std::time(nullptr);
const struct std::tm& local = *std::localtime(&now); 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<int>::max(); int min = std::numeric_limits<int>::max();
int min_index = -1; int min_index = -1;
@ -69,15 +100,16 @@ std::string FindSystemTimeZone() {
} }
const auto offset = offsets[i]; const auto offset = offsets[i];
const int difference = std::abs(std::abs(offset) - std::abs(system_offset)); const int difference =
static_cast<int>(std::abs(std::abs(offset) - std::abs(system_offset)));
if (difference < min) { if (difference < min) {
min = difference; min = difference;
min_index = i; min_index = i;
} }
} }
system_time_zone_cached = timezones[min_index]; system_time_zone_cached = GetTimeZoneStrings()[min_index];
return timezones[min_index]; return system_time_zone_cached;
#endif #endif
} }

View file

@ -9,31 +9,7 @@
namespace Common::TimeZone { namespace Common::TimeZone {
// Time zone strings [[nodiscard]] const std::array<const char*, 46>& GetTimeZoneStrings();
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,
};
/// Gets the default timezone, i.e. "GMT" /// Gets the default timezone, i.e. "GMT"
[[nodiscard]] std::string GetDefaultTimeZone(); [[nodiscard]] std::string GetDefaultTimeZone();