All files / test/demo/orchestrator/clock clockGet.ts

20.45% Statements 9/44
100% Branches 0/0
0% Functions 0/2
20.45% Lines 9/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 451x 1x 1x 1x             1x             1x 1x 1x                                               1x  
import {type IMeta, handler} from '@feasibleone/blong';
 
type ApiHandler = (params: {location?: string; area?: string}) => Promise<object>;
 
function dstOffset(date: Date): number {
    const jan = new Date(date.getFullYear(), 0, 1);
    const jul = new Date(date.getFullYear(), 6, 1);
    const stdTimezoneOffset = Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
    return stdTimezoneOffset - date.getTimezoneOffset();
}
 
function weekNumber(date: Date): number {
    const d = new Date(+date);
    d.setHours(0, 0, 0, 0);
    d.setDate(d.getDate() + 4 - (d.getDay() || 7));
    return Math.ceil(((d.getTime() - new Date(d.getFullYear(), 0, 1).getTime()) / 8.64e7 + 1) / 7);
}
 
export default handler(
    () =>
        async function clockGet(
            params: Parameters<ApiHandler>[0],
            $meta: IMeta,
        ): ReturnType<ApiHandler> {
            const now = new Date();
            const dst_offset = dstOffset(now);
            return {
                abbreviation: Intl.DateTimeFormat().resolvedOptions().timeZone,
                client_ip: $meta.ipAddress,
                datetime: now.toString(),
                day_of_week: now.getDay(),
                day_of_year: now.getDate(),
                dst: dst_offset !== 0,
                dst_offset,
                timezone: now
                    .toLocaleDateString(undefined, {day: '2-digit', timeZoneName: 'long'})
                    .substring(4),
                unixtime: now.getTime(),
                utc_datetime: now.toISOString(),
                utc_offset: now.getTimezoneOffset(),
                week_number: weekNumber(now),
            };
        },
);