fingerprintjs
29 строк · 1.1 Кб
1import { toFloat } from '../utils/data'2
3export default function getTimezone(): string {4const DateTimeFormat = window.Intl?.DateTimeFormat5if (DateTimeFormat) {6const timezone = new DateTimeFormat().resolvedOptions().timeZone7if (timezone) {8return timezone9}10}11
12// For browsers that don't support timezone names13// The minus is intentional because the JS offset is opposite to the real offset14const offset = -getTimezoneOffset()15return `UTC${offset >= 0 ? '+' : ''}${offset}`16}
17
18function getTimezoneOffset(): number {19const currentYear = new Date().getFullYear()20// The timezone offset may change over time due to daylight saving time (DST) shifts.21// The non-DST timezone offset is used as the result timezone offset.22// Since the DST season differs in the northern and the southern hemispheres,23// both January and July timezones offsets are considered.24return Math.max(25// `getTimezoneOffset` returns a number as a string in some unidentified cases26toFloat(new Date(currentYear, 0, 1).getTimezoneOffset()),27toFloat(new Date(currentYear, 6, 1).getTimezoneOffset()),28)29}
30