time_manager.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2019 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <chrono>
  5. #include <ctime>
  6. #include "common/time_zone.h"
  7. #include "core/hle/service/time/ephemeral_network_system_clock_context_writer.h"
  8. #include "core/hle/service/time/local_system_clock_context_writer.h"
  9. #include "core/hle/service/time/network_system_clock_context_writer.h"
  10. #include "core/hle/service/time/time_manager.h"
  11. #include "core/settings.h"
  12. namespace Service::Time {
  13. constexpr Clock::TimeSpanType standard_network_clock_accuracy{0x0009356907420000ULL};
  14. static std::chrono::seconds GetSecondsSinceEpoch() {
  15. return std::chrono::duration_cast<std::chrono::seconds>(
  16. std::chrono::system_clock::now().time_since_epoch()) +
  17. Settings::values.custom_rtc_differential;
  18. }
  19. static s64 GetExternalTimeZoneOffset() {
  20. // With "auto" timezone setting, we use the external system's timezone offset
  21. if (Settings::GetTimeZoneString() == "auto") {
  22. return Common::TimeZone::GetCurrentOffsetSeconds().count();
  23. }
  24. return 0;
  25. }
  26. static s64 GetExternalRtcValue() {
  27. return GetSecondsSinceEpoch().count() + GetExternalTimeZoneOffset();
  28. }
  29. TimeManager::TimeManager(Core::System& system)
  30. : shared_memory{system}, standard_local_system_clock_core{standard_steady_clock_core},
  31. standard_network_system_clock_core{standard_steady_clock_core},
  32. standard_user_system_clock_core{standard_local_system_clock_core,
  33. standard_network_system_clock_core, system},
  34. ephemeral_network_system_clock_core{tick_based_steady_clock_core},
  35. local_system_clock_context_writer{
  36. std::make_shared<Clock::LocalSystemClockContextWriter>(shared_memory)},
  37. network_system_clock_context_writer{
  38. std::make_shared<Clock::NetworkSystemClockContextWriter>(shared_memory)},
  39. ephemeral_network_system_clock_context_writer{
  40. std::make_shared<Clock::EphemeralNetworkSystemClockContextWriter>()},
  41. time_zone_content_manager{*this, system} {
  42. const auto system_time{Clock::TimeSpanType::FromSeconds(GetExternalRtcValue())};
  43. SetupStandardSteadyClock(system, Common::UUID::Generate(), system_time, {}, {});
  44. SetupStandardLocalSystemClock(system, {}, system_time.ToSeconds());
  45. SetupStandardNetworkSystemClock({}, standard_network_clock_accuracy);
  46. SetupStandardUserSystemClock(system, {}, Clock::SteadyClockTimePoint::GetRandom());
  47. SetupEphemeralNetworkSystemClock();
  48. }
  49. TimeManager::~TimeManager() = default;
  50. void TimeManager::SetupTimeZoneManager(std::string location_name,
  51. Clock::SteadyClockTimePoint time_zone_updated_time_point,
  52. std::size_t total_location_name_count,
  53. u128 time_zone_rule_version,
  54. FileSys::VirtualFile& vfs_file) {
  55. if (time_zone_content_manager.GetTimeZoneManager().SetDeviceLocationNameWithTimeZoneRule(
  56. location_name, vfs_file) != RESULT_SUCCESS) {
  57. UNREACHABLE();
  58. return;
  59. }
  60. time_zone_content_manager.GetTimeZoneManager().SetUpdatedTime(time_zone_updated_time_point);
  61. time_zone_content_manager.GetTimeZoneManager().SetTotalLocationNameCount(
  62. total_location_name_count);
  63. time_zone_content_manager.GetTimeZoneManager().SetTimeZoneRuleVersion(time_zone_rule_version);
  64. time_zone_content_manager.GetTimeZoneManager().MarkAsInitialized();
  65. }
  66. void TimeManager::SetupStandardSteadyClock(Core::System& system, Common::UUID clock_source_id,
  67. Clock::TimeSpanType setup_value,
  68. Clock::TimeSpanType internal_offset,
  69. bool is_rtc_reset_detected) {
  70. standard_steady_clock_core.SetClockSourceId(clock_source_id);
  71. standard_steady_clock_core.SetSetupValue(setup_value);
  72. standard_steady_clock_core.SetInternalOffset(internal_offset);
  73. standard_steady_clock_core.MarkAsInitialized();
  74. const auto current_time_point{standard_steady_clock_core.GetCurrentRawTimePoint(system)};
  75. shared_memory.SetupStandardSteadyClock(system, clock_source_id, current_time_point);
  76. }
  77. void TimeManager::SetupStandardLocalSystemClock(Core::System& system,
  78. Clock::SystemClockContext clock_context,
  79. s64 posix_time) {
  80. standard_local_system_clock_core.SetUpdateCallbackInstance(local_system_clock_context_writer);
  81. const auto current_time_point{
  82. standard_local_system_clock_core.GetSteadyClockCore().GetCurrentTimePoint(system)};
  83. if (current_time_point.clock_source_id == clock_context.steady_time_point.clock_source_id) {
  84. standard_local_system_clock_core.SetSystemClockContext(clock_context);
  85. } else {
  86. if (standard_local_system_clock_core.SetCurrentTime(system, posix_time) != RESULT_SUCCESS) {
  87. UNREACHABLE();
  88. return;
  89. }
  90. }
  91. standard_local_system_clock_core.MarkAsInitialized();
  92. }
  93. void TimeManager::SetupStandardNetworkSystemClock(Clock::SystemClockContext clock_context,
  94. Clock::TimeSpanType sufficient_accuracy) {
  95. standard_network_system_clock_core.SetUpdateCallbackInstance(
  96. network_system_clock_context_writer);
  97. if (standard_network_system_clock_core.SetSystemClockContext(clock_context) != RESULT_SUCCESS) {
  98. UNREACHABLE();
  99. return;
  100. }
  101. standard_network_system_clock_core.SetStandardNetworkClockSufficientAccuracy(
  102. sufficient_accuracy);
  103. standard_network_system_clock_core.MarkAsInitialized();
  104. }
  105. void TimeManager::SetupStandardUserSystemClock(
  106. Core::System& system, bool is_automatic_correction_enabled,
  107. Clock::SteadyClockTimePoint steady_clock_time_point) {
  108. if (standard_user_system_clock_core.SetAutomaticCorrectionEnabled(
  109. system, is_automatic_correction_enabled) != RESULT_SUCCESS) {
  110. UNREACHABLE();
  111. return;
  112. }
  113. standard_user_system_clock_core.SetAutomaticCorrectionUpdatedTime(steady_clock_time_point);
  114. standard_user_system_clock_core.MarkAsInitialized();
  115. shared_memory.SetAutomaticCorrectionEnabled(is_automatic_correction_enabled);
  116. }
  117. void TimeManager::SetupEphemeralNetworkSystemClock() {
  118. ephemeral_network_system_clock_core.SetUpdateCallbackInstance(
  119. ephemeral_network_system_clock_context_writer);
  120. ephemeral_network_system_clock_core.MarkAsInitialized();
  121. }
  122. } // namespace Service::Time