2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-05-11 17:51:26 -04:00
|
|
|
|
|
|
|
#include <chrono>
|
2020-08-19 18:27:31 -04:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2020-05-11 17:51:26 -04:00
|
|
|
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/time_zone.h"
|
|
|
|
|
|
|
|
namespace Common::TimeZone {
|
|
|
|
|
|
|
|
std::string GetDefaultTimeZone() {
|
|
|
|
return "GMT";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string GetOsTimeZoneOffset() {
|
2020-08-19 18:27:31 -04:00
|
|
|
const std::time_t t{std::time(nullptr)};
|
|
|
|
const std::tm tm{*std::localtime(&t)};
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::put_time(&tm, "%z"); // Get the current timezone offset, e.g. "-400", as a string
|
|
|
|
|
|
|
|
return ss.str();
|
2020-05-11 17:51:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) {
|
|
|
|
try {
|
|
|
|
return std::stoi(timezone);
|
|
|
|
} catch (const std::invalid_argument&) {
|
|
|
|
LOG_CRITICAL(Common, "invalid_argument with {}!", timezone);
|
|
|
|
return 0;
|
|
|
|
} catch (const std::out_of_range&) {
|
|
|
|
LOG_CRITICAL(Common, "out_of_range with {}!", timezone);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:44:07 -04:00
|
|
|
std::chrono::seconds GetCurrentOffsetSeconds() {
|
2020-05-11 17:51:26 -04:00
|
|
|
const int offset{ConvertOsTimeZoneOffsetToInt(GetOsTimeZoneOffset())};
|
|
|
|
|
|
|
|
int seconds{(offset / 100) * 60 * 60}; // Convert hour component to seconds
|
|
|
|
seconds += (offset % 100) * 60; // Convert minute component to seconds
|
|
|
|
|
2020-05-12 18:44:07 -04:00
|
|
|
return std::chrono::seconds{seconds};
|
2020-05-11 17:51:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Common::TimeZone
|