time_zone.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours));
  75. }
  76. } // namespace Common::TimeZone