Skip to content

Commit 3253b71

Browse files
committed
Add support for RFC 1123 dates #176
1 parent ecde9cd commit 3253b71

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

Sources/FeedKit/FeedDateFormatter.swift

+32-3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,25 @@ class RFC822DateFormatter: PermissiveDateFormatter, @unchecked Sendable {
183183
}
184184
}
185185

186+
// MARK: - RFC1123 formatter
187+
188+
/// Formatter for RFC1123 date specification.
189+
class RFC1123DateFormatter: PermissiveDateFormatter, @unchecked Sendable {
190+
/// List of date formats supported for RFC1123.
191+
override var dateFormats: [String] {
192+
[
193+
"EEE, dd MMM yyyy HH:mm:ss z"
194+
]
195+
}
196+
197+
override var permissiveDateFormats: [String] {
198+
[
199+
// Omits the time and timezone
200+
"EEE, dd MMM yyyy"
201+
]
202+
}
203+
}
204+
186205
// MARK: - DateSpec
187206

188207
/// Enum representing different date specifications.
@@ -193,8 +212,10 @@ enum DateSpec {
193212
case rfc3339
194213
/// RFC822 date format (e.g., Tue, 05 Dec 2024 10:30:00 GMT).
195214
case rfc822
215+
/// RFC1123 date format (e.g., Fri, 06 Sep 2024 12:34:56 GMT).
216+
case rfc1123
196217
/// Permissive mode which attempts to parse the date using multiple formats.
197-
/// It tries RFC822 first, then RFC3339, and finally ISO8601 in that order.
218+
/// It tries RFC822 first, then RFC3339, RFC1123 and finally ISO8601 in that order.
198219
case permissive
199220
}
200221

@@ -231,6 +252,9 @@ class FeedDateFormatter: DateFormatter, @unchecked Sendable {
231252
/// RFC822 date formatter.
232253
lazy var rfc822Formatter: RFC822DateFormatter = .init()
233254

255+
/// RFC1123 date formatter.
256+
lazy var rfc1123Formatter: RFC1123DateFormatter = .init()
257+
234258
/// Converts a string to a Date based on the given date specification.
235259
///
236260
/// - Parameters:
@@ -244,10 +268,13 @@ class FeedDateFormatter: DateFormatter, @unchecked Sendable {
244268
rfc3339Formatter.date(from: string)
245269
case .rfc822:
246270
rfc822Formatter.date(from: string)
271+
case .rfc1123:
272+
rfc1123Formatter.date(from: string)
247273
case .permissive:
248274
rfc822Formatter.date(from: string) ??
249-
rfc3339Formatter.date(from: string) ??
250-
iso8601Formatter.date(from: string)
275+
rfc3339Formatter.date(from: string) ??
276+
rfc1123Formatter.date(from: string) ??
277+
iso8601Formatter.date(from: string)
251278
}
252279
}
253280

@@ -264,6 +291,8 @@ class FeedDateFormatter: DateFormatter, @unchecked Sendable {
264291
rfc3339Formatter.string(from: date)
265292
case .rfc822:
266293
rfc822Formatter.string(from: date)
294+
case .rfc1123:
295+
rfc1123Formatter.string(from: date)
267296
case .permissive:
268297
fatalError()
269298
}

0 commit comments

Comments
 (0)