1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-27 11:02:16 +01:00
unleash.unleash/src/lib/util/time-utils.ts
Christopher Kolstad efcf04487d
chore: make it build with strict null checks set to true (#9554)
As part of preparation for ESM and node/TSC updates, this PR will make
Unleash build with strictNullChecks set to true, since that's what's in
our tsconfig file. Hence, this PR also removes the `--strictNullChecks
false` flag in our compile tasks in package.json.

TL;DR - Clean up your code rather than turning off compiler security
features :)
2025-03-19 10:01:49 +01:00

29 lines
725 B
TypeScript

import { endOfDay, startOfHour, subDays, subHours } from 'date-fns';
export interface HourBucket {
timestamp: Date;
}
export function generateHourBuckets(hours: number): HourBucket[] {
const start = startOfHour(new Date());
const result: HourBucket[] = [];
for (let i = 0; i < hours; i++) {
result.push({ timestamp: subHours(start, i) });
}
return result;
}
// Generate last x days starting from end of yesterday
export function generateDayBuckets(days: number): HourBucket[] {
const start = endOfDay(subDays(new Date(), 1));
const result: HourBucket[] = [];
for (let i = 0; i < days; i++) {
result.push({ timestamp: subDays(start, i) });
}
return result;
}