Merge remote-tracking branch 'origin/develop' into actor

This commit is contained in:
Alex Gleason
2023-07-27 09:57:52 -05:00
18 changed files with 328 additions and 14 deletions

23
src/utils/time.test.ts Normal file
View File

@@ -0,0 +1,23 @@
import { assertEquals } from '@/deps-test.ts';
import { generateDateRange } from './time.ts';
Deno.test('generateDateRange', () => {
const since = new Date('2023-07-03T16:30:00.000Z');
const until = new Date('2023-07-07T09:01:00.000Z');
const expected = [
new Date('2023-07-03T00:00:00.000Z'),
new Date('2023-07-04T00:00:00.000Z'),
new Date('2023-07-05T00:00:00.000Z'),
new Date('2023-07-06T00:00:00.000Z'),
new Date('2023-07-07T00:00:00.000Z'),
];
const result = generateDateRange(since, until);
assertEquals(
result.map((d) => d.getTime()),
expected.map((d) => d.getTime()),
);
});

View File

@@ -9,4 +9,24 @@ const Time = {
years: (y: number) => y * Time.days(365),
};
export { Time };
/** Strips the time off the date, giving 12am UTC. */
function stripTime(date: Date): Date {
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
}
/** Strips times off the dates and generates all 24h intervals between them, inclusive of both inputs. */
function generateDateRange(since: Date, until: Date): Date[] {
const dates = [];
const sinceDate = stripTime(since);
const untilDate = stripTime(until);
while (sinceDate <= untilDate) {
dates.push(new Date(sinceDate));
sinceDate.setUTCDate(sinceDate.getUTCDate() + 1);
}
return dates;
}
export { generateDateRange, stripTime, Time };