Skip to content

Commit 0da7da2

Browse files
authored
Merge pull request #1586 from armadsen/sr-7079
Add support for .withFractionalSeconds to ISO8601DateFormatter.
2 parents 3f59e33 + fbda4ed commit 0da7da2

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Foundation/ISO8601DateFormatter.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extension ISO8601DateFormatter {
3737

3838
public static var withColonSeparatorInTimeZone = ISO8601DateFormatter.Options(rawValue: 1 << 10)
3939

40+
public static var withFractionalSeconds = ISO8601DateFormatter.Options(rawValue: 1 << 11)
41+
4042
public static var withFullDate = ISO8601DateFormatter.Options(rawValue: withYear.rawValue + withMonth.rawValue + withDay.rawValue + withDashSeparatorInDate.rawValue)
4143

4244
public static var withFullTime = ISO8601DateFormatter.Options(rawValue: withTime.rawValue + withTimeZone.rawValue + withColonSeparatorInTime.rawValue + withColonSeparatorInTimeZone.rawValue)

TestFoundation/TestISO8601DateFormatter.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class TestISO8601DateFormatter: XCTestCase {
2020

2121
func test_stringFromDate() {
2222
let formatter = DateFormatter()
23-
formatter.dateFormat = "yyyy/MM/dd HH:mm zzz"
24-
let dateString = "2016/10/08 22:31 GMT"
23+
formatter.dateFormat = "yyyy/MM/dd HH:mm:ss.SSSS zzz"
24+
let dateString = "2016/10/08 22:31:00.0713 GMT"
2525

2626
guard let someDateTime = formatter.date(from: dateString) else {
2727
XCTFail("DateFormatter was unable to parse '\(dateString)' using '\(formatter.dateFormat ?? "")' date format.")
@@ -44,6 +44,9 @@ class TestISO8601DateFormatter: XCTestCase {
4444
isoFormatter.formatOptions = .withFullTime
4545
XCTAssertEqual(isoFormatter.string(from: someDateTime), "22:31:00Z")
4646

47+
isoFormatter.formatOptions = [.withFullTime, .withFractionalSeconds]
48+
XCTAssertEqual(isoFormatter.string(from: someDateTime), "22:31:00.071Z")
49+
4750
isoFormatter.formatOptions = .withFullDate
4851
XCTAssertEqual(isoFormatter.string(from: someDateTime), "2016-10-08")
4952

@@ -53,9 +56,15 @@ class TestISO8601DateFormatter: XCTestCase {
5356
isoFormatter.formatOptions = [.withFullTime, .withFullDate, .withSpaceBetweenDateAndTime]
5457
XCTAssertEqual(isoFormatter.string(from: someDateTime), "2016-10-08 22:31:00Z")
5558

59+
isoFormatter.formatOptions = [.withFullTime, .withFullDate, .withSpaceBetweenDateAndTime, .withFractionalSeconds]
60+
XCTAssertEqual(isoFormatter.string(from: someDateTime), "2016-10-08 22:31:00.071Z")
61+
5662
isoFormatter.formatOptions = [.withDay, .withTime]
5763
XCTAssertEqual(isoFormatter.string(from: someDateTime), "282T223100")
5864

65+
isoFormatter.formatOptions = [.withDay, .withTime, .withFractionalSeconds]
66+
XCTAssertEqual(isoFormatter.string(from: someDateTime), "282T223100.071")
67+
5968
isoFormatter.formatOptions = [.withWeekOfYear, .withTime]
6069
XCTAssertEqual(isoFormatter.string(from: someDateTime), "W40T223100")
6170

@@ -79,6 +88,9 @@ class TestISO8601DateFormatter: XCTestCase {
7988

8089
isoFormatter.formatOptions = [.withWeekOfYear, .withMonth, .withTime, .withColonSeparatorInTime, .withSpaceBetweenDateAndTime, .withDashSeparatorInDate]
8190
XCTAssertEqual(isoFormatter.string(from: someDateTime), "10-W40 22:31:00")
91+
92+
isoFormatter.formatOptions = [.withWeekOfYear, .withMonth, .withTime, .withColonSeparatorInTime, .withSpaceBetweenDateAndTime, .withDashSeparatorInDate, .withFractionalSeconds]
93+
XCTAssertEqual(isoFormatter.string(from: someDateTime), "10-W40 22:31:00.071")
8294

8395
isoFormatter.formatOptions = [.withDay, .withWeekOfYear]
8496
XCTAssertEqual(isoFormatter.string(from: someDateTime), "W4006")
@@ -92,6 +104,10 @@ class TestISO8601DateFormatter: XCTestCase {
92104
isoFormatter.formatOptions = [.withDay, .withWeekOfYear, .withMonth]
93105
XCTAssertEqual(isoFormatter.string(from: someDateTime), "10W4006")
94106

107+
// .withFractionalSeconds should be ignored if neither .withTime or .withFullTime are specified
108+
isoFormatter.formatOptions = [.withDay, .withWeekOfYear, .withMonth, .withFractionalSeconds]
109+
XCTAssertEqual(isoFormatter.string(from: someDateTime), "10W4006")
110+
95111
isoFormatter.formatOptions = [.withMonth, .withDay, .withWeekOfYear, .withDashSeparatorInDate]
96112
XCTAssertEqual(isoFormatter.string(from: someDateTime), "10-W40-06")
97113

0 commit comments

Comments
 (0)