Skip to content

Commit

Permalink
Explicit UTC settings and TZ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 5, 2024
1 parent a3a6b4d commit 6442a73
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Sources/Confidence/ConfidenceValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,15 @@ 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 {
throw ConfidenceError.internalError(message: "Could not create date from components")
}
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):
Expand Down
11 changes: 6 additions & 5 deletions Tests/ConfidenceTests/ConfidenceValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand Down

0 comments on commit 6442a73

Please sign in to comment.