time_zone.cpp 1.3 KB

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