Skip to content

Commit

Permalink
Merge pull request #9 from juri/nanonseconds2
Browse files Browse the repository at this point in the history
Fix nanosecond helper
  • Loading branch information
juri authored Jul 31, 2023
2 parents 0612c43 + 4389c62 commit 5ec33b3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
6 changes: 1 addition & 5 deletions Sources/Parse3339/Parse3339.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ public struct Parts {

/// The fractional second value in nanoseconds.
public var nanosecond: Int {
let multiplier = power10(self.secondFractionDigits)
let nanoZeroes: Int = 9
let leading = self.secondFraction * multiplier
let missingDigits = nanoZeroes - self.secondFractionDigits - 1
let nanoMultiplier = power10(missingDigits)
return leading * nanoMultiplier
return self.secondFraction * power10(nanoZeroes - self.secondFractionDigits)
}

/// Time zone in seconds.
Expand Down
60 changes: 60 additions & 0 deletions Tests/Parse3339Tests/Parse3339Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,66 @@ final class Parse3339Tests: XCTestCase {
XCTAssertEqual(parsed.secondFractionDigits, 1)
XCTAssertEqual(parsed.zone, 0)
XCTAssertEqual(parsed.nanosecond, 200_000_000)

let unixTime = 1_688_458_885.2
XCTAssertEqual(parsed.dateComponents.date!.timeIntervalSince1970, unixTime, accuracy: 0.1)
}

func testFullZZoneWithMillisecondsSuccessful() throws {
let s = "2023-07-04T08:21:25.295Z"
let parsed = try XCTUnwrap(parse(s))

XCTAssertEqual(parsed.year, 2023)
XCTAssertEqual(parsed.month, 7)
XCTAssertEqual(parsed.day, 4)
XCTAssertEqual(parsed.hour, 8)
XCTAssertEqual(parsed.minute, 21)
XCTAssertEqual(parsed.second, 25)
XCTAssertEqual(parsed.secondFraction, 295)
XCTAssertEqual(parsed.secondFractionDigits, 3)
XCTAssertEqual(parsed.zone, 0)
XCTAssertEqual(parsed.nanosecond, 295_000_000)

let unixTime = 1_688_458_885.295
XCTAssertEqual(parsed.dateComponents.date!.timeIntervalSince1970, unixTime, accuracy: 0.001)
}

func testFullZZoneWithMicrosecondsSuccessful() throws {
let s = "2023-07-04T08:21:25.295729Z"
let parsed = try XCTUnwrap(parse(s))

XCTAssertEqual(parsed.year, 2023)
XCTAssertEqual(parsed.month, 7)
XCTAssertEqual(parsed.day, 4)
XCTAssertEqual(parsed.hour, 8)
XCTAssertEqual(parsed.minute, 21)
XCTAssertEqual(parsed.second, 25)
XCTAssertEqual(parsed.secondFraction, 295_729)
XCTAssertEqual(parsed.secondFractionDigits, 6)
XCTAssertEqual(parsed.zone, 0)
XCTAssertEqual(parsed.nanosecond, 295_729_000)

let unixTime = 1_688_458_885.295729
XCTAssertEqual(parsed.dateComponents.date!.timeIntervalSince1970, unixTime, accuracy: 0.000001)
}

func testFullZZoneWithNanosecondsSuccessful() throws {
let s = "2023-07-04T08:21:25.295729572Z"
let parsed = try XCTUnwrap(parse(s))

XCTAssertEqual(parsed.year, 2023)
XCTAssertEqual(parsed.month, 7)
XCTAssertEqual(parsed.day, 4)
XCTAssertEqual(parsed.hour, 8)
XCTAssertEqual(parsed.minute, 21)
XCTAssertEqual(parsed.second, 25)
XCTAssertEqual(parsed.secondFraction, 295_729_572)
XCTAssertEqual(parsed.secondFractionDigits, 9)
XCTAssertEqual(parsed.zone, 0)
XCTAssertEqual(parsed.nanosecond, 295_729_572)

let unixTime = 1_688_458_885.295729572
XCTAssertEqual(parsed.dateComponents.date!.timeIntervalSince1970, unixTime, accuracy: 0.000000001)
}

// MARK: Without fractions
Expand Down

0 comments on commit 5ec33b3

Please sign in to comment.