-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed force unwrap from string initializers and added tests. isoDat…
…eTime now includes seconds, Removed isoDateTimeSec. Renamed isoDateTimeMilliSec isoDateTimeFull.
- Loading branch information
Showing
4 changed files
with
126 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import XCTest | ||
@testable import DateHelper | ||
|
||
final class DateHelperTests: XCTestCase { | ||
|
||
// date from isoYear string | ||
func testDateFromIsoYearString() throws { | ||
let date = Date(fromString: "2009", format: .isoYear) | ||
let comparison = Calendar.current.date(from: DateComponents(year: 2009)) | ||
XCTAssertEqual(date, comparison) | ||
} | ||
|
||
// date from isoYearMonth string | ||
func testDateFromIsoYearMonthString() throws { | ||
let date = Date(fromString: "2009-08", format: .isoYearMonth) | ||
let comparison = Calendar.current.date(from: DateComponents(year: 2009, month: 08)) | ||
XCTAssertEqual(date, comparison) | ||
} | ||
|
||
// date from isoYearMonth string | ||
func testDateFromIsoDateString() throws { | ||
let date = Date(fromString: "2009-08-11", format: .isoDate) | ||
let comparison = Calendar.current.date(from: DateComponents(year: 2009, month: 08, day: 11)) | ||
XCTAssertEqual(date, comparison) | ||
} | ||
|
||
// date from isoDateTime string | ||
func testDateFromIsoDateTimeString() throws { | ||
let date = Date(fromString: "2009-08-11T06:30:03-05:00", format: .isoDateTime) | ||
let offset = -5 | ||
let timeZone = TimeZone(secondsFromGMT: 60 * 60 * offset) | ||
let comparison = Calendar.current.date(from: DateComponents(timeZone: timeZone, year: 2009, month: 08, day: 11, hour: 06, minute: 30, second: 03)) | ||
XCTAssertEqual(date, comparison) | ||
} | ||
|
||
// date from isoDateTimeFull string | ||
func testDateFromIsoDateTimeFullString() throws { | ||
let date = Date(fromString: "2009-08-11T06:30:03.161-05:00", format: .isoDateTimeFull) | ||
let offset = -5 | ||
let nanoseconds = 161*1000000 | ||
let timeZone = TimeZone(secondsFromGMT: 60 * 60 * offset) | ||
let comparison = Calendar.current.date(from: DateComponents(timeZone: timeZone, year: 2009, month: 08, day: 11, hour: 06, minute: 30, second: 03, nanosecond: nanoseconds)) | ||
XCTAssertEqual(date, comparison) | ||
} | ||
|
||
// date from dotNet string | ||
func testDateFromDotNetString() throws { | ||
let date = Date(fromString: "/Date(1260123281843)/", format: .dotNet) | ||
let timeZone = TimeZone(secondsFromGMT: 0) | ||
let nanoseconds = 843*1000000 | ||
let comparison = Calendar.current.date(from: DateComponents(timeZone: timeZone, year: 2009, month: 12, day: 06, hour: 18, minute: 14, second: 41, nanosecond: nanoseconds)) | ||
XCTAssertEqual(date?.timeIntervalSince1970, comparison?.timeIntervalSince1970) | ||
} | ||
|
||
// date from rss string | ||
func testDateFromRSSString() throws { | ||
let date = Date(fromString: "Fri, 09 Sep 2011 15:26:08 +0200", format: .rss) | ||
let offset = 2 | ||
let timeZone = TimeZone(secondsFromGMT: 60 * 60 * offset) | ||
let comparison = Calendar.current.date(from: DateComponents(timeZone: timeZone, year: 2011, month: 09, day: 09, hour: 15, minute: 26, second: 08)) | ||
XCTAssertEqual(date, comparison) | ||
} | ||
|
||
// date from altRSS string | ||
func testDateFromAltRSSString() throws { | ||
let date = Date(fromString: "09 Sep 2011 15:26:08 +0200", format: .altRSS) | ||
let offset = 2 | ||
let timeZone = TimeZone(secondsFromGMT: 60 * 60 * offset) | ||
let comparison = Calendar.current.date(from: DateComponents(timeZone: timeZone, year: 2011, month: 09, day: 09, hour: 15, minute: 26, second: 08)) | ||
XCTAssertEqual(date, comparison) | ||
} | ||
|
||
// date from httpHeader string | ||
func testDateFromHttpHeaderString() throws { | ||
let date = Date(fromString: "Wed, 01 03 2017 06:43:19 -0500", format: .httpHeader) | ||
let offset = -5 | ||
let timeZone = TimeZone(secondsFromGMT: 60 * 60 * offset) | ||
let comparison = Calendar.current.date(from: DateComponents(timeZone: timeZone, year: 2017, month: 03, day: 01, hour: 06, minute: 43, second: 19)) | ||
XCTAssertEqual(date, comparison) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.