diff options
Diffstat (limited to 'src/common/time_zone.cpp')
| -rw-r--r-- | src/common/time_zone.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
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 @@ | |||
| 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 | std::chrono::seconds 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 std::chrono::seconds{seconds}; | ||
| 47 | } | ||
| 48 | |||
| 49 | } // namespace Common::TimeZone | ||