2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-05-11 23:51:26 +02:00
|
|
|
|
|
|
|
#include <chrono>
|
2023-06-03 23:06:43 +02:00
|
|
|
#include <exception>
|
2020-08-20 00:27:31 +02:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2023-06-03 23:06:43 +02:00
|
|
|
#include <stdexcept>
|
2023-05-24 00:32:28 +02:00
|
|
|
#include <fmt/chrono.h>
|
|
|
|
#include <fmt/core.h>
|
2020-05-11 23:51:26 +02:00
|
|
|
|
|
|
|
#include "common/logging/log.h"
|
2023-05-24 00:32:28 +02:00
|
|
|
#include "common/settings.h"
|
2020-05-11 23:51:26 +02:00
|
|
|
#include "common/time_zone.h"
|
|
|
|
|
|
|
|
namespace Common::TimeZone {
|
|
|
|
|
2023-05-24 00:32:28 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-05-11 23:51:26 +02:00
|
|
|
std::string GetDefaultTimeZone() {
|
|
|
|
return "GMT";
|
|
|
|
}
|
|
|
|
|
2023-07-10 23:51:34 +02:00
|
|
|
// Results are not comparable to seconds since Epoch
|
|
|
|
static std::time_t TmSpecToSeconds(const struct std::tm& spec) {
|
2023-07-12 08:34:02 +02:00
|
|
|
const int year = spec.tm_year - 1; // Years up to now
|
|
|
|
const int leap_years = year / 4 - year / 100;
|
2023-07-10 23:51:34 +02:00
|
|
|
std::time_t cumulative = spec.tm_year;
|
2023-07-12 08:34:02 +02:00
|
|
|
cumulative = cumulative * 365 + leap_years + spec.tm_yday; // Years to days
|
|
|
|
cumulative = cumulative * 24 + spec.tm_hour; // Days to hours
|
|
|
|
cumulative = cumulative * 60 + spec.tm_min; // Hours to minutes
|
|
|
|
cumulative = cumulative * 60 + spec.tm_sec; // Minutes to seconds
|
2023-07-10 23:51:34 +02:00
|
|
|
return cumulative;
|
2020-05-11 23:51:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 00:44:07 +02:00
|
|
|
std::chrono::seconds GetCurrentOffsetSeconds() {
|
2023-07-10 23:51:34 +02:00
|
|
|
const std::time_t t{std::time(nullptr)};
|
|
|
|
const std::tm local{*std::localtime(&t)};
|
|
|
|
const std::tm gmt{*std::gmtime(&t)};
|
2020-05-11 23:51:26 +02:00
|
|
|
|
2023-07-10 23:51:34 +02:00
|
|
|
// gmt_seconds is a different offset than time(nullptr)
|
|
|
|
const auto gmt_seconds = TmSpecToSeconds(gmt);
|
|
|
|
const auto local_seconds = TmSpecToSeconds(local);
|
|
|
|
const auto seconds_offset = gmt_seconds - local_seconds;
|
2020-05-11 23:51:26 +02:00
|
|
|
|
2023-07-10 23:51:34 +02:00
|
|
|
return std::chrono::seconds{seconds_offset};
|
2020-05-11 23:51:26 +02:00
|
|
|
}
|
|
|
|
|
2023-06-03 23:06:43 +02:00
|
|
|
// 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"},
|
|
|
|
};
|
|
|
|
|
2023-05-24 00:32:28 +02:00
|
|
|
std::string FindSystemTimeZone() {
|
2023-06-03 23:06:43 +02:00
|
|
|
const s64 seconds = static_cast<s64>(GetCurrentOffsetSeconds().count());
|
2023-05-24 00:32:28 +02:00
|
|
|
|
2023-06-03 23:06:43 +02:00
|
|
|
const s64 minutes = seconds / 60;
|
|
|
|
const s64 hours = minutes / 60;
|
2023-05-24 00:32:28 +02:00
|
|
|
|
2023-06-03 23:06:43 +02:00
|
|
|
const s64 minutes_off = minutes - hours * 60;
|
2023-05-24 00:32:28 +02:00
|
|
|
|
2023-06-03 23:06:43 +02:00
|
|
|
if (minutes_off != 0) {
|
|
|
|
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 tz_index = (hours * 100 + minutes_off) * (is_dst ? 100 : 1);
|
2023-05-24 00:32:28 +02:00
|
|
|
|
2023-06-03 23:06:43 +02:00
|
|
|
try {
|
|
|
|
return off_timezones.at(tz_index);
|
|
|
|
} catch (std::out_of_range&) {
|
|
|
|
LOG_ERROR(Common, "Time zone {} not handled, defaulting to hour offset.", tz_index);
|
2023-05-24 00:32:28 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-03 23:06:43 +02:00
|
|
|
return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours));
|
2023-05-24 00:32:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 23:51:26 +02:00
|
|
|
} // namespace Common::TimeZone
|