Skip to content

Commit 0a95017

Browse files
authored
Merge pull request #181 from sebj/dateformatter-fail-early
2 parents bfb0d0a + 0d5b2b7 commit 0a95017

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Sources/FeedKit/FeedDateFormatter.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class PermissiveDateFormatter: DateFormatter, @unchecked Sendable {
5656
override func date(from string: String) -> Date? {
5757
let trimmedString = string.trimmingCharacters(in: .whitespacesAndNewlines)
5858

59+
guard !trimmedString.isEmpty else {
60+
return nil
61+
}
62+
5963
// Attempts parsing with the last successful format first to avoid
6064
// unnecessary iterations over all formats.
6165
if dateFormat != nil, !dateFormat.isEmpty {
@@ -270,7 +274,11 @@ final class FeedDateFormatter: DateFormatter, @unchecked Sendable {
270274
/// - string: The date string to be parsed.
271275
/// - Returns: A Date object if parsing is successful, otherwise nil.
272276
override func date(from string: String) -> Date? {
273-
switch spec {
277+
guard !string.isEmpty else {
278+
return nil
279+
}
280+
281+
return switch spec {
274282
case .iso8601:
275283
iso8601Formatter.date(from: string)
276284
case .rfc3339:

0 commit comments

Comments
 (0)