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:
Ralph Küpper 2018-03-12 06:23:35 -04:00
parent a27ed9e9d6
commit a0161cf29a
4 changed files with 56 additions and 17 deletions

View File

@ -10,7 +10,7 @@ let dateString = "2018-03-08T15:49:46Z"
let dateFormatter = DateFormatter() let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "YYYYMMdd'T'HHmmss'Z'" 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. 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: With _SwiftyDates_ all you need to do is this:
```swift ```swift
let dateString = "2018-03-08T15:49:46Z" let dateString = "2018-03-08T15:49:46Z"
let date:Date = dateString.swiftyDateTime() // this will work let date:Date? = dateString.swiftyDateTime() // this will work
``` ```
## Dates ## Dates
@ -57,6 +57,7 @@ Or in combinations:
2. Years are always given in 4 digits or in combination with months and days 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 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) 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 ## Limitations / Todo
1. No support for ordinal dates 1. No support for ordinal dates

View File

@ -9,7 +9,7 @@
import Foundation import Foundation
extension String { extension String {
func swiftyTime() -> TimeInterval { public func swiftyTime() -> TimeInterval {
var hour: Double = 0 var hour: Double = 0
var minute: Double = 0 var minute: Double = 0
var second: Double = 0 var second: Double = 0
@ -70,7 +70,12 @@ extension String {
return Double((hour * 3600) + (minute * 60) + second - offset) 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 day: Int?
var month: Int? var month: Int?
var year: Int? var year: Int?
@ -171,9 +176,13 @@ extension String {
let components = DateComponents(calendar: calendar, year: year, month: month, day: day) let components = DateComponents(calendar: calendar, year: year, month: month, day: day)
return components.date! 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 time: TimeInterval = 0
var date: Date var date: Date?
if (self == "") {
return nil
}
// cleaning // cleaning
let cleanString = replacingOccurrences(of: " am", with: "am").replacingOccurrences(of: " pm", with: "pm") let cleanString = replacingOccurrences(of: " am", with: "am").replacingOccurrences(of: " pm", with: "pm")
@ -200,6 +209,9 @@ extension String {
else { else {
date = swiftyDate() date = swiftyDate()
} }
return date + time if (date == nil) {
return nil
}
return date! + time
} }
} }

View File

@ -11,9 +11,9 @@ import XCTest
struct TestCase { struct TestCase {
var input:String var input:String
var output:String var output:String?
init(_ i: String, _ o: String) { init(_ i: String, _ o: String?) {
input = i input = i
output = o output = o
} }
@ -83,6 +83,7 @@ class SwiftyDatesTests: XCTestCase {
timeTestCases.append(TestCase("10.10pm", "01/01/2018 22:10:00")) 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("10.10 pm", "01/01/2018 22:10:00"))
timeTestCases.append(TestCase("10am", "01/01/2018 10:00: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 10am", "10/12/2018 10:00:00"))
datetimeTestCases.append(TestCase("10/12/2018 10:30am", "10/12/2018 10:30:00")) datetimeTestCases.append(TestCase("10/12/2018 10:30am", "10/12/2018 10:30:00"))
@ -109,24 +110,43 @@ class SwiftyDatesTests: XCTestCase {
func testIsoCases() { func testIsoCases() {
let dateFormatter = getDateFormatter() let dateFormatter = getDateFormatter()
for tc in iso8601TestCases { for tc in iso8601TestCases {
let formatedDate = dateFormatter.string(from: tc.input.swiftyDateTime()) let date:Date? = tc.input.swiftyDateTime()
XCTAssertEqual(formatedDate, tc.output) if (date == nil) {
XCTAssertNil(tc.output)
}
else {
let formatedDate = dateFormatter.string(from: date!)
XCTAssertEqual(formatedDate, tc.output)
}
} }
} }
func testDateCases() { func testDateCases() {
let dateFormatter = getDateFormatter() let dateFormatter = getDateFormatter()
for tc in dateTestCases { for tc in dateTestCases {
let formatedDate = dateFormatter.string(from: tc.input.swiftyDateTime()) let date:Date? = tc.input.swiftyDateTime()
XCTAssertEqual(formatedDate, tc.output) if (date == nil) {
XCTAssertNil(tc.output)
}
else {
let formatedDate = dateFormatter.string(from: date!)
XCTAssertEqual(formatedDate, tc.output)
}
} }
} }
func testDateTimeCases() { func testDateTimeCases() {
let dateFormatter = getDateFormatter() let dateFormatter = getDateFormatter()
for tc in datetimeTestCases { for tc in datetimeTestCases {
let formatedDate = dateFormatter.string(from: tc.input.swiftyDateTime()) let date:Date? = tc.input.swiftyDateTime()
XCTAssertEqual(formatedDate, tc.output) if (date == nil) {
XCTAssertNil(tc.output)
}
else {
let formatedDate = dateFormatter.string(from: date!)
XCTAssertEqual(formatedDate, tc.output)
}
} }
} }
@ -136,8 +156,14 @@ class SwiftyDatesTests: XCTestCase {
for tc in timeTestCases { 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)
XCTAssertEqual(formatedDate, tc.output) if (date == nil) {
XCTAssertNil(tc.output)
}
else {
let formatedDate = dateFormatter.string(from: date!)
XCTAssertEqual(formatedDate, tc.output)
}
} }
} }