time_zone.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <chrono>
  4. #include <exception>
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <stdexcept>
  8. #include <fmt/chrono.h>
  9. #include <fmt/core.h>
  10. #include "common/logging/log.h"
  11. #include "common/settings.h"
  12. #include "common/time_zone.h"
  13. namespace Common::TimeZone {
  14. // Time zone strings
  15. constexpr std::array timezones{
  16. "GMT", "GMT", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire",
  17. "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0",
  18. "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan",
  19. "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT",
  20. "Poland", "Portugal", "PRC", "PST8PDT", "ROC", "ROK", "Singapore", "Turkey",
  21. "UCT", "Universal", "UTC", "W-SU", "WET", "Zulu",
  22. };
  23. const std::array<const char*, 46>& GetTimeZoneStrings() {
  24. return timezones;
  25. }
  26. std::string GetDefaultTimeZone() {
  27. return "GMT";
  28. }
  29. static std::string GetOsTimeZoneOffset() {
  30. const std::time_t t{std::time(nullptr)};
  31. const std::tm tm{*std::localtime(&t)};
  32. return fmt::format("{:%z}", tm);
  33. }
  34. static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) {
  35. try {
  36. return std::stoi(timezone);
  37. } catch (const std::invalid_argument&) {
  38. LOG_CRITICAL(Common, "invalid_argument with {}!", timezone);
  39. return 0;
  40. } catch (const std::out_of_range&) {
  41. LOG_CRITICAL(Common, "out_of_range with {}!", timezone);
  42. return 0;
  43. }
  44. }
  45. std::chrono::seconds GetCurrentOffsetSeconds() {
  46. const int offset{ConvertOsTimeZoneOffsetToInt(GetOsTimeZoneOffset())};
  47. int seconds{(offset / 100) * 60 * 60}; // Convert hour component to seconds
  48. seconds += (offset % 100) * 60; // Convert minute component to seconds
  49. return std::chrono::seconds{seconds};
  50. }
  51. // Key is [Hours * 100 + Minutes], multiplied by 100 if DST
  52. const static std::map<s64, const char*> off_timezones = {
  53. {530, "Asia/Calcutta"}, {930, "Australia/Darwin"}, {845, "Australia/Eucla"},
  54. {103000, "Australia/Adelaide"}, {1030, "Australia/Lord_Howe"}, {630, "Indian/Cocos"},
  55. {1245, "Pacific/Chatham"}, {134500, "Pacific/Chatham"}, {-330, "Canada/Newfoundland"},
  56. {-23000, "Canada/Newfoundland"}, {430, "Asia/Kabul"}, {330, "Asia/Tehran"},
  57. {43000, "Asia/Tehran"}, {545, "Asia/Kathmandu"}, {-930, "Asia/Marquesas"},
  58. };
  59. std::string FindSystemTimeZone() {
  60. #if defined(MINGW)
  61. // MinGW has broken strftime -- https://sourceforge.net/p/mingw-w64/bugs/793/
  62. // e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400"
  63. return timezones[0];
  64. #else
  65. const s64 seconds = static_cast<s64>(GetCurrentOffsetSeconds().count());
  66. const s64 minutes = seconds / 60;
  67. const s64 hours = minutes / 60;
  68. const s64 minutes_off = minutes - hours * 60;
  69. if (minutes_off != 0) {
  70. const auto the_time = std::time(nullptr);
  71. const struct std::tm& local = *std::localtime(&the_time);
  72. const bool is_dst = local.tm_isdst != 0;
  73. const s64 tz_index = (hours * 100 + minutes_off) * (is_dst ? 100 : 1);
  74. try {
  75. return off_timezones.at(tz_index);
  76. } catch (std::out_of_range&) {
  77. LOG_ERROR(Common, "Time zone {} not handled, defaulting to hour offset.", tz_index);
  78. }
  79. }
  80. return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours));
  81. #endif
  82. }
  83. } // namespace Common::TimeZone