Skip to content

feat: support three different formats #2722

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

Closed
Changes from 1 commit
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
Prev Previous commit
feat: a array include three different date formatter
  • Loading branch information
zymxxxs committed Mar 20, 2020
commit fe3b72c19bdfe3e0f1dc7277161cd8ebd97e2971
33 changes: 15 additions & 18 deletions Sources/FoundationNetworking/URLSession/HTTP/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,34 +239,31 @@ internal class _HTTPURLProtocol: _NativeProtocol {
return true
}

static let dateFormatter: DateFormatter = {
let x = DateFormatter()
return x
}()

static func date(from string: String) -> Date? {
static let dateFormatters: [DateFormatter] = {
// https://tools.ietf.org/html/rfc2616#section-3.3.1
// HTTP applications have historically allowed three different formats
// for the representation of date/time stamps

// RCF 822 --- Sun, 06 Nov 1994 08:49:37 GMT
self.dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
if let d1 = self.dateFormatter.date(from: string) {
return d1
}
let d1 = DateFormatter()
d1.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"

// RCF 855 --- Sunday, 06-Nov-94 08:49:37 GMT
self.dateFormatter.dateFormat = "EEEE, dd-MMM-yy HH:mm:ss zzz"
if let d2 = self.dateFormatter.date(from: string) {
return d2
}
let d2 = DateFormatter()
d2.dateFormat = "EEEE, dd-MMM-yy HH:mm:ss zzz"

// ANSI C's asctime() format --- Sun Nov 6 08:49:37 1994
self.dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss yy"
if let d3 = self.dateFormatter.date(from: string) {
return d3
let d3 = DateFormatter()
d3.dateFormat = "EEE MMM dd HH:mm:ss yy"
return [d1, d2, d3]
}()

static func date(from string: String) -> Date? {
for dateFormat in self.dateFormatters {
if let d = dateFormat.date(from: string) {
return d
}
}

return nil
}

Expand Down