From fcfe9b56a3347102a8c6db3e76fdd9a9c295a246 Mon Sep 17 00:00:00 2001 From: hugodearaujo Date: Wed, 7 Oct 2026 18:52:22 -0300 Subject: [PATCH] fix(rtc): match sceRtcParseRFC3339 to the console Measured on a PS5 (firmware 13.60) through dlopen of libSceRtc.sprx: an empty fraction is accepted, any digits past the sixth are dropped, text after the zone is ignored, the offset is applied without a range check, and a 60th second returns 0 with the tick only shifted by the offset. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceRtc/Export.cpp | 53 ++++++++++++++++++------------ core/libs/tests/GuestRtc.cpp | 39 +++++++++++++++------- 2 files changed, 60 insertions(+), 32 deletions(-) diff --git a/core/libs/prx/libSceRtc/Export.cpp b/core/libs/prx/libSceRtc/Export.cpp index a8986f228..e1e6656cd 100644 --- a/core/libs/prx/libSceRtc/Export.cpp +++ b/core/libs/prx/libSceRtc/Export.cpp @@ -198,6 +198,37 @@ RtcDateTime makeDateTime(int year, int month, int day, int hour, int minute, int static_cast(hour), static_cast(minute), static_cast(second), microsecond}; } +int storeParsedTick(RtcTick* utc, const RtcDateTime& time, std::int64_t offsetMinutes) { + if (validate(&time) == 0) utc->tick = toTick(time); + utc->tick -= static_cast(offsetMinutes * TICKS_PER_MINUTE); + return 0; +} + +int parseRfc3339(RtcTick* utc, const char* cursor) { + int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; + if (!parseDigits(cursor, 4, year) || *cursor++ != '-' || !parseDigits(cursor, 2, month) || *cursor++ != '-' || !parseDigits(cursor, 2, day)) return SCE_RTC_ERROR_BAD_PARSE; + if (*cursor != 'T' && *cursor != 't') return SCE_RTC_ERROR_BAD_PARSE; + ++cursor; + if (!parseDigits(cursor, 2, hour) || *cursor++ != ':' || !parseDigits(cursor, 2, minute) || *cursor++ != ':' || !parseDigits(cursor, 2, second)) return SCE_RTC_ERROR_BAD_PARSE; + std::uint32_t microsecond = 0; + if (*cursor == '.') { + ++cursor; + for (std::uint32_t scale = 100000; *cursor >= '0' && *cursor <= '9'; ++cursor, scale /= 10) microsecond += static_cast(*cursor - '0') * scale; + } + std::int64_t offsetMinutes = 0; + if (*cursor == '+' || *cursor == '-') { + const int sign = *cursor++ == '-' ? -1 : 1; + int offsetHours = 0, offsetMinute = 0; + if (!parseDigits(cursor, 2, offsetHours) || *cursor++ != ':' || !parseDigits(cursor, 2, offsetMinute)) return SCE_RTC_ERROR_BAD_PARSE; + offsetMinutes = sign * (offsetHours * 60 + offsetMinute); + } else if (*cursor != 'Z' && *cursor != 'z') { + return SCE_RTC_ERROR_BAD_PARSE; + } + const RtcDateTime time = makeDateTime(year, month, day, hour, minute, second, microsecond); + if (const int result = validate(&time); result != 0 && (result != SCE_RTC_ERROR_INVALID_SECOND || second != 60)) return result; + return storeParsedTick(utc, time, offsetMinutes); +} + bool parseIsoDateTime(const char* cursor, RtcDateTime& time, std::int64_t& offsetMinutes) { int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; if (!parseDigits(cursor, 4, year) || *cursor++ != '-' || !parseDigits(cursor, 2, month) || *cursor++ != '-' || !parseDigits(cursor, 2, day)) return false; @@ -434,27 +465,7 @@ int APS5_VABI sceRtcFormatRFC2822LocalTime(char* date_time, const RtcTick* utc) int APS5_VABI sceRtcParseRFC3339(RtcTick* utc, const char* date_time) { if (!utc || !date_time) return SCE_RTC_ERROR_INVALID_POINTER; - const char* cursor = date_time; - int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; - if (!parseDigits(cursor, 4, year) || *cursor++ != '-' || !parseDigits(cursor, 2, month) || *cursor++ != '-' || !parseDigits(cursor, 2, day)) return SCE_RTC_ERROR_BAD_PARSE; - if (*cursor != 'T' && *cursor != 't' && *cursor != ' ') return SCE_RTC_ERROR_BAD_PARSE; - ++cursor; - if (!parseDigits(cursor, 2, hour) || *cursor++ != ':' || !parseDigits(cursor, 2, minute) || *cursor++ != ':' || !parseDigits(cursor, 2, second)) return SCE_RTC_ERROR_BAD_PARSE; - std::uint32_t microsecond = 0; - if (*cursor == '.') { - ++cursor; - std::uint32_t scale = 100000; - if (*cursor < '0' || *cursor > '9') return SCE_RTC_ERROR_BAD_PARSE; - for (; *cursor >= '0' && *cursor <= '9'; ++cursor, scale /= 10) microsecond += static_cast(*cursor - '0') * scale; - } - std::int64_t offsetMinutes = 0; - if (!parseZoneSuffix(cursor, offsetMinutes)) return SCE_RTC_ERROR_BAD_PARSE; - if (*cursor != 0) return SCE_RTC_ERROR_BAD_PARSE; - const RtcDateTime time{static_cast(year), static_cast(month), static_cast(day), - static_cast(hour), static_cast(minute), static_cast(second), microsecond}; - if (const int result = validate(&time); result != 0) return result; - const RtcTick local{toTick(time)}; - return addTicks(utc, &local, -offsetMinutes, TICKS_PER_MINUTE); + return parseRfc3339(utc, date_time); } int APS5_VABI sceRtcParseDateTime(RtcTick* utc, const char* date_time) { diff --git a/core/libs/tests/GuestRtc.cpp b/core/libs/tests/GuestRtc.cpp index 576dacbb8..aeba3a722 100644 --- a/core/libs/tests/GuestRtc.cpp +++ b/core/libs/tests/GuestRtc.cpp @@ -157,22 +157,39 @@ int main() { Require(sceRtcParseRFC3339(&tick, "1970-01-01T00:00:00Z") == 0 && tick.tick == unixEpochTick); Require(sceRtcParseRFC3339(&tick, "2023-02-29T00:00:00Z") == invalidDay); Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:56") == badParse); - Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:56Zjunk") == badParse); - const char* invalidOffsets[] = { - "2024-02-29T12:34:56.789+00:99", - "2024-02-29T12:34:56.789-00:99", - "2024-02-29T12:34:56.789+00:60", - "2024-02-29T12:34:56.789-00:60", - "2024-02-29T12:34:56.789+24:00", - "2024-02-29T12:34:56.789-24:00", - "2024-02-29T12:34:56.789+99:59", - "2024-02-29T12:34:56.789-99:59", + const std::uint64_t secondTick = leapDayTick - 789000ull; + Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:56Zjunk") == 0 && tick.tick == secondTick); + Require(sceRtcParseRFC3339(&tick, "2024-02-29T14:34:56+02:00 trailing") == 0 && tick.tick == secondTick); + Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:56.Z") == 0 && tick.tick == secondTick); + Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:56.1234567Z") == 0 && tick.tick == secondTick + 123456ull); + const struct { + const char* text; + std::int64_t minutes; + } unrangedOffsets[] = { + {"2024-02-29T12:34:56.789+00:99", 99}, + {"2024-02-29T12:34:56.789-00:99", -99}, + {"2024-02-29T12:34:56.789+00:60", 60}, + {"2024-02-29T12:34:56.789-00:60", -60}, + {"2024-02-29T12:34:56.789+24:00", 1440}, + {"2024-02-29T12:34:56.789-24:00", -1440}, + {"2024-02-29T12:34:56.789+99:59", 5999}, + {"2024-02-29T12:34:56.789-99:99", -6039}, }; - for (const char* text : invalidOffsets) { + for (const auto& offset : unrangedOffsets) { + Require(sceRtcParseRFC3339(&tick, offset.text) == 0); + Require(tick.tick == leapDayTick - static_cast(offset.minutes * 60000000)); + } + for (const char* text : {"2024-02-29 12:34:56Z", "2024-02-29T12:34:56+0100", "2024-02-29T12:34:56+01", "2024-02-29T12:34:56 Z", " 2024-02-29T12:34:56Z", "2024-02-29T12:34:56..Z", "2024-2-29T12:34:56Z"}) { tick.tick = 123; Require(sceRtcParseRFC3339(&tick, text) == badParse); Require(tick.tick == 123); } + tick.tick = 123; + Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:60Z") == 0 && tick.tick == 123); + Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:60+01:00") == 0 && tick.tick == 123ull - 3600000000ull); + Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:61Z") == static_cast(0x80B5000D)); + Require(sceRtcParseRFC3339(&tick, "0000-13-40T99:99:99Z") == static_cast(0x80B50008)); + Require(sceRtcParseRFC3339(&tick, "0001-01-01T00:00:00+01:00") == 0 && tick.tick == 0xFFFFFFFF296C5C00ull); Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:56.789+00:00") == 0 && tick.tick == leapDayTick); Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:56.789-00:00") == 0 && tick.tick == leapDayTick); Require(sceRtcParseRFC3339(&tick, "2024-02-29T12:34:56.789+00:59") == 0 && tick.tick == leapDayTick - 3540000000ull);