Skip to content

Commit 5a8661a

Browse files
committed
use double instead of
1 parent 0484a04 commit 5a8661a

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

Sources/Swift/Protocol/Codable/SentryCodable.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ import Foundation
33

44
func encodeToJSONData<T: Encodable>(data: T) throws -> Data {
55
let jsonEncoder = JSONEncoder()
6-
jsonEncoder.dateEncodingStrategy = .custom({ (date, encoder) in
7-
let formatter = sentryGetIso8601FormatterWithMillisecondPrecision()
8-
let dateString = formatter.string(from: date)
9-
10-
var container = encoder.singleValueContainer()
11-
try container.encode(dateString)
12-
})
6+
jsonEncoder.dateEncodingStrategy = .secondsSince1970
137
return try jsonEncoder.encode(data)
148
}
159

Tests/SentryTests/Protocol/Codable/SentryCodableTests.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import XCTest
44
class SentryCodableTests: XCTestCase {
55

66
func testEncodeToJSONData_EncodesCorrectDateFormat() throws {
7-
let date = Date(timeIntervalSince1970: 1_234_567_890)
7+
let date = Date(timeIntervalSince1970: 1_234_567_890.987_654)
88
let jsonData = try encodeToJSONData(data: date)
99
let json = String(data: jsonData, encoding: .utf8)
10-
XCTAssertEqual("\"2009-02-13T23:31:30.000Z\"", json)
10+
XCTAssertEqual("1234567890.987654", json)
1111
}
1212

1313
func testDecode_DecodesFromCorrectDateFormat() throws {
@@ -20,6 +20,16 @@ class SentryCodableTests: XCTestCase {
2020
XCTAssertEqual(date, Date(timeIntervalSince1970: 1_234_567_890))
2121
}
2222

23+
func testDecode_DecodesFromCorrectDateTimestampFormat() throws {
24+
let json = "1234567890.987654"
25+
guard let jsonData = json.data(using: .utf8) else {
26+
XCTFail("Could not convert json string to data.")
27+
return
28+
}
29+
let date: Date? = decodeFromJSONData(jsonData: jsonData)
30+
XCTAssertEqual(date, Date(timeIntervalSince1970: 1_234_567_890.987_654))
31+
}
32+
2333
func testDecodeWithEmptyData_ReturnsNil() {
2434
XCTAssertNil(decodeFromJSONData(jsonData: Data()) as Geo?)
2535
}

Tests/SentryTests/Protocol/SentryLogTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class SentryLogTests: XCTestCase {
66
// MARK: - Encoding Tests
77

88
private let log = SentryLog(
9-
timestamp: Date(timeIntervalSince1970: 1_234_567_890),
9+
timestamp: Date(timeIntervalSince1970: 1_234_567_890.987_654),
1010
traceId: SentryId(uuidString: "550e8400-e29b-41d4-a716-446655440000"),
1111
level: SentryLog.Level.info,
1212
body: "Test log message",
@@ -21,7 +21,7 @@ final class SentryLogTests: XCTestCase {
2121

2222
private let jsonData = Data("""
2323
{
24-
"timestamp": "2009-02-13T23:31:30.000Z",
24+
"timestamp": 1234567890.987654,
2525
"trace_id": "550e8400e29b41d4a716446655440000",
2626
"level": "trace",
2727
"body": "Trace message",
@@ -43,7 +43,7 @@ final class SentryLogTests: XCTestCase {
4343
let data = try encodeToJSONData(data: log)
4444
let json = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])
4545

46-
XCTAssertEqual(json["timestamp"] as? String, "2009-02-13T23:31:30.000Z")
46+
XCTAssertEqual(json["timestamp"] as? TimeInterval, 1234567890.987654)
4747
XCTAssertEqual(json["trace_id"] as? String, "550e8400e29b41d4a716446655440000")
4848
XCTAssertEqual(json["level"] as? String, "info")
4949
XCTAssertEqual(json["body"] as? String, "Test log message")
@@ -66,7 +66,7 @@ final class SentryLogTests: XCTestCase {
6666

6767
let log = try XCTUnwrap(decodeFromJSONData(jsonData: jsonData) as SentryLog?)
6868

69-
XCTAssertEqual(log.timestamp, Date(timeIntervalSince1970: 1_234_567_890))
69+
XCTAssertEqual(log.timestamp, Date(timeIntervalSince1970: 1_234_567_890.987_654))
7070
XCTAssertEqual(log.traceId.sentryIdString, "550e8400e29b41d4a716446655440000")
7171
XCTAssertEqual(log.level, .trace)
7272
XCTAssertEqual(log.body, "Trace message")

0 commit comments

Comments
 (0)