mirror of
https://github.com/yuzu-emu/yuzu.git
synced 2024-11-11 20:44:57 +00:00
fe97d34156
We can simplify this function down into a single line with the use of fmt. A benefit with the fmt approach is that the fmt variant of localtime is thread-safe as well, making GetOsTimeZoneOffset() thread-safe as well.
45 lines
1.2 KiB
C++
45 lines
1.2 KiB
C++
// Copyright 2020 yuzu Emulator Project
|
|
// Licensed under GPLv2 or any later version
|
|
// Refer to the license.txt file included.
|
|
|
|
#include <chrono>
|
|
#include <ctime>
|
|
|
|
#include <fmt/chrono.h>
|
|
|
|
#include "common/logging/log.h"
|
|
#include "common/time_zone.h"
|
|
|
|
namespace Common::TimeZone {
|
|
|
|
std::string GetDefaultTimeZone() {
|
|
return "GMT";
|
|
}
|
|
|
|
static std::string GetOsTimeZoneOffset() {
|
|
// Get the current timezone offset, e.g. "-400", as a string
|
|
return fmt::format("%z", fmt::localtime(std::time(nullptr)));
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
std::chrono::seconds GetCurrentOffsetSeconds() {
|
|
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
|
|
|
|
return std::chrono::seconds{seconds};
|
|
}
|
|
|
|
} // namespace Common::TimeZone
|