-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
snapWhenToSteps: support snapping forward
- Loading branch information
Showing
1 changed file
with
11 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import {strictEqual} from 'node:assert' | ||
|
||
// snap `when` to 10s steps for a better cache hit ratio | ||
// Note: With most HAFAs instances, this will yield slightly different results because it all it will return results <=10s in the past. | ||
const snapWhenTo10sSteps = () => { | ||
const now = Date.now() | ||
return now - now % 10 * 1000 | ||
const snapWhenTo10sSteps = (t = Date.now(), snapForward = false) => { | ||
t -= t % (10 * 1000) | ||
if (snapForward) t += 10 * 1000 | ||
return t | ||
} | ||
|
||
strictEqual(snapWhenTo10sSteps(Date.parse('2024-01-29T16:10:50.000+01:00')), Date.parse('2024-01-29T16:10:50+01:00')) | ||
strictEqual(snapWhenTo10sSteps(Date.parse('2024-01-29T16:10:57.812+01:00')), Date.parse('2024-01-29T16:10:50+01:00')) | ||
strictEqual(snapWhenTo10sSteps(Date.parse('2024-01-29T16:11:00.000+01:00')), Date.parse('2024-01-29T16:11:00+01:00')) | ||
strictEqual(snapWhenTo10sSteps(Date.parse('2024-01-29T16:10:57.812+01:00'), true), Date.parse('2024-01-29T16:11:00+01:00')) | ||
|
||
export { | ||
snapWhenTo10sSteps as snapWhenToSteps, | ||
} |