diff --git a/README.md b/README.md index 3db9c64..15b9b67 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Sources/String+SwiftyDates.swift b/Sources/String+SwiftyDates.swift index 9fa7b37..0bbe525 100644 --- a/Sources/String+SwiftyDates.swift +++ b/Sources/String+SwiftyDates.swift @@ -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 } } diff --git a/SwiftyDates.xcodeproj/project.xcworkspace/xcuserdata/amlug.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftyDates.xcodeproj/project.xcworkspace/xcuserdata/amlug.xcuserdatad/UserInterfaceState.xcuserstate index b402ff7..df7b85a 100644 Binary files a/SwiftyDates.xcodeproj/project.xcworkspace/xcuserdata/amlug.xcuserdatad/UserInterfaceState.xcuserstate and b/SwiftyDates.xcodeproj/project.xcworkspace/xcuserdata/amlug.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/SwiftyDatesTests/SwiftyDatesTests.swift b/SwiftyDatesTests/SwiftyDatesTests.swift index 9e75829..98eb98c 100644 --- a/SwiftyDatesTests/SwiftyDatesTests.swift +++ b/SwiftyDatesTests/SwiftyDatesTests.swift @@ -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,24 +110,43 @@ class SwiftyDatesTests: XCTestCase { func testIsoCases() { let dateFormatter = getDateFormatter() for tc in iso8601TestCases { - let formatedDate = dateFormatter.string(from: tc.input.swiftyDateTime()) - XCTAssertEqual(formatedDate, tc.output) + 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()) - XCTAssertEqual(formatedDate, tc.output) + 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()) - XCTAssertEqual(formatedDate, tc.output) + let date:Date? = tc.input.swiftyDateTime() + 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 { - let formatedDate = dateFormatter.string(from: tc.input.swiftyDateTime(calendar: Calendar.current, baseDate: testDate)) - XCTAssertEqual(formatedDate, tc.output) + 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) + } } }