Skip to content

Commit

Permalink
feat: Add parseDate method to support none/ multiple fractional sec…
Browse files Browse the repository at this point in the history
…ond for a date string
  • Loading branch information
ankurkbackbase03 committed Aug 29, 2024
1 parent cc90ec7 commit 3a4d73c
Showing 1 changed file with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import Foundation
return formatter
}()

// Primary formatter with fractional seconds
private static let sharedDateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
return formatter
}()

private func setup() {
calendar = Calendar(identifier: .iso8601)
locale = Locale(identifier: "en_US_POSIX")
Expand All @@ -39,6 +48,33 @@ import Foundation
if let result = super.date(from: string) {
return result
}
return OpenISO8601DateFormatter.withoutSeconds.date(from: string)
if let result = OpenISO8601DateFormatter.withoutSeconds.date(from: string) {
return result
}
return OpenISO8601DateFormatter.parseDate(from: string)
}

private static func parseDate(from dateString: String) -> Date? {
// Base date format
let baseFormat = "yyyy-MM-dd'T'HH:mm:ss"
// Create an instance of DateFormatter for parsing
let dateFormatter = OpenISO8601DateFormatter.sharedDateFormatter
// Check for fractional seconds by locating the dot
guard let dotIndex = dateString.firstIndex(of: ".") else {
// No fractional seconds, use base format
dateFormatter.dateFormat = baseFormat
return dateFormatter.date(from: dateString)
}

// Extract fractional part length
let fractionLength = dateString[dateString.index(after: dotIndex)...].count

// Construct the date format with fractional seconds
let fractionalFormat = String(repeating: "S", count: fractionLength)
dateFormatter.dateFormat = "\(baseFormat).\(fractionalFormat)"

return dateFormatter.date(from: dateString)
}
}
}

0 comments on commit 3a4d73c

Please sign in to comment.