From 05dbb47af51fb00826912155da85469cb74022db Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Fri, 28 Dec 2018 20:24:24 -0500
Subject: [PATCH] settings: Use std::chrono::seconds instead of s64 for RTC

---
 src/core/core.cpp                           |  3 +--
 src/core/hle/service/time/time.cpp          | 10 ++++------
 src/core/settings.h                         |  8 +++++---
 src/yuzu/configuration/config.cpp           |  6 ++++--
 src/yuzu/configuration/configure_system.cpp |  8 +++++---
 src/yuzu_cmd/config.cpp                     |  3 ++-
 6 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/src/core/core.cpp b/src/core/core.cpp
index 7459c0851e..123b114093 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -96,8 +96,7 @@ struct System::Impl {
         kernel.Initialize();
 
         const auto current_time = std::chrono::duration_cast<std::chrono::seconds>(
-                                      std::chrono::system_clock::now().time_since_epoch())
-                                      .count();
+            std::chrono::system_clock::now().time_since_epoch());
         Settings::values.custom_rtc_differential =
             Settings::values.custom_rtc.value_or(current_time) - current_time;
 
diff --git a/src/core/hle/service/time/time.cpp b/src/core/hle/service/time/time.cpp
index ef8c9f2b77..c13640ad8f 100644
--- a/src/core/hle/service/time/time.cpp
+++ b/src/core/hle/service/time/time.cpp
@@ -16,10 +16,9 @@
 
 namespace Service::Time {
 
-static s64 GetSecondsSinceEpoch() {
+static std::chrono::seconds GetSecondsSinceEpoch() {
     return std::chrono::duration_cast<std::chrono::seconds>(
-               std::chrono::system_clock::now().time_since_epoch())
-               .count() +
+               std::chrono::system_clock::now().time_since_epoch()) +
            Settings::values.custom_rtc_differential;
 }
 
@@ -76,7 +75,7 @@ public:
 
 private:
     void GetCurrentTime(Kernel::HLERequestContext& ctx) {
-        const s64 time_since_epoch{GetSecondsSinceEpoch()};
+        const s64 time_since_epoch{GetSecondsSinceEpoch().count()};
         LOG_DEBUG(Service_Time, "called");
 
         IPC::ResponseBuilder rb{ctx, 4};
@@ -272,8 +271,7 @@ void Module::Interface::GetClockSnapshot(Kernel::HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto initial_type = rp.PopRaw<u8>();
 
-    const s64 time_since_epoch{GetSecondsSinceEpoch()};
-
+    const s64 time_since_epoch{GetSecondsSinceEpoch().count()};
     const std::time_t time(time_since_epoch);
     const std::tm* tm = std::localtime(&time);
     if (tm == nullptr) {
diff --git a/src/core/settings.h b/src/core/settings.h
index 5b211a7162..bb5aafa0c3 100644
--- a/src/core/settings.h
+++ b/src/core/settings.h
@@ -6,6 +6,7 @@
 
 #include <array>
 #include <atomic>
+#include <chrono>
 #include <map>
 #include <optional>
 #include <string>
@@ -350,9 +351,10 @@ struct Values {
     bool use_docked_mode;
     bool enable_nfc;
     std::optional<u32> rng_seed;
-    std::optional<s64> custom_rtc; // Measured in seconds since epoch
-    s64 custom_rtc_differential;   // Set on game boot, reset on stop. Seconds difference between
-                                   // current time and `custom_rtc`
+    std::optional<std::chrono::seconds> custom_rtc; // Measured in seconds since epoch
+    std::chrono::seconds
+        custom_rtc_differential; // Set on game boot, reset on stop. Seconds difference between
+                                 // current time and `custom_rtc`
     s32 current_user;
     s32 language_index;
 
diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp
index 6e034ef190..6c5284db5f 100644
--- a/src/yuzu/configuration/config.cpp
+++ b/src/yuzu/configuration/config.cpp
@@ -428,7 +428,8 @@ void Config::ReadValues() {
 
     const auto custom_rtc_enabled = qt_config->value("custom_rtc_enabled", false).toBool();
     if (custom_rtc_enabled) {
-        Settings::values.custom_rtc = qt_config->value("custom_rtc", 0).toULongLong();
+        Settings::values.custom_rtc =
+            std::chrono::seconds(qt_config->value("custom_rtc", 0).toULongLong());
     } else {
         Settings::values.custom_rtc = std::nullopt;
     }
@@ -661,7 +662,8 @@ void Config::SaveValues() {
     qt_config->setValue("rng_seed", Settings::values.rng_seed.value_or(0));
 
     qt_config->setValue("custom_rtc_enabled", Settings::values.custom_rtc.has_value());
-    qt_config->setValue("custom_rtc", Settings::values.custom_rtc.value_or(0));
+    qt_config->setValue("custom_rtc",
+                        Settings::values.custom_rtc.value_or(std::chrono::seconds{}).count());
 
     qt_config->endGroup();
 
diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp
index 753db75d22..94e27349da 100644
--- a/src/yuzu/configuration/configure_system.cpp
+++ b/src/yuzu/configuration/configure_system.cpp
@@ -77,8 +77,9 @@ void ConfigureSystem::setConfiguration() {
     ui->custom_rtc_checkbox->setChecked(Settings::values.custom_rtc.has_value());
     ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.has_value());
 
-    const auto rtc_time = Settings::values.custom_rtc.value_or(QDateTime::currentSecsSinceEpoch());
-    ui->custom_rtc_edit->setDateTime(QDateTime::fromSecsSinceEpoch(rtc_time));
+    const auto rtc_time = Settings::values.custom_rtc.value_or(
+        std::chrono::seconds(QDateTime::currentSecsSinceEpoch()));
+    ui->custom_rtc_edit->setDateTime(QDateTime::fromSecsSinceEpoch(rtc_time.count()));
 }
 
 void ConfigureSystem::ReadSystemSettings() {}
@@ -95,7 +96,8 @@ void ConfigureSystem::applyConfiguration() {
         Settings::values.rng_seed = std::nullopt;
 
     if (ui->custom_rtc_checkbox->isChecked())
-        Settings::values.custom_rtc = ui->custom_rtc_edit->dateTime().toSecsSinceEpoch();
+        Settings::values.custom_rtc =
+            std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch());
     else
         Settings::values.custom_rtc = std::nullopt;
 
diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp
index 8f3b74cdf4..7a77f76e83 100644
--- a/src/yuzu_cmd/config.cpp
+++ b/src/yuzu_cmd/config.cpp
@@ -334,7 +334,8 @@ void Config::ReadValues() {
 
     const auto custom_rtc_enabled = sdl2_config->GetBoolean("System", "custom_rtc_enabled", false);
     if (custom_rtc_enabled) {
-        Settings::values.custom_rtc = sdl2_config->GetInteger("System", "custom_rtc", 0);
+        Settings::values.custom_rtc =
+            std::chrono::seconds(sdl2_config->GetInteger("System", "custom_rtc", 0));
     } else {
         Settings::values.custom_rtc = std::nullopt;
     }