-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from DataDog/buranmert/RUMM-437-iso8601-milise…
…conds-fix RUMM-437 Encoding doesn't ignore miliseconds anymore
- Loading branch information
Showing
12 changed files
with
118 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-2020 Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
|
||
extension ISO8601DateFormatter { | ||
static func `default`() -> ISO8601DateFormatter { | ||
let formatter = ISO8601DateFormatter() | ||
formatter.formatOptions.insert(.withFractionalSeconds) | ||
return formatter | ||
} | ||
|
||
static var encodingStrategy: JSONEncoder.DateEncodingStrategy { | ||
let formatter = Self.default() | ||
return .custom { date, encoder in | ||
var container = encoder.singleValueContainer() | ||
let formatted = formatter.string(from: date) | ||
try container.encode(formatted) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-2020 Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
|
||
extension JSONEncoder { | ||
static func `default`() -> JSONEncoder { | ||
let encoder = JSONEncoder() | ||
encoder.dateEncodingStrategy = ISO8601DateFormatter.encodingStrategy | ||
if #available(iOS 13.0, OSX 10.15, *) { | ||
encoder.outputFormatting = [.withoutEscapingSlashes] | ||
} | ||
return encoder | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-2020 Datadog, Inc. | ||
*/ | ||
|
||
import XCTest | ||
@testable import Datadog | ||
|
||
class DateFormatterTests: XCTestCase { | ||
func testISO8601FormatWithSubSecondPrecision() throws { | ||
let dateFormatter = ISO8601DateFormatter.default() | ||
|
||
let knownDate: Date = .mockDecember15th2019At10AMUTC(addingTimeInterval: 0.123) | ||
let formattedDate = dateFormatter.string(from: knownDate) | ||
|
||
XCTAssertEqual(formattedDate, "2019-12-15T10:00:00.123Z") | ||
} | ||
} | ||
|
||
class EncodingTests: XCTestCase { | ||
func testEncodingDateWithSubSecondPrecision() throws { | ||
let jsonEncoder = JSONEncoder.default() | ||
|
||
let knownDate: Date = .mockDecember15th2019At10AMUTC(addingTimeInterval: 0.123) | ||
let encodedKnownDate = try jsonEncoder.encode(knownDate) | ||
|
||
let jsonDecoder = JSONDecoder() | ||
let knownDateDecodedString = try jsonDecoder.decode(String.self, from: encodedKnownDate) | ||
|
||
XCTAssertEqual(knownDateDecodedString, "2019-12-15T10:00:00.123Z") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters