Skip to content
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
2 changes: 2 additions & 0 deletions Foundation/ISO8601DateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extension ISO8601DateFormatter {

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

public static var withFractionalSeconds = ISO8601DateFormatter.Options(rawValue: 1 << 11)

public static var withFullDate = ISO8601DateFormatter.Options(rawValue: withYear.rawValue + withMonth.rawValue + withDay.rawValue + withDashSeparatorInDate.rawValue)

public static var withFullTime = ISO8601DateFormatter.Options(rawValue: withTime.rawValue + withTimeZone.rawValue + withColonSeparatorInTime.rawValue + withColonSeparatorInTimeZone.rawValue)
Expand Down
20 changes: 18 additions & 2 deletions TestFoundation/TestISO8601DateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class TestISO8601DateFormatter: XCTestCase {

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

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

isoFormatter.formatOptions = [.withFullTime, .withFractionalSeconds]
XCTAssertEqual(isoFormatter.string(from: someDateTime), "22:31:00.071Z")

isoFormatter.formatOptions = .withFullDate
XCTAssertEqual(isoFormatter.string(from: someDateTime), "2016-10-08")

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

isoFormatter.formatOptions = [.withFullTime, .withFullDate, .withSpaceBetweenDateAndTime, .withFractionalSeconds]
XCTAssertEqual(isoFormatter.string(from: someDateTime), "2016-10-08 22:31:00.071Z")

isoFormatter.formatOptions = [.withDay, .withTime]
XCTAssertEqual(isoFormatter.string(from: someDateTime), "282T223100")

isoFormatter.formatOptions = [.withDay, .withTime, .withFractionalSeconds]
XCTAssertEqual(isoFormatter.string(from: someDateTime), "282T223100.071")

isoFormatter.formatOptions = [.withWeekOfYear, .withTime]
XCTAssertEqual(isoFormatter.string(from: someDateTime), "W40T223100")

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

isoFormatter.formatOptions = [.withWeekOfYear, .withMonth, .withTime, .withColonSeparatorInTime, .withSpaceBetweenDateAndTime, .withDashSeparatorInDate]
XCTAssertEqual(isoFormatter.string(from: someDateTime), "10-W40 22:31:00")

isoFormatter.formatOptions = [.withWeekOfYear, .withMonth, .withTime, .withColonSeparatorInTime, .withSpaceBetweenDateAndTime, .withDashSeparatorInDate, .withFractionalSeconds]
XCTAssertEqual(isoFormatter.string(from: someDateTime), "10-W40 22:31:00.071")

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

// .withFractionalSeconds should be ignored if neither .withTime or .withFullTime are specified
isoFormatter.formatOptions = [.withDay, .withWeekOfYear, .withMonth, .withFractionalSeconds]
XCTAssertEqual(isoFormatter.string(from: someDateTime), "10W4006")

isoFormatter.formatOptions = [.withMonth, .withDay, .withWeekOfYear, .withDashSeparatorInDate]
XCTAssertEqual(isoFormatter.string(from: someDateTime), "10-W40-06")

Expand Down