Skip to content

Commit 6049594

Browse files
committed
Optimize date parsing by reusing the last successful date format
1 parent c997b65 commit 6049594

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Sources/FeedKit/FeedDateFormatter.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ class PermissiveDateFormatter: DateFormatter, @unchecked Sendable {
5555
/// Attempts to parse a string into a Date using available formats.
5656
override func date(from string: String) -> Date? {
5757
let trimmedString = string.trimmingCharacters(in: .whitespacesAndNewlines)
58+
59+
// Attempts parsing with the last successful format first to avoid
60+
// unnecessary iterations over all formats.
61+
if dateFormat != nil && !dateFormat.isEmpty {
62+
if let date = super.date(from: trimmedString) {
63+
return date
64+
}
65+
}
66+
5867
for format in dateFormats + permissiveDateFormats {
5968
dateFormat = format
6069
if let date = super.date(from: trimmedString) {

0 commit comments

Comments
 (0)