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

Fail early when parsing a date from an empty or blank string #181

Merged
merged 1 commit into from
Mar 16, 2025
Merged
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
10 changes: 9 additions & 1 deletion Sources/FeedKit/FeedDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class PermissiveDateFormatter: DateFormatter, @unchecked Sendable {
override func date(from string: String) -> Date? {
let trimmedString = string.trimmingCharacters(in: .whitespacesAndNewlines)

guard !trimmedString.isEmpty else {
return nil
}

// Attempts parsing with the last successful format first to avoid
// unnecessary iterations over all formats.
if dateFormat != nil, !dateFormat.isEmpty {
Expand Down Expand Up @@ -270,7 +274,11 @@ final class FeedDateFormatter: DateFormatter, @unchecked Sendable {
/// - string: The date string to be parsed.
/// - Returns: A Date object if parsing is successful, otherwise nil.
override func date(from string: String) -> Date? {
switch spec {
guard !string.isEmpty else {
return nil
}

return switch spec {
case .iso8601:
iso8601Formatter.date(from: string)
case .rfc3339:
Expand Down
Loading