@beignet/core
Version:
Core framework primitives for Beignet
41 lines • 1.12 kB
JavaScript
function toDate(value) {
return value instanceof Date ? new Date(value.getTime()) : new Date(value);
}
/**
* Create a clock backed by `new Date()`.
*
* Use this as the default production `ClockPort` when the app does not need a
* provider-specific time source.
*
* @returns A clock that reads the current system time.
*/
export function createSystemClock() {
return {
now: () => new Date(),
};
}
/**
* Create a mutable clock for deterministic tests.
*
* @example
* ```ts
* const clock = createFrozenClock("2026-01-01T00:00:00.000Z");
* clock.advance(1000);
* ```
*
* @param initial - Initial time for the clock. Defaults to Unix epoch.
* @returns A clock whose time can be replaced or advanced.
*/
export function createFrozenClock(initial = new Date(0)) {
let current = toDate(initial);
return {
now: () => new Date(current.getTime()),
setNow: (value) => {
current = toDate(value);
},
advance: (milliseconds) => {
current = new Date(current.getTime() + milliseconds);
},
};
}
//# sourceMappingURL=clock.js.map