diff options
| author | 2020-05-11 17:51:26 -0400 | |
|---|---|---|
| committer | 2020-05-11 17:51:26 -0400 | |
| commit | 33441fa728363998410e5d7f128cd3d26e0bb512 (patch) | |
| tree | aacb925820b6af99f853a3bd2e70d9c433976071 | |
| parent | core: settings: Add a setting for time zone. (diff) | |
| download | yuzu-33441fa728363998410e5d7f128cd3d26e0bb512.tar.gz yuzu-33441fa728363998410e5d7f128cd3d26e0bb512.tar.xz yuzu-33441fa728363998410e5d7f128cd3d26e0bb512.zip | |
common: Add module to get the current time zone.
Diffstat (limited to '')
| -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 | 17 |
3 files changed, 68 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 | |||
| 148 | thread.h | 148 | thread.h |
| 149 | thread_queue_list.h | 149 | thread_queue_list.h |
| 150 | threadsafe_queue.h | 150 | threadsafe_queue.h |
| 151 | time_zone.cpp | ||
| 152 | time_zone.h | ||
| 151 | timer.cpp | 153 | timer.cpp |
| 152 | timer.h | 154 | timer.h |
| 153 | uint128.cpp | 155 | uint128.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 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <chrono> | ||
| 6 | #include <iomanip> | ||
| 7 | #include <sstream> | ||
| 8 | |||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "common/time_zone.h" | ||
| 11 | |||
| 12 | namespace Common::TimeZone { | ||
| 13 | |||
| 14 | std::string GetDefaultTimeZone() { | ||
| 15 | return "GMT"; | ||
| 16 | } | ||
| 17 | |||
| 18 | static std::string GetOsTimeZoneOffset() { | ||
| 19 | const std::time_t t{std::time(nullptr)}; | ||
| 20 | const std::tm tm{*std::localtime(&t)}; | ||
| 21 | |||
| 22 | std::stringstream ss; | ||
| 23 | ss << std::put_time(&tm, "%z"); // Get the current timezone offset, e.g. "-400", as a string | ||
| 24 | |||
| 25 | return ss.str(); | ||
| 26 | } | ||
| 27 | |||
| 28 | static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) { | ||
| 29 | try { | ||
| 30 | return std::stoi(timezone); | ||
| 31 | } catch (const std::invalid_argument&) { | ||
| 32 | LOG_CRITICAL(Common, "invalid_argument with {}!", timezone); | ||
| 33 | return 0; | ||
| 34 | } catch (const std::out_of_range&) { | ||
| 35 | LOG_CRITICAL(Common, "out_of_range with {}!", timezone); | ||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | int GetCurrentOffsetSeconds() { | ||
| 41 | const int offset{ConvertOsTimeZoneOffsetToInt(GetOsTimeZoneOffset())}; | ||
| 42 | |||
| 43 | int seconds{(offset / 100) * 60 * 60}; // Convert hour component to seconds | ||
| 44 | seconds += (offset % 100) * 60; // Convert minute component to seconds | ||
| 45 | |||
| 46 | return seconds; | ||
| 47 | } | ||
| 48 | |||
| 49 | } // namespace Common::TimeZone | ||
diff --git a/src/common/time_zone.h b/src/common/time_zone.h new file mode 100644 index 000000000..b7aa1bb10 --- /dev/null +++ b/src/common/time_zone.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <string> | ||
| 8 | |||
| 9 | namespace Common::TimeZone { | ||
| 10 | |||
| 11 | /// Gets the default timezone, i.e. "GMT" | ||
| 12 | std::string GetDefaultTimeZone(); | ||
| 13 | |||
| 14 | /// Gets the offset of the current timezone (from the default), in seconds | ||
| 15 | int GetCurrentOffsetSeconds(); | ||
| 16 | |||
| 17 | } // namespace Common::TimeZone | ||