Skip to content

Commit df93071

Browse files
authored
Use correct calculation for Date's current time (#734)
* Use correct calculation for Date's current time * Update test to also check upper bound
1 parent b58b051 commit df93071

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Sources/FoundationEssentials/Date.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ extension Date {
229229
li.LowPart = ft.dwLowDateTime
230230
li.HighPart = ft.dwHighDateTime
231231
// FILETIME represents 100-ns intervals since January 1, 1601 (UTC)
232-
return TimeInterval((li.QuadPart - 1164447360_000_000) / 1_000_000_000)
232+
return TimeInterval(Double(li.QuadPart) / 10_000_000.0 - Self.timeIntervalBetween1601AndReferenceDate)
233233
#else
234234
var ts: timespec = timespec()
235235
clock_gettime(CLOCK_REALTIME, &ts)

Tests/FoundationEssentialsTests/DateTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ final class DateTests : XCTestCase {
127127
XCTAssertEqual("<description unavailable>", date.description)
128128
#endif
129129
}
130+
131+
func testNowIsAfterReasonableDate() {
132+
let date = Date.now
133+
XCTAssert(date.timeIntervalSinceReferenceDate > 742100000.0) // "2024-07-08T02:53:20Z"
134+
XCTAssert(date.timeIntervalSinceReferenceDate < 3896300000.0) // "2124-06-21T01:33:20Z"
135+
}
130136
}
131137

132138
// MARK: - Bridging

0 commit comments

Comments
 (0)