39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { format, addHours } from "date-fns";
|
|
import { TZDate } from "@date-fns/tz";
|
|
import { UTCDate, utc } from "@date-fns/utc";
|
|
import spacetime from "spacetime";
|
|
|
|
export function dateFromInput(input: { dateQuery: string, timezone: string }) {
|
|
console.log(`Looking for ${input.dateQuery} in ${input.timezone}`);
|
|
|
|
|
|
let t: string;
|
|
if (input.dateQuery == "today") {
|
|
t = spacetime(input.dateQuery, input.timezone).format("yyyy-MM-dd");
|
|
return t;
|
|
}
|
|
else {
|
|
t = new UTCDate(input.dateQuery);
|
|
return format(t, "yyyy-MM-dd", { in: utc });
|
|
}
|
|
|
|
}
|
|
|
|
function generateHour(d, t) {
|
|
const dt: TZDate = addHours(d, t);
|
|
console.log(dt);
|
|
return {
|
|
date: format(dt, 'yyyy-MM-dd'),
|
|
time: parseInt(format(dt, 'H')),
|
|
};
|
|
}
|
|
|
|
export function hoursListInUTC(input: { dateQuery: string, timezone: string }) {
|
|
const midnight = spacetime(input.dateQuery, input.timezone).time('00:00');
|
|
const hours = Array.from({ length: 24 }, function (_, i) {
|
|
const utcMoment = midnight.add(i, 'hour').goto('UTC');
|
|
return { date: utcMoment.format('hh'), time: utcMoment.hour() };
|
|
});
|
|
return hours;
|
|
}
|