From 33441fa728363998410e5d7f128cd3d26e0bb512 Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 11 May 2020 17:51:26 -0400 Subject: common: Add module to get the current time zone. --- src/common/time_zone.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/common/time_zone.cpp (limited to 'src/common/time_zone.cpp') diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp new file mode 100644 index 000000000..b902d2d47 --- /dev/null +++ b/src/common/time_zone.cpp @@ -0,0 +1,49 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include +#include + +#include "common/logging/log.h" +#include "common/time_zone.h" + +namespace Common::TimeZone { + +std::string GetDefaultTimeZone() { + return "GMT"; +} + +static std::string GetOsTimeZoneOffset() { + 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(); +} + +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; + } +} + +int 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 seconds; +} + +} // namespace Common::TimeZone -- cgit v1.2.3