From 0b6bf01b36f49ab9c3e0cb5b16baa16fe3e108ce Mon Sep 17 00:00:00 2001 From: Stefanos Costa Date: Sat, 1 Aug 2026 22:59:51 +0300 Subject: [PATCH] kernel: preserve microsecond wall-clock resolution Extracted from 3db2b3c5c5e1a26a861df7ebcacd9ccb8c484420 in KytyPS5/KytyPS5#147. --- src/kernel/pthread.cpp | 8 ++++++-- src/libs/libRtc.cpp | 21 +++++++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/kernel/pthread.cpp b/src/kernel/pthread.cpp index 6b1c342..47c0008 100644 --- a/src/kernel/pthread.cpp +++ b/src/kernel/pthread.cpp @@ -3886,8 +3886,12 @@ int KYTY_SYSV_ABI KernelGettimeofday(KernelTimeval* tp) { tp->tv_sec = static_cast(ticks / 1000000); tp->tv_usec = static_cast(ticks % 1000000); #else - auto dt = Common::DateTime::FromSystemUTC(); - sec_to_timeval(tp, dt.ToUnix()); + struct timespec ts {}; + result = ::clock_gettime(CLOCK_REALTIME, &ts); + if (result == 0) { + tp->tv_sec = static_cast(ts.tv_sec); + tp->tv_usec = static_cast(ts.tv_nsec / 1000); + } #endif if (result == 0) { diff --git a/src/libs/libRtc.cpp b/src/libs/libRtc.cpp index dc55f66..fda2623 100644 --- a/src/libs/libRtc.cpp +++ b/src/libs/libRtc.cpp @@ -4,6 +4,7 @@ #include "libs/libs.h" #include "loader/symbolDatabase.h" +#include #include #include #include @@ -247,20 +248,12 @@ static int KYTY_SYSV_ABI RtcGetCurrentTick(RtcTick* tick) { return RTC_ERROR_DATETIME_UNINITIALIZED; } - const auto now = Common::DateTime::FromSystemUTC(); - const auto date = now.GetDate(); - const auto tod = now.GetTime(); - - RtcDateTime time {}; - time.year = static_cast(date.Year()); - time.month = static_cast(date.Month()); - time.day = static_cast(date.Day()); - time.hour = static_cast(tod.Hour24()); - time.minute = static_cast(tod.Minute()); - time.second = static_cast(tod.Second()); - time.microsecond = static_cast(tod.Msec() * 1000); - - return RtcGetTick(&time, tick); + const auto now_us = + static_cast(std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count()); + tick->tick = RTC_UNIX_EPOCH_TICKS + now_us; + return OK; } static int KYTY_SYSV_ABI RtcGetCurrentNetworkTick(RtcTick* tick) {