Skip to content

Commit c3ffc71

Browse files
committed
Update Date handling.
1 parent dc5cc1f commit c3ffc71

3 files changed

Lines changed: 13 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Three of the methods accept the following arguments for specifying custom reques
7878
The other two methods accept a `body` argument of type `B` that is serialized using `JSONEncoder`. JSON data is encoded and decoded using a date strategy of `millisecondsSince1970`.
7979

8080
## Query Arguments
81-
Any sendable value may be used as a query argument and will generally be encoded using its string representation. However, `Date` instances are first converted to a 64-bit integer value representing epoch time. Additionally, array instances represent multi-value parameters and behave similarly to `<select multiple>` tags in HTML forms.
81+
Any sendable value may be used as a query argument and will generally be encoded using its string representation. However, `Date` instances are encoded using `ISO8601DateFormatter`. Additionally, array instances represent multi-value parameters and behave similarly to `<select multiple>` tags in HTML forms.
8282

8383
The `undefined` property of the `WebServiceProxy` class can be used to represent unspecified or unknown argument values.
8484

Sources/Echo/WebServiceProxy.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ public class WebServiceProxy {
173173
responseHandler: @escaping ResponseHandler<T>) async throws -> T {
174174
var urlQueryItems: [URLQueryItem] = []
175175

176+
let iso8601DateFormatter = ISO8601DateFormatter()
177+
178+
iso8601DateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
179+
176180
for argument in arguments {
177181
if (argument.key.isEmpty) {
178182
throw WebServiceError(errorDescription: "Invalid key.", statusCode: 0)
@@ -185,7 +189,7 @@ public class WebServiceProxy {
185189

186190
let value: String
187191
if let date = element as? Date {
188-
value = String(describing: Int64(date.timeIntervalSince1970 * 1000))
192+
value = iso8601DateFormatter.string(from: date)
189193
} else {
190194
value = String(describing: element)
191195
}

Tests/EchoTests/EchoTests.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ final class EchoTests: XCTestCase {
5757
func testGet() async throws {
5858
let webServiceProxy = createWebServiceProxy()
5959

60-
let now = Date(timeIntervalSince1970: TimeInterval(UInt64(Date().timeIntervalSince1970 * 1000)))
60+
let now = Date()
61+
62+
let date = Date(timeIntervalSince1970: TimeInterval(UInt64(now.timeIntervalSince1970 / 1000) * 1000))
6163

6264
let result: Response = try await webServiceProxy.invoke(.get, path: "test", arguments: [
6365
"string": "héllo&gøod+bye?",
@@ -66,8 +68,8 @@ final class EchoTests: XCTestCase {
6668
"numbers": [1, 2, 2, 3, 3, 3],
6769
"flag": true,
6870
"dayOfWeek": DayOfWeek.monday,
69-
"date": now,
70-
"dates": [now],
71+
"date": date,
72+
"dates": [date],
7173
"instant": WebServiceProxy.undefined
7274
])
7375

@@ -77,8 +79,8 @@ final class EchoTests: XCTestCase {
7779
XCTAssert(result.numbers == [1, 2, 3])
7880
XCTAssert(result.flag == true)
7981
XCTAssert(result.dayOfWeek == .monday)
80-
XCTAssert(result.date == now)
81-
XCTAssert(result.dates == [now])
82+
XCTAssert(result.date == date)
83+
XCTAssert(result.dates == [date])
8284
}
8385

8486
func testPost() async throws {

0 commit comments

Comments
 (0)