UNPKG

1.28 kBJavaScriptView Raw
1import { afterEach, beforeEach } from "./test/vitest";
2import { addLeadingZeros } from "./addLeadingZeros.js";
3import { setDefaultOptions } from "./defaultOptions.js";
4import sinon from "./test/sinon";
5
6export function assertType(_value) {}
7
8export function resetDefaultOptions() {
9 setDefaultOptions({});
10}
11
12// This makes sure we create the consistent offsets across timezones, no matter where these tests are ran.
13export function generateOffset(originalDate) {
14 // Add the timezone.
15 let offset = "";
16 const tzOffset = originalDate.getTimezoneOffset();
17
18 if (tzOffset !== 0) {
19 const absoluteOffset = Math.abs(tzOffset);
20 const hourOffset = addLeadingZeros(Math.trunc(absoluteOffset / 60), 2);
21 const minuteOffset = addLeadingZeros(absoluteOffset % 60, 2);
22 // If less than 0, the sign is +, because it is ahead of time.
23 const sign = tzOffset < 0 ? "+" : "-";
24
25 offset = `${sign}${hourOffset}:${minuteOffset}`;
26 } else {
27 offset = "Z";
28 }
29
30 return offset;
31}
32
33export function fakeDate(date) {
34 let clock;
35
36 function fakeNow(date) {
37 clock?.restore();
38 clock = sinon.useFakeTimers(+date);
39 }
40
41 beforeEach(() => {
42 fakeNow(+date);
43 });
44
45 afterEach(() => {
46 clock?.restore();
47 clock = undefined;
48 });
49
50 return { fakeNow };
51}