time_zone: Handle offset time zones
time_zone: Remove maybe_unused time_zone: Use s64 storages time_zone: Catch by reference
This commit is contained in:
parent
84642bdd3f
commit
de1fe66d81
1 changed files with 25 additions and 37 deletions
|
@ -2,8 +2,10 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <exception>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
#include <fmt/chrono.h>
|
#include <fmt/chrono.h>
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
|
|
||||||
|
@ -59,56 +61,42 @@ std::chrono::seconds GetCurrentOffsetSeconds() {
|
||||||
return std::chrono::seconds{seconds};
|
return std::chrono::seconds{seconds};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Key is [Hours * 100 + Minutes], multiplied by 100 if DST
|
||||||
|
const static std::map<s64, const char*> off_timezones = {
|
||||||
|
{530, "Asia/Calcutta"}, {930, "Australia/Darwin"}, {845, "Australia/Eucla"},
|
||||||
|
{103000, "Australia/Adelaide"}, {1030, "Australia/Lord_Howe"}, {630, "Indian/Cocos"},
|
||||||
|
{1245, "Pacific/Chatham"}, {134500, "Pacific/Chatham"}, {-330, "Canada/Newfoundland"},
|
||||||
|
{-23000, "Canada/Newfoundland"}, {430, "Asia/Kabul"}, {330, "Asia/Tehran"},
|
||||||
|
{43000, "Asia/Tehran"}, {545, "Asia/Kathmandu"}, {-930, "Asia/Marquesas"},
|
||||||
|
};
|
||||||
|
|
||||||
std::string FindSystemTimeZone() {
|
std::string FindSystemTimeZone() {
|
||||||
#if defined(MINGW)
|
#if defined(MINGW)
|
||||||
// MinGW has broken strftime -- https://sourceforge.net/p/mingw-w64/bugs/793/
|
// MinGW has broken strftime -- https://sourceforge.net/p/mingw-w64/bugs/793/
|
||||||
// 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
|
const s64 seconds = static_cast<s64>(GetCurrentOffsetSeconds().count());
|
||||||
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
|
const s64 minutes = seconds / 60;
|
||||||
constexpr std::array dst{
|
const s64 hours = minutes / 60;
|
||||||
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{};
|
const s64 minutes_off = minutes - hours * 60;
|
||||||
if (!system_time_zone_cached.empty()) {
|
|
||||||
return system_time_zone_cached;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto now = std::time(nullptr);
|
if (minutes_off != 0) {
|
||||||
const struct std::tm& local = *std::localtime(&now);
|
const auto the_time = std::time(nullptr);
|
||||||
|
const struct std::tm& local = *std::localtime(&the_time);
|
||||||
|
const bool is_dst = local.tm_isdst != 0;
|
||||||
|
|
||||||
const s64 system_offset = GetCurrentOffsetSeconds().count() - (local.tm_isdst ? 3600 : 0);
|
const s64 tz_index = (hours * 100 + minutes_off) * (is_dst ? 100 : 1);
|
||||||
|
|
||||||
int min = std::numeric_limits<int>::max();
|
try {
|
||||||
int min_index = -1;
|
return off_timezones.at(tz_index);
|
||||||
for (u32 i = 2; i < offsets.size(); i++) {
|
} catch (std::out_of_range&) {
|
||||||
// Skip if system is celebrating DST but considered time zone does not
|
LOG_ERROR(Common, "Time zone {} not handled, defaulting to hour offset.", tz_index);
|
||||||
if (local.tm_isdst && !dst[i]) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto offset = offsets[i];
|
|
||||||
const int difference = static_cast<int>(std::abs(offset - system_offset));
|
|
||||||
if (difference < min) {
|
|
||||||
min = difference;
|
|
||||||
min_index = i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours));
|
||||||
system_time_zone_cached = GetTimeZoneStrings()[min_index];
|
|
||||||
return system_time_zone_cached;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue