time_zone.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <map>
  7. #include <sstream>
  8. #include <stdexcept>
  9. #include <fmt/chrono.h>
  10. #include <fmt/core.h>
  11. #include "common/logging/log.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. // Results are not comparable to seconds since Epoch
  30. static std::time_t TmSpecToSeconds(const struct std::tm& spec) {
  31. const int year = spec.tm_year - 1; // Years up to now
  32. const int leap_years = year / 4 - year / 100;
  33. std::time_t cumulative = spec.tm_year;
  34. cumulative = cumulative * 365 + leap_years + spec.tm_yday; // Years to days
  35. cumulative = cumulative * 24 + spec.tm_hour; // Days to hours
  36. cumulative = cumulative * 60 + spec.tm_min; // Hours to minutes
  37. cumulative = cumulative * 60 + spec.tm_sec; // Minutes to seconds
  38. return cumulative;
  39. }
  40. std::chrono::seconds GetCurrentOffsetSeconds() {
  41. const std::time_t t{std::time(nullptr)};
  42. const std::tm local{*std::localtime(&t)};
  43. const std::tm gmt{*std::gmtime(&t)};
  44. // gmt_seconds is a different offset than time(nullptr)
  45. const auto gmt_seconds = TmSpecToSeconds(gmt);
  46. const auto local_seconds = TmSpecToSeconds(local);
  47. const auto seconds_offset = local_seconds - gmt_seconds;
  48. return std::chrono::seconds{seconds_offset};
  49. }
  50. // Key is [Hours * 100 + Minutes], multiplied by 100 if DST
  51. const static std::map<s64, const char*> off_timezones = {
  52. {530, "Asia/Calcutta"}, {930, "Australia/Darwin"}, {845, "Australia/Eucla"},
  53. {103000, "Australia/Adelaide"}, {1030, "Australia/Lord_Howe"}, {630, "Indian/Cocos"},
  54. {1245, "Pacific/Chatham"}, {134500, "Pacific/Chatham"}, {-330, "Canada/Newfoundland"},
  55. {-23000, "Canada/Newfoundland"}, {430, "Asia/Kabul"}, {330, "Asia/Tehran"},
  56. {43000, "Asia/Tehran"}, {545, "Asia/Kathmandu"}, {-930, "Asia/Marquesas"},
  57. };
  58. std::string FindSystemTimeZone() {
  59. const s64 seconds = static_cast<s64>(GetCurrentOffsetSeconds().count());
  60. const s64 minutes = seconds / 60;
  61. const s64 hours = minutes / 60;
  62. const s64 minutes_off = minutes - hours * 60;
  63. if (minutes_off != 0) {
  64. const auto the_time = std::time(nullptr);
  65. const struct std::tm& local = *std::localtime(&the_time);
  66. const bool is_dst = local.tm_isdst != 0;
  67. const s64 tz_index = (hours * 100 + minutes_off) * (is_dst ? 100 : 1);
  68. try {
  69. return off_timezones.at(tz_index);
  70. } catch (std::out_of_range&) {
  71. LOG_ERROR(Common, "Time zone {} not handled, defaulting to hour offset.", tz_index);
  72. }
  73. }
  74. // For some reason the Etc/GMT times are reversed. GMT+6 contains -21600 as its offset,
  75. // -6 hours instead of +6 hours, so these signs are purposefully reversed to fix it.
  76. std::string postfix{""};
  77. if (hours > 0) {
  78. postfix = fmt::format("-{:d}", std::abs(hours));
  79. } else if (hours < 0) {
  80. postfix = fmt::format("+{:d}", std::abs(hours));
  81. }
  82. return fmt::format("Etc/GMT{:s}", postfix);
  83. }
  84. } // namespace Common::TimeZone