fixed: NSDateFormatter was unable to parse dates in Javascript Date.toISOString format

This commit is contained in:
daboe01 committed 2025-06-29 17:51:08 +02:00
1 parent b63cde48c6
commit 1fb42fc119
2 files changed
+42

No files matched your search

+12
View File
@@ -1291,6 +1291,18 @@ var _separatorsCharacterSet = nil;
if (!aString)
return [[CPDate alloc] initWithTimeIntervalSinceReferenceDate:-31622400];
// Special case for ISO8601 format, which is poorly handled by the generic parser.
if (aFormat === @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") {
var d = new Date(aString);
if (d && !isNaN(d.getTime())) {
// The native JS Date object handles ISO 8601 strings correctly (as UTC).
// We can create a CPDate directly from the resulting timestamp.
var kCFAbsoluteTimeIntervalSince1970 = 978307200.0;
var interval = (d.getTime() / 1000.0) - kCFAbsoluteTimeIntervalSince1970;
return [CPDate dateWithTimeIntervalSinceReferenceDate:interval];
}
}
if (aFormat == nil)
return nil;
+30
View File
@@ -1245,6 +1245,36 @@
[self assert:result equals:nil];
}
- (void)testDateFromStringISO8601
{
// This test verifies the fix for issue #2537.
// The parser should correctly handle the ISO 8601 format produced by Date.toISOString().
var dateFormatter = [[CPDateFormatter alloc] init];
[dateFormatter setLocale:[[CPLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
// Test with a fixed date string.
var fixedISOString = @"2017-03-31T16:40:15.768Z";
var parsedFixedDate = [dateFormatter dateFromString:fixedISOString];
var expectedFixedDate = [[CPDate alloc] initWithString:@"2017-03-31 16:40:15 +0000"];
[expectedFixedDate setMilliseconds:768];
[self assert:parsedFixedDate equals:expectedFixedDate];
// Test with a dynamically generated date string.
var liveJSDate = new Date();
var liveISOString = liveJSDate.toISOString();
var parsedLiveDate = [dateFormatter dateFromString:liveISOString];
// The fix converts from a JS Date, so we can compare time intervals for accuracy.
var liveJSDateTime = liveJSDate.getTime();
var parsedLiveDateTime = [parsedLiveDate timeIntervalSince1970] * 1000;
// We allow a small tolerance for floating point rounding.
[self assertTrue:Math.abs(liveJSDateTime - parsedLiveDateTime) < 1.0];
}
- (void)testGetObjectValueAcceptsNilErrorDescription
{
var date = nil,