Skip to content

Commit

Permalink
URL-encode dates using unix timestamp (vapor#2276)
Browse files Browse the repository at this point in the history
* Added ability to configure date coding/decoding for UrlEncodedForm

* Added documentation to the `DateFormat` enum

* renamed internetDateTime to iso8601

* Added custom date formatter

* Added comment about `ISO8601DateFormatter` performance

* Added ISO8601DateFormatter.threadSpecific so a new ISO8601DateFormatter isn't created for every encode/decode.

* Use typealias instead of declaring DateFormat 2x

* Added `ThreadSpecificDateFormatter` to ensure thread safety when using a `custom` DateFormat for `URLEncodedFormEncoder` or `URLEncodedFormDecoder`

* Fixed comment

* Changed custom interface to mimic `JSONDecoder.DateDecodingStrategy.custom(_:)` and `JSONEncoder.DateEncodingStrategy.custom(_:)` interfaces

* Removed unused ThreadSpecificDateFormatter

* `ISO8601DateFormatter` seems to be thread safe. This bug was filed: https://bugs.swift.org/browse/SR-7745?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel
I tried running the sample program with `10000000` iterations in the `vapor/swift:5.2` docker image without any issue.

* Default the date format for URLEncodedFrom coding/decoding to `timeIntervalSinceReferenceDate`

* Default to `timeIntervalSince1970`

* Removed all references to customizing DateFormat and default to `timeIntervalSince1970`

* Removed reference to `import NIO` and fixed spacing

* Fixed spacing

* Implemented using `extension Date: URLQueryFragmentConvertible`

* Spacing
  • Loading branch information
thecheatah authored and pull[bot] committed Mar 30, 2020
1 parent 1d48aa3 commit f8f83fd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Sources/Vapor/URLEncodedForm/URLQueryFragmentConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,16 @@ extension Decimal: URLQueryFragmentConvertible {
return .urlDecoded(self.description)
}
}

extension Date: URLQueryFragmentConvertible {
init?(urlQueryFragmentValue value: URLQueryFragment) {
guard let double = Double(urlQueryFragmentValue: value) else {
return nil
}
self = Date(timeIntervalSince1970: double)
}

var urlQueryFragmentValue: URLQueryFragment {
return timeIntervalSince1970.urlQueryFragmentValue
}
}
15 changes: 15 additions & 0 deletions Tests/VaporTests/URLEncodedFormTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ final class URLEncodedFormTests: XCTestCase {
XCTAssert(result.contains("isCool=true"))
}

func testDateCoding() throws {
let toEncode = DateCoding(date: Date(timeIntervalSince1970: 0))
let resultForTimeIntervalSince1970 = try URLEncodedFormEncoder()
.encode(toEncode)
XCTAssertEqual("date=0.0", resultForTimeIntervalSince1970)

let decodedTimeIntervalSince1970 = try URLEncodedFormDecoder()
.decode(DateCoding.self, from: resultForTimeIntervalSince1970)
XCTAssertEqual(decodedTimeIntervalSince1970, toEncode)
}

func testEncodedArrayValues() throws {
let user = User(name: "Tanner", age: 23, pets: ["Zizek", "Foo"], dict: ["a": 1, "b": 2], foos: [.baz], nums: [3.14], isCool: true)
let result = try URLEncodedFormEncoder(
Expand Down Expand Up @@ -469,3 +480,7 @@ private struct Users: Codable, Equatable {
private enum Foo: String, Codable {
case foo, bar, baz
}

struct DateCoding: Codable, Equatable {
let date: Date
}

0 comments on commit f8f83fd

Please sign in to comment.