forked from fingerprintjs/fingerprintjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimezone.ts
More file actions
29 lines (26 loc) · 1.12 KB
/
timezone.ts
File metadata and controls
29 lines (26 loc) · 1.12 KB
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
import { toFloat } from '../utils/data'
export default function getTimezone(): string {
const DateTimeFormat = window.Intl?.DateTimeFormat
if (DateTimeFormat) {
const timezone = new DateTimeFormat().resolvedOptions().timeZone
if (timezone) {
return timezone
}
}
// For browsers that don't support timezone names
// The minus is intentional because the JS offset is opposite to the real offset
const offset = -getTimezoneOffset()
return `UTC${offset >= 0 ? '+' : ''}${Math.abs(offset)}`
}
function getTimezoneOffset(): number {
const currentYear = new Date().getFullYear()
// The timezone offset may change over time due to daylight saving time (DST) shifts.
// The non-DST timezone offset is used as the result timezone offset.
// Since the DST season differs in the northern and the southern hemispheres,
// both January and July timezones offsets are considered.
return Math.max(
// `getTimezoneOffset` returns a number as a string in some unidentified cases
toFloat(new Date(currentYear, 0, 1).getTimezoneOffset()),
toFloat(new Date(currentYear, 6, 1).getTimezoneOffset()),
)
}