2020-05-11 17:51:26 -04:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <chrono>
|
2020-08-15 12:52:35 -04:00
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include <fmt/chrono.h>
|
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-15 12:52:35 -04:00
|
|
|
// Get the current timezone offset, e.g. "-400", as a string
|
|
|
|
return fmt::format("%z", fmt::localtime(std::time(nullptr)));
|
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
|