From 5dd207357ac8452c27158ca8a19aa76668c69054 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Fri, 5 Apr 2024 15:00:00 +0200 Subject: [PATCH] Explicit UTC settings and TZ tests --- Sources/Confidence/ConfidenceValue.swift | 1 + Tests/ConfidenceTests/ConfidenceValueTests.swift | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/Confidence/ConfidenceValue.swift b/Sources/Confidence/ConfidenceValue.swift index afbe00c0..d4536dd9 100644 --- a/Sources/Confidence/ConfidenceValue.swift +++ b/Sources/Confidence/ConfidenceValue.swift @@ -220,6 +220,7 @@ extension ConfidenceValueInternal { } case .timestamp(let date): let isoFormatter = ISO8601DateFormatter() + isoFormatter.timeZone = TimeZone.current let formattedDate = isoFormatter.string(from: date) try container.encode(formattedDate) case .structure(let structure): diff --git a/Tests/ConfidenceTests/ConfidenceValueTests.swift b/Tests/ConfidenceTests/ConfidenceValueTests.swift index f315b577..ecbedede 100644 --- a/Tests/ConfidenceTests/ConfidenceValueTests.swift +++ b/Tests/ConfidenceTests/ConfidenceValueTests.swift @@ -101,9 +101,10 @@ final class ConfidenceConfidenceValueTests: XCTestCase { func testEncodeDecode() throws { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" - formatter.timeZone = TimeZone(abbreviation: "UTC") - let date = try XCTUnwrap(formatter.date(from: "2022-01-01 12:00:00")) - let dateComponents = DateComponents(year: 2024, month: 4, day: 3) + formatter.timeZone = TimeZone(abbreviation: "EDT") // Verify TimeZone conversion + let date = try XCTUnwrap(formatter.date(from: "2024-04-05 16:00:00")) + var dateComponents = DateComponents(year: 2024, month: 4, day: 3) + let value = ConfidenceValue(structure: ([ "bool": ConfidenceValue(boolean: true), @@ -119,6 +120,10 @@ final class ConfidenceConfidenceValueTests: XCTestCase { let encoder = JSONEncoder() encoder.outputFormatting = .sortedKeys let resultString = String(data: try encoder.encode(value), encoding: .utf8) + + let isoFormatter = ISO8601DateFormatter() + isoFormatter.timeZone = TimeZone.current + let expectedSerializedTimestamp = isoFormatter.string(from: date) let expectedString = """ {\"bool\":true, \"date\":\"2024-04-03\", @@ -128,9 +133,10 @@ final class ConfidenceConfidenceValueTests: XCTestCase { \"null\":null, \"string\":\"value\", \"structure\":{\"int\":5}, - \"timestamp\":\"2022-01-01T12:00:00Z\"} + \"timestamp\":\"\(expectedSerializedTimestamp)\"} """.replacingOccurrences(of: "\n", with: "") // Newlines were added for readability + XCTAssertTrue(expectedSerializedTimestamp.starts(with: "2024-04-05T22:00:00")) XCTAssertEqual(resultString, expectedString) } }