Skip to content

Commit

Permalink
Fixes a iso8601 issue when a string might have a T (like Tue) but is …
Browse files Browse the repository at this point in the history
…not actually iso8601
  • Loading branch information
proggeramlug committed Oct 18, 2023
1 parent 87d3631 commit 1ff37f9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
Binary file not shown.
10 changes: 8 additions & 2 deletions Sources/String+SwiftyDates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,14 @@ extension String {
let cleanString = replacingOccurrences(of: " am", with: "am").replacingOccurrences(of: " pm", with: "pm")

// check for iso 8601 (yyyy-MM-ddTHH:mm:ss.SSZ)
if cleanString.contains("T") {
let parts: [String] = split(separator: "T").map(String.init)
var isIso8601 = false
let parts: [String] = split(separator: "T").map(String.init)

if cleanString.contains("T"), parts.count == 2 {
isIso8601 = true
}

if isIso8601 {
date = parts[0].swiftyDate(calendar: calendar)
time = parts[1].swiftyTime()
} else if cleanString.contains(" ") && (cleanString.contains(":") || cleanString.contains("am") || cleanString.contains("pm")) && (cleanString.contains("/") || cleanString.contains("-") || cleanString.contains(".")) {
Expand Down
1 change: 1 addition & 0 deletions SwiftyDatesTests/SwiftyDatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class SwiftyDatesTests: XCTestCase {
self.iso8601TestCases.append(TestCase("2001-02-03T04:05:06.007+06:30", "02/02/2001 21:35:06"))
self.iso8601TestCases.append(TestCase("2001-02-03T04:05:06.007-06:30", "02/03/2001 10:35:06"))
self.iso8601TestCases.append(TestCase("2020-01-16T08:34:00Z", "01/16/2020 08:34:00"))
self.iso8601TestCases.append(TestCase("Tue, 17 Oct 2023 21:46:34 -0400", "10/17/2023 17:46:34"))

// Text dates
self.datetimeTestCases.append(TestCase("Fri, 15 Jan 2021 13:22:00 -0500", "01/15/2021 08:22:00"))
Expand Down

0 comments on commit 1ff37f9

Please sign in to comment.