Return nil if no date possible
We are now returning nil if no date can be deterined. However. More testing is needed but this will help if simply no date is given at all.
This commit is contained in:
parent
a27ed9e9d6
commit
a0161cf29a
|
@ -10,7 +10,7 @@ let dateString = "2018-03-08T15:49:46Z"
|
|||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
|
||||
dateFormatter.dateFormat = "YYYYMMdd'T'HHmmss'Z'"
|
||||
let date:Date = dateFormatter.string(from: dateString) // this will work.
|
||||
let date:Date? = dateFormatter.string(from: dateString) // this will work.
|
||||
```
|
||||
So far so good. Now, if we instead get another date, say: `2018-03-08T15:49:46.5+03:30` Now Swift will throw you an exception because the specified format `YYYYMMdd'T'HHmmss'Z'`does not fit anymore.
|
||||
|
||||
|
@ -25,7 +25,7 @@ let date:Date = dateFormatter.string(from: dateString) // this will not work wor
|
|||
With _SwiftyDates_ all you need to do is this:
|
||||
```swift
|
||||
let dateString = "2018-03-08T15:49:46Z"
|
||||
let date:Date = dateString.swiftyDateTime() // this will work
|
||||
let date:Date? = dateString.swiftyDateTime() // this will work
|
||||
```
|
||||
|
||||
## Dates
|
||||
|
@ -57,6 +57,7 @@ Or in combinations:
|
|||
2. Years are always given in 4 digits or in combination with months and days
|
||||
3. Time without dates is an number in seconds from the beginning of the day
|
||||
4. Dates without times have the time 00:00:00 (hh-mm-ss)
|
||||
5. We are not throwing exceptions, if a date/time does not work _nil_ is returned.
|
||||
|
||||
## Limitations / Todo
|
||||
1. No support for ordinal dates
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
import Foundation
|
||||
|
||||
extension String {
|
||||
func swiftyTime() -> TimeInterval {
|
||||
public func swiftyTime() -> TimeInterval {
|
||||
var hour: Double = 0
|
||||
var minute: Double = 0
|
||||
var second: Double = 0
|
||||
|
@ -70,7 +70,12 @@ extension String {
|
|||
return Double((hour * 3600) + (minute * 60) + second - offset)
|
||||
}
|
||||
|
||||
func swiftyDate(calendar:Calendar = Calendar.current) -> Date {
|
||||
public func swiftyDate(calendar:Calendar = Calendar.current) -> Date? {
|
||||
// is this even a string we can work with?
|
||||
if (self == "") {
|
||||
return nil
|
||||
}
|
||||
|
||||
var day: Int?
|
||||
var month: Int?
|
||||
var year: Int?
|
||||
|
@ -171,9 +176,13 @@ extension String {
|
|||
let components = DateComponents(calendar: calendar, year: year, month: month, day: day)
|
||||
return components.date!
|
||||
}
|
||||
func swiftyDateTime(calendar: Calendar = Calendar.current, baseDate: Date = Date()) -> Date {
|
||||
public func swiftyDateTime(calendar: Calendar = Calendar.current, baseDate: Date = Date()) -> Date? {
|
||||
var time: TimeInterval = 0
|
||||
var date: Date
|
||||
var date: Date?
|
||||
|
||||
if (self == "") {
|
||||
return nil
|
||||
}
|
||||
|
||||
// cleaning
|
||||
let cleanString = replacingOccurrences(of: " am", with: "am").replacingOccurrences(of: " pm", with: "pm")
|
||||
|
@ -200,6 +209,9 @@ extension String {
|
|||
else {
|
||||
date = swiftyDate()
|
||||
}
|
||||
return date + time
|
||||
if (date == nil) {
|
||||
return nil
|
||||
}
|
||||
return date! + time
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -11,9 +11,9 @@ import XCTest
|
|||
|
||||
struct TestCase {
|
||||
var input:String
|
||||
var output:String
|
||||
var output:String?
|
||||
|
||||
init(_ i: String, _ o: String) {
|
||||
init(_ i: String, _ o: String?) {
|
||||
input = i
|
||||
output = o
|
||||
}
|
||||
|
@ -83,6 +83,7 @@ class SwiftyDatesTests: XCTestCase {
|
|||
timeTestCases.append(TestCase("10.10pm", "01/01/2018 22:10:00"))
|
||||
timeTestCases.append(TestCase("10.10 pm", "01/01/2018 22:10:00"))
|
||||
timeTestCases.append(TestCase("10am", "01/01/2018 10:00:00"))
|
||||
timeTestCases.append(TestCase("", nil))
|
||||
|
||||
datetimeTestCases.append(TestCase("10/12/2018 10am", "10/12/2018 10:00:00"))
|
||||
datetimeTestCases.append(TestCase("10/12/2018 10:30am", "10/12/2018 10:30:00"))
|
||||
|
@ -109,26 +110,45 @@ class SwiftyDatesTests: XCTestCase {
|
|||
func testIsoCases() {
|
||||
let dateFormatter = getDateFormatter()
|
||||
for tc in iso8601TestCases {
|
||||
let formatedDate = dateFormatter.string(from: tc.input.swiftyDateTime())
|
||||
let date:Date? = tc.input.swiftyDateTime()
|
||||
if (date == nil) {
|
||||
XCTAssertNil(tc.output)
|
||||
}
|
||||
else {
|
||||
let formatedDate = dateFormatter.string(from: date!)
|
||||
XCTAssertEqual(formatedDate, tc.output)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func testDateCases() {
|
||||
let dateFormatter = getDateFormatter()
|
||||
for tc in dateTestCases {
|
||||
let formatedDate = dateFormatter.string(from: tc.input.swiftyDateTime())
|
||||
let date:Date? = tc.input.swiftyDateTime()
|
||||
if (date == nil) {
|
||||
XCTAssertNil(tc.output)
|
||||
}
|
||||
else {
|
||||
let formatedDate = dateFormatter.string(from: date!)
|
||||
XCTAssertEqual(formatedDate, tc.output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDateTimeCases() {
|
||||
let dateFormatter = getDateFormatter()
|
||||
for tc in datetimeTestCases {
|
||||
let formatedDate = dateFormatter.string(from: tc.input.swiftyDateTime())
|
||||
let date:Date? = tc.input.swiftyDateTime()
|
||||
if (date == nil) {
|
||||
XCTAssertNil(tc.output)
|
||||
}
|
||||
else {
|
||||
let formatedDate = dateFormatter.string(from: date!)
|
||||
XCTAssertEqual(formatedDate, tc.output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testTimeCases() {
|
||||
let dateFormatter = getDateFormatter()
|
||||
|
@ -136,10 +156,16 @@ class SwiftyDatesTests: XCTestCase {
|
|||
|
||||
|
||||
for tc in timeTestCases {
|
||||
let formatedDate = dateFormatter.string(from: tc.input.swiftyDateTime(calendar: Calendar.current, baseDate: testDate))
|
||||
let date:Date? = tc.input.swiftyDateTime(calendar: Calendar.current, baseDate: testDate)
|
||||
if (date == nil) {
|
||||
XCTAssertNil(tc.output)
|
||||
}
|
||||
else {
|
||||
let formatedDate = dateFormatter.string(from: date!)
|
||||
XCTAssertEqual(formatedDate, tc.output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDateTimePerformanceExample() {
|
||||
// This is an example of a performance test case.
|
||||
|
|
Loading…
Reference in New Issue