File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,12 @@ open class DateFormatter : Formatter {
62
62
}
63
63
return res. _swiftObject
64
64
}
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
+ }
65
71
return date
66
72
}
67
73
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ class TestDateFormatter: XCTestCase {
26
26
( " test_setTimeZoneToNil " , test_setTimeZoneToNil) ,
27
27
( " test_setTimeZone " , test_setTimeZone) ,
28
28
( " test_expectedTimeZone " , test_expectedTimeZone) ,
29
+ ( " test_dateFrom " , test_dateFrom) ,
29
30
]
30
31
}
31
32
@@ -409,4 +410,19 @@ class TestDateFormatter: XCTestCase {
409
410
f. timeZone = losAngeles
410
411
XCTAssertEqual ( f. string ( from: now) , losAngeles. abbreviation ( ) )
411
412
}
413
+
414
+ func test_dateFrom( ) throws {
415
+ let formatter = DateFormatter ( )
416
+ formatter. timeZone = TimeZone ( identifier: " UTC " )
417
+ formatter. dateFormat = " yyyy-MM-dd "
418
+
419
+ XCTAssertNil ( formatter. date ( from: " 2018-03-09T10:25:16+01:00 " ) )
420
+ let d1 = try formatter. date ( from: " 2018-03-09 " ) . unwrapped ( )
421
+ XCTAssertEqual ( d1. description, " 2018-03-09 00:00:00 +0000 " )
422
+
423
+ formatter. dateFormat = " yyyy-MM-dd'T'HH:mm:ssZZZZZ "
424
+ XCTAssertNil ( formatter. date ( from: " 2018-03-09 " ) )
425
+ let d2 = try formatter. date ( from: " 2018-03-09T10:25:16+01:00 " ) . unwrapped ( )
426
+ XCTAssertEqual ( d2. description, " 2018-03-09 09:25:16 +0000 " )
427
+ }
412
428
}
You can’t perform that action at this time.
0 commit comments