time_zone.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <chrono>
  5. #include <iomanip>
  6. #include <sstream>
  7. #include "common/logging/log.h"
  8. #include "common/time_zone.h"
  9. namespace Common::TimeZone {
  10. std::string GetDefaultTimeZone() {
  11. return "GMT";
  12. }
  13. static std::string GetOsTimeZoneOffset() {
  14. const std::time_t t{std::time(nullptr)};
  15. const std::tm tm{*std::localtime(&t)};
  16. std::stringstream ss;
  17. ss << std::put_time(&tm, "%z"); // Get the current timezone offset, e.g. "-400", as a string
  18. return ss.str();
  19. }
  20. static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) {
  21. try {
  22. return std::stoi(timezone);
  23. } catch (const std::invalid_argument&) {
  24. LOG_CRITICAL(Common, "invalid_argument with {}!", timezone);
  25. return 0;
  26. } catch (const std::out_of_range&) {
  27. LOG_CRITICAL(Common, "out_of_range with {}!", timezone);
  28. return 0;
  29. }
  30. }
  31. std::chrono::seconds GetCurrentOffsetSeconds() {
  32. const int offset{ConvertOsTimeZoneOffsetToInt(GetOsTimeZoneOffset())};
  33. int seconds{(offset / 100) * 60 * 60}; // Convert hour component to seconds
  34. seconds += (offset % 100) * 60; // Convert minute component to seconds
  35. return std::chrono::seconds{seconds};
  36. }
  37. } // namespace Common::TimeZone