mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #1397 from hugodearaujo/fix/rtc-parse-rfc3339-hardware
fix(rtc): match sceRtcParseRFC3339 to the console
This commit is contained in:
@@ -198,6 +198,37 @@ RtcDateTime makeDateTime(int year, int month, int day, int hour, int minute, int
|
||||
static_cast<std::uint16_t>(hour), static_cast<std::uint16_t>(minute), static_cast<std::uint16_t>(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<std::uint64_t>(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<std::uint32_t>(*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<std::uint32_t>(*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<std::uint16_t>(year), static_cast<std::uint16_t>(month), static_cast<std::uint16_t>(day),
|
||||
static_cast<std::uint16_t>(hour), static_cast<std::uint16_t>(minute), static_cast<std::uint16_t>(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) {
|
||||
|
||||
@@ -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<std::uint64_t>(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<int>(0x80B5000D));
|
||||
Require(sceRtcParseRFC3339(&tick, "0000-13-40T99:99:99Z") == static_cast<int>(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);
|
||||
|
||||
Reference in New Issue
Block a user