Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support none / one / multiple fractional seconds #907

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ It currently consists of

# Release Notes
BOAT is still under development and subject to change.
## 0.17.46
* boat-scaffold
* Enhanced ISO8601 Date Formatting with Fractional Seconds Support for Swift template.
* The `OpenISO8601DateFormatter` template now supports parsing and formatting ISO8601 dates with none, one, or multiple fractional seconds in Swift 5. This enhancement provides greater flexibility and precision when working with date and time values, accommodating various use cases that require different levels of fractional second accuracy.
## 0.17.36
* Lint rule `B014` doesn't throw a null exception when parsing a string array property in a schema.
## 0.17.35
Expand Down
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)
}
}
}