From 6442a73cd1cc89a9af067b03f3913172475010e0 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 | 2 ++ Tests/ConfidenceTests/ConfidenceValueTests.swift | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Sources/Confidence/ConfidenceValue.swift b/Sources/Confidence/ConfidenceValue.swift index afbe00c0..5bfef721 100644 --- a/Sources/Confidence/ConfidenceValue.swift +++ b/Sources/Confidence/ConfidenceValue.swift @@ -213,6 +213,7 @@ extension ConfidenceValueInternal { case .date(let dateComponents): let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" + dateFormatter.timeZone = TimeZone(identifier: "UTC") if let date = Calendar.current.date(from: dateComponents) { try container.encode(dateFormatter.string(from: date)) } else { @@ -220,6 +221,7 @@ extension ConfidenceValueInternal { } case .timestamp(let date): let isoFormatter = ISO8601DateFormatter() + isoFormatter.timeZone = TimeZone(identifier: "UTC") 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..0e597df4 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) + dateComponents.timeZone = TimeZone(abbreviation: "JST") // Verify TimeZone conversion let value = ConfidenceValue(structure: ([ "bool": ConfidenceValue(boolean: true), @@ -121,14 +122,14 @@ final class ConfidenceConfidenceValueTests: XCTestCase { let resultString = String(data: try encoder.encode(value), encoding: .utf8) let expectedString = """ {\"bool\":true, - \"date\":\"2024-04-03\", + \"date\":\"2024-04-02\", \"double\":4.5, \"int\":3, \"list\":[3,5], \"null\":null, \"string\":\"value\", \"structure\":{\"int\":5}, - \"timestamp\":\"2022-01-01T12:00:00Z\"} + \"timestamp\":\"2024-04-05T20:00:00Z\"} """.replacingOccurrences(of: "\n", with: "") // Newlines were added for readability XCTAssertEqual(resultString, expectedString)