Skip to content
Myr

tz.ts

This file

Path

src/tz.ts

98 lines · 2.9 KiB · TypeScript

Last changed by

landed by oscabriel6a2b761

History of this file

Take it

landed by oscabriel6a2b761

lines 1–98
1/**
2 * Time zones. datekit keeps every date as an instant and works in UTC; a3 * time zone matters only when a date is shown, so this module turns an4 * instant into the wall-clock fields of an IANA zone (`Europe/Lisbon`), with5 * the zone's offset at that instant. The zone data is the runtime's own6 * (`Intl`), so datekit ships none.7 */8import { MS_PER_MINUTE, isoWeekday, utcMidnight } from "./calendar.ts";910/** An instant's wall-clock fields in one zone, and the zone's offset from UTC then. */11export interface ZonedParts {12 readonly year: number;13 /** 1 to 12. */14 readonly month: number;15 readonly day: number;16 /** 0 to 23. */17 readonly hour: number;18 readonly minute: number;19 readonly second: number;20 readonly millisecond: number;21 /** The ISO weekday: Monday is 1 and Sunday is 7. */22 readonly weekday: number;23 /** Minutes east of UTC: `+09:00` is 540. */24 readonly offset: number;25}2627/** An instant's fields in UTC. */28export const utcParts = (date: Date): ZonedParts => ({29 day: date.getUTCDate(),30 hour: date.getUTCHours(),31 millisecond: date.getUTCMilliseconds(),32 minute: date.getUTCMinutes(),33 month: date.getUTCMonth() + 1,34 offset: 0,35 second: date.getUTCSeconds(),36 weekday: isoWeekday(date),37 year: date.getUTCFullYear(),38});3940/** Whether the runtime knows `name` as a time zone. */41export const isTimeZone = (name: string): boolean => {42 try {43 new Intl.DateTimeFormat("en-US", { timeZone: name });4445 return true;46 } catch {47 return false;48 }49};5051const fieldsIn = (timeZone: string) =>52 new Intl.DateTimeFormat("en-US", {53 day: "numeric",54 hour: "numeric",55 hourCycle: "h23",56 minute: "numeric",57 month: "numeric",58 second: "numeric",59 timeZone,60 year: "numeric",61 });6263/**64 * An instant's fields in the IANA zone `timeZone`. Throws a `RangeError`65 * for a zone the runtime doesn't know.66 */67export const zonedParts = (date: Date, timeZone: string): ZonedParts => {68 const parts = new Map(69 fieldsIn(timeZone)70 .formatToParts(date)71 .map(({ type, value }) => [type, Number(value)]),72 );7374 const field = (type: Intl.DateTimeFormatPartTypes) => parts.get(type) ?? 0;75 const millisecond = date.getUTCMilliseconds();76 const wall = new Date(utcMidnight(field("year"), field("month"), field("day")));7778 // The wall clock read as if it were UTC, less the instant, is the offset.79 const wallTime =80 wall.getTime() +81 ((field("hour") * 60 + field("minute")) * 60 + field("second")) * 1000 +82 millisecond;8384 return {85 day: field("day"),86 hour: field("hour"),87 millisecond,88 minute: field("minute"),89 month: field("month"),90 offset: Math.round((wallTime - date.getTime()) / MS_PER_MINUTE),91 second: field("second"),92 weekday: isoWeekday(wall),93 year: field("year"),94 };95};9697/** The offset of `timeZone` from UTC at `date`, in minutes east of UTC. */98export const offsetIn = (date: Date, timeZone: string): number => zonedParts(date, timeZone).offset;