diff options
| author | bunnei <bunneidev@gmail.com> | 2020-05-13 21:41:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-13 21:41:45 -0400 |
| commit | 670a7f51e8f3134fb246a471f0c9833904a6234e (patch) | |
| tree | d20a6ccc6070f49aefd3069456545451a42e66cf /src/common | |
| parent | b1a1bd12cae560b39c718b7188db0d8eae975917 (diff) | |
| parent | bba54e1880bd70d634797052d78359e30ec79acd (diff) | |
Merge pull request #3909 from bunnei/timezone
Improve time zone support
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/common/time_zone.cpp | 49 | ||||
| -rw-r--r-- | src/common/time_zone.h | 18 |
3 files changed, 69 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d1ec8ff08..e6769a5f3 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -148,6 +148,8 @@ add_library(common STATIC thread.h thread_queue_list.h threadsafe_queue.h + time_zone.cpp + time_zone.h timer.cpp timer.h uint128.cpp diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp new file mode 100644 index 000000000..ce239eb63 --- /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 <chrono> +#include <iomanip> +#include <sstream> + +#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; + } +} + +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 diff --git a/src/common/time_zone.h b/src/common/time_zone.h new file mode 100644 index 000000000..945daa09c --- /dev/null +++ b/src/common/time_zone.h @@ -0,0 +1,18 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <chrono> +#include <string> + +namespace Common::TimeZone { + +/// Gets the default timezone, i.e. "GMT" +std::string GetDefaultTimeZone(); + +/// Gets the offset of the current timezone (from the default), in seconds +std::chrono::seconds GetCurrentOffsetSeconds(); + +} // namespace Common::TimeZone |
