From e2a85a3119d14fe28a1ef67c9cbdd03b4d8cd97d Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 2 Jan 2026 20:12:00 -0800 Subject: [PATCH] Add fix for > 3 digits in milliseconds. --- Modules/RSParser/Sources/ObjC/RSDateParser.m | 4 +++- .../Tests/RSParserTests/RSDateParserTests.swift | 13 +++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Modules/RSParser/Sources/ObjC/RSDateParser.m b/Modules/RSParser/Sources/ObjC/RSDateParser.m index cb9c572d3..4fe95e983 100755 --- a/Modules/RSParser/Sources/ObjC/RSDateParser.m +++ b/Modules/RSParser/Sources/ObjC/RSDateParser.m @@ -389,6 +389,9 @@ static NSDate *RSParseW3CWithBytes(const char *bytes, NSUInteger numberOfBytes) if (hasMilliseconds) { milliseconds = nextNumericValue(bytes, numberOfBytes, currentIndex, 3, &finalIndex); currentIndex = finalIndex + 1; + + // Ignore more than 3 digits for fraction of a second + while (currentIndex < numberOfBytes && isdigit(bytes[currentIndex])) currentIndex++; } timeZoneOffset = parsedTimeZoneOffset(bytes, numberOfBytes, currentIndex); @@ -458,4 +461,3 @@ NSDate *RSDateWithString(NSString *dateString) { const char *utf8String = [dateString UTF8String]; return RSDateWithBytes(utf8String, strlen(utf8String)); } - diff --git a/Modules/RSParser/Tests/RSParserTests/RSDateParserTests.swift b/Modules/RSParser/Tests/RSParserTests/RSDateParserTests.swift index e7ff4f2e9..26a805b86 100644 --- a/Modules/RSParser/Tests/RSParserTests/RSDateParserTests.swift +++ b/Modules/RSParser/Tests/RSParserTests/RSDateParserTests.swift @@ -11,7 +11,7 @@ import RSParser final class RSDateParserTests: XCTestCase { - static func dateWithValues(_ year: Int, _ month: Int, _ day: Int, _ hour: Int, _ minute: Int, _ second: Int) -> Date { + static func dateWithValues(_ year: Int, _ month: Int, _ day: Int, _ hour: Int, _ minute: Int, _ second: Int, _ milliseconds: Int = 0) -> Date { var dateComponents = DateComponents() dateComponents.calendar = Calendar.current dateComponents.timeZone = TimeZone(secondsFromGMT: 0) @@ -22,6 +22,7 @@ final class RSDateParserTests: XCTestCase { dateComponents.hour = hour dateComponents.minute = minute dateComponents.second = second + dateComponents.nanosecond = milliseconds * 1000000 return dateComponents.date! } @@ -100,9 +101,9 @@ final class RSDateParserTests: XCTestCase { XCTAssertEqual(d, expectedDateResult) } -// func testHighMillisecondDate() { -// let expectedDateResult = Self.dateWithValues(2021, 03, 29, 10, 46, 56) -// let d = RSDateWithString("2021-03-29T10:46:56.516941+00:00") -// XCTAssertEqual(d, expectedDateResult) -// } + func testHighMillisecondDate() { + let expectedDateResult = Self.dateWithValues(2021, 03, 29, 10, 46, 56, 516) + let d = RSDateWithString("2021-03-29T10:46:56.516941+00:00") + XCTAssertEqual(d!.timeIntervalSince1970, expectedDateResult.timeIntervalSince1970, accuracy: 0.000001) + } }