-
Notifications
You must be signed in to change notification settings - Fork 2
/
getTimezoneOffset.js
45 lines (35 loc) · 962 Bytes
/
getTimezoneOffset.js
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
45
var locale = 'en-US';
var us_re = /(\d+).(\d+).(\d+),?\s+(\d+).(\d+)(.(\d+))?/;
var format = {
timeZone: "UTC",
hour12: false,
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric'
};
var utc_f = new Intl.DateTimeFormat(locale, format );
function parseDate( date_str ) {
date_str = date_str.replace(/[\u200E\u200F]/g, '');
return [].slice.call(us_re.exec( date_str ), 1)
.map( Math.floor );
}
function diffMinutes( d1, d2 ) {
var d1h = d1[3] === 24 ? 0: d1[3];
var d2h = d2[3] === 24 ? 0: d2[3];
var day = d1[1] - d2[1];
var hour = d1h - d2h;
var min = d1[4] - d2[4];
if( day > 15 ) day = -1;
if( day < -15 ) day = 1;
return 60 * ( 24 * day + hour ) + min;
}
module.exports = function getTimezoneOffset( tz_str, date ) {
format.timeZone = tz_str;
var loc_f = new Intl.DateTimeFormat(locale, format );
return diffMinutes(
parseDate( utc_f.format( date )),
parseDate( loc_f.format( date ))
);
}