The UTC offset (ms, east-positive) of `timeZone` at a given instant.
(instant: Date, timeZone: string)
| 123 | |
| 124 | /** The UTC offset (ms, east-positive) of `timeZone` at a given instant. */ |
| 125 | function timezoneOffsetMs(instant: Date, timeZone: string): number { |
| 126 | const parts = new Intl.DateTimeFormat('en-US', { |
| 127 | timeZone, |
| 128 | hourCycle: 'h23', |
| 129 | year: 'numeric', |
| 130 | month: '2-digit', |
| 131 | day: '2-digit', |
| 132 | hour: '2-digit', |
| 133 | minute: '2-digit', |
| 134 | second: '2-digit', |
| 135 | }).formatToParts(instant) |
| 136 | const get = (type: string) => Number(parts.find((p) => p.type === type)?.value) |
| 137 | const asUtc = Date.UTC( |
| 138 | get('year'), |
| 139 | get('month') - 1, |
| 140 | get('day'), |
| 141 | get('hour'), |
| 142 | get('minute'), |
| 143 | get('second') |
| 144 | ) |
| 145 | return asUtc - instant.getTime() |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Resolves a naive `yyyy-MM-ddTHH:mm[:ss]` wall-clock — interpreted as local |
no test coverage detected