mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-18 21:04:23 +01:00
Merge remote-tracking branch 'origin/develop' into actor
This commit is contained in:
23
src/utils/time.test.ts
Normal file
23
src/utils/time.test.ts
Normal 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()),
|
||||
);
|
||||
});
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user