Skip to content

SR-9322: DateFormatter difference between macOS and Linux #1822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Foundation/DateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ open class DateFormatter : Formatter {
}
return res._swiftObject
}

// range.length is updated with the last position of the input string that was parsed
guard range.length == string.length else {
// The whole string was not parsed
return nil
}
return date
}

Expand Down
16 changes: 16 additions & 0 deletions TestFoundation/TestDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TestDateFormatter: XCTestCase {
("test_setTimeZoneToNil", test_setTimeZoneToNil),
("test_setTimeZone", test_setTimeZone),
("test_expectedTimeZone", test_expectedTimeZone),
("test_dateFrom", test_dateFrom),
]
}

Expand Down Expand Up @@ -409,4 +410,19 @@ class TestDateFormatter: XCTestCase {
f.timeZone = losAngeles
XCTAssertEqual(f.string(from: now), losAngeles.abbreviation())
}

func test_dateFrom() throws {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(identifier: "UTC")
formatter.dateFormat = "yyyy-MM-dd"

XCTAssertNil(formatter.date(from: "2018-03-09T10:25:16+01:00"))
let d1 = try formatter.date(from: "2018-03-09").unwrapped()
XCTAssertEqual(d1.description, "2018-03-09 00:00:00 +0000")

formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
XCTAssertNil(formatter.date(from: "2018-03-09"))
let d2 = try formatter.date(from: "2018-03-09T10:25:16+01:00").unwrapped()
XCTAssertEqual(d2.description, "2018-03-09 09:25:16 +0000")
}
}