Skip to content

Commit 3a23d83

Browse files
authored
Merge pull request #1913 from spevans/pr_sr_9322_42
[4.2] SR-9322: DateFormatter difference between macOS and Linux
2 parents 0b1fd4c + 0db955b commit 3a23d83

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

Foundation/DateFormatter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ open class DateFormatter : Formatter {
6262
}
6363
return res._swiftObject
6464
}
65+
66+
// range.length is updated with the last position of the input string that was parsed
67+
guard range.length == string.length else {
68+
// The whole string was not parsed
69+
return nil
70+
}
6571
return date
6672
}
6773

TestFoundation/TestDateFormatter.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class TestDateFormatter: XCTestCase {
2222
("test_customDateFormat", test_customDateFormat),
2323
("test_setLocalizedDateFormatFromTemplate", test_setLocalizedDateFormatFromTemplate),
2424
("test_dateFormatString", test_dateFormatString),
25+
("test_dateFrom", test_dateFrom),
2526
]
2627
}
2728

@@ -330,4 +331,19 @@ class TestDateFormatter: XCTestCase {
330331
XCTAssertEqual(f.dateFormat, dateFormat)
331332
}
332333
}
334+
335+
func test_dateFrom() throws {
336+
let formatter = DateFormatter()
337+
formatter.timeZone = TimeZone(identifier: "UTC")
338+
formatter.dateFormat = "yyyy-MM-dd"
339+
340+
XCTAssertNil(formatter.date(from: "2018-03-09T10:25:16+01:00"))
341+
let d1 = try formatter.date(from: "2018-03-09").unwrapped()
342+
XCTAssertEqual(d1.description, "2018-03-09 00:00:00 +0000")
343+
344+
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
345+
XCTAssertNil(formatter.date(from: "2018-03-09"))
346+
let d2 = try formatter.date(from: "2018-03-09T10:25:16+01:00").unwrapped()
347+
XCTAssertEqual(d2.description, "2018-03-09 09:25:16 +0000")
348+
}
333349
}

TestFoundation/TestUtils.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,19 @@ func ensureFiles(_ fileNames: [String]) -> Bool {
4242
}
4343
return result
4444
}
45+
46+
47+
enum TestError: Error {
48+
case unexpectedNil
49+
}
50+
51+
extension Optional {
52+
func unwrapped(_ fn: String = #function, file: StaticString = #file, line: UInt = #line) throws -> Wrapped {
53+
if let x = self {
54+
return x
55+
} else {
56+
XCTFail("Tried to invoke .unwrapped() on nil in \(file):\(line):\(fn)")
57+
throw TestError.unexpectedNil
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)