Skip to content
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

URL-encode dates using unix timestamp #2276

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4c6301f
Added ability to configure date coding/decoding for UrlEncodedForm
thecheatah Mar 29, 2020
a318cac
Added documentation to the `DateFormat` enum
thecheatah Mar 29, 2020
569205e
renamed internetDateTime to iso8601
thecheatah Mar 29, 2020
47d275c
Added custom date formatter
thecheatah Mar 29, 2020
ad50caa
Added comment about `ISO8601DateFormatter` performance
thecheatah Mar 29, 2020
4f8165f
Added ISO8601DateFormatter.threadSpecific so a new ISO8601DateFormatt…
thecheatah Mar 29, 2020
f212661
Use typealias instead of declaring DateFormat 2x
thecheatah Mar 29, 2020
78908d1
Added `ThreadSpecificDateFormatter` to ensure thread safety when usin…
thecheatah Mar 29, 2020
b9bdedb
Fixed comment
thecheatah Mar 29, 2020
287dff9
Changed custom interface to mimic `JSONDecoder.DateDecodingStrategy.c…
thecheatah Mar 29, 2020
bbaf3e8
Removed unused ThreadSpecificDateFormatter
thecheatah Mar 29, 2020
73617bf
`ISO8601DateFormatter` seems to be thread safe. This bug was filed: h…
thecheatah Mar 30, 2020
2cdccb4
Default the date format for URLEncodedFrom coding/decoding to `timeIn…
thecheatah Mar 30, 2020
ab98e1e
Default to `timeIntervalSince1970`
thecheatah Mar 30, 2020
5a0b265
Removed all references to customizing DateFormat and default to `time…
thecheatah Mar 30, 2020
c89fa0f
Removed reference to `import NIO` and fixed spacing
thecheatah Mar 30, 2020
90fe00c
Fixed spacing
thecheatah Mar 30, 2020
61e124b
Implemented using `extension Date: URLQueryFragmentConvertible`
thecheatah Mar 30, 2020
3fc9a91
Spacing
thecheatah Mar 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}