Skip to content

Use correct calculation for Date's current time #734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ extension Date {
li.LowPart = ft.dwLowDateTime
li.HighPart = ft.dwHighDateTime
// FILETIME represents 100-ns intervals since January 1, 1601 (UTC)
return TimeInterval((li.QuadPart - 1164447360_000_000) / 1_000_000_000)
return TimeInterval(Double(li.QuadPart) / 10_000_000.0 - Self.timeIntervalBetween1601AndReferenceDate)
#else
var ts: timespec = timespec()
clock_gettime(CLOCK_REALTIME, &ts)
Expand Down
6 changes: 6 additions & 0 deletions Tests/FoundationEssentialsTests/DateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ final class DateTests : XCTestCase {
XCTAssertEqual("<description unavailable>", date.description)
#endif
}

func testNowIsAfterReasonableDate() {
let date = Date.now
XCTAssert(date.timeIntervalSinceReferenceDate > 742100000.0) // "2024-07-08T02:53:20Z"
XCTAssert(date.timeIntervalSinceReferenceDate < 3896300000.0) // "2124-06-21T01:33:20Z"
}
}

// MARK: - Bridging
Expand Down