Skip to content

Commit

Permalink
Get rid of global iso8601 formatter (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Nov 16, 2023
1 parent d7fb66e commit a0a17ae
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,4 @@ internal enum URLEncodedForm {

/// ASCII characters that will not be percent encoded in URL encoded form data
static let unreservedCharacters = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~")

@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
/// ISO8601 data formatter used throughout URL encoded form code
static var iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime
return formatter
}()
}
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,10 @@ extension _URLEncodedFormDecoder {
return Date(timeIntervalSince1970: seconds)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
let iso8601Formatter = ISO8601DateFormatter()
iso8601Formatter.formatOptions = .withInternetDateTime
let dateString = try unbox(node, as: String.self)
guard let date = URLEncodedForm.iso8601Formatter.date(from: dateString) else {
guard let date = iso8601Formatter.date(from: dateString) else {
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Invalid date format"))
}
return date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ extension _URLEncodedFormEncoder {
try self.encode(Double(date.timeIntervalSince1970).description)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
try encode(URLEncodedForm.iso8601Formatter.string(from: date))
let iso8601Formatter = ISO8601DateFormatter()
iso8601Formatter.formatOptions = .withInternetDateTime
try encode(iso8601Formatter.string(from: date))
} else {
preconditionFailure("ISO8601DateFormatter is unavailable on this platform")
}
Expand Down

1 comment on commit a0a17ae

@mkll
Copy link

@mkll mkll commented on a0a17ae Nov 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just replace this

static var iso8601Formatter: ISO8601DateFormatter = {
	let formatter = ISO8601DateFormatter()
	formatter.formatOptions = .withInternetDateTime
	return formatter
}()

with this?

static var iso8601Formatter: ISO8601DateFormatter: {
	let formatter = ISO8601DateFormatter()
	formatter.formatOptions = .withInternetDateTime
	return formatter
}

i.e. stored var with computed one?

Please sign in to comment.