Skip to content

Commit

Permalink
include no iso extended format
Browse files Browse the repository at this point in the history
  • Loading branch information
osrufung committed Jan 24, 2021
1 parent c588ac7 commit ed83cfd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
12 changes: 9 additions & 3 deletions Sources/SafeCodable/SafeDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ extension Date {
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
static func getDateFromString(_ dateString: String, includeFractions: Bool = false) -> Date? {
static let longFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter
}()

static func getDateFromString(_ from: String, includeFractions: Bool = false) -> Date? {
let formatter = ISO8601DateFormatter()
var options:ISO8601DateFormatter.Options = [.withInternetDateTime,
.withDashSeparatorInDate,
Expand All @@ -23,8 +29,8 @@ extension Date {
options.insert(.withFractionalSeconds)
}
formatter.formatOptions = options
guard let date = formatter.date(from: dateString) else {
return shortFormatter.date(from: dateString)
guard let date = formatter.date(from: from) else {
return shortFormatter.date(from: from) ?? longFormatter.date(from: from)
}
return date
}
Expand Down
5 changes: 3 additions & 2 deletions Tests/SafeCodableTests/SafeCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ final class SafeCodableTests: XCTestCase {
[
{ "value" : "2015-08-29T11:22:09Z"},
{ "value" : "2015-08-29T11:22:09.129Z"},
{ "value" : "2020-11-27"}
{ "value" : "2020-11-27"},
{ "value" : "2019-08-03 23:59:59"}
]
""".data(using: .utf8)
Expand All @@ -42,7 +43,7 @@ final class SafeCodableTests: XCTestCase {
}

let dates = try? decoder.decode([DateContainer].self, from: rawDates!)
XCTAssertEqual(dates?.count, 3)
XCTAssertEqual(dates?.count, 4)
}

static var allTests = [
Expand Down

0 comments on commit ed83cfd

Please sign in to comment.