-
Notifications
You must be signed in to change notification settings - Fork 835
/
helpers.js
86 lines (71 loc) · 2.06 KB
/
helpers.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"use strict";
var moment = require('../../index');
var getTimezoneOffset = Date.prototype.getTimezoneOffset;
var toTimeString = Date.prototype.toTimeString;
var parent = (typeof window !== 'undefined' && window) || (typeof global !== 'undefined' && global);
var oldIntl = parent.Intl;
function getUTCOffset (m) {
if (m.utcOffset !== undefined) {
return m.utcOffset();
} else {
return -m.zone();
}
}
function testYear(test, name, expected) {
var len = expected.length,
i,
date, time, abbr, offset, m;
test.expect(len * 3);
for (i = 0; i < len; i++) {
date = expected[i][0];
time = expected[i][1];
abbr = expected[i][2];
offset = expected[i][3];
m = moment(date).tz(name);
test.equal(m.format("HH:mm:ss"), time, date + ' should be ' + time + ' ' + abbr);
test.equal(getUTCOffset(m), -offset, date + ' should be ' + offset + ' minutes offset in ' + abbr);
test.equal(m.zoneAbbr(), abbr, date + ' should be ' + abbr);
}
test.done();
}
function mockTimezoneOffset(name) {
var zone = moment.tz.zone(name);
Date.prototype.getTimezoneOffset = function () {
return zone.utcOffset(+this);
};
}
function mockToTimeString(name, format) {
Date.prototype.toTimeString = function () {
return moment.tz(+this, name).format(format || 'HH:mm:ss [GMT]ZZ');
};
}
function testGuess(test, name, mostPopulatedFor) {
parent.Intl = undefined;
if (mostPopulatedFor.offset) {
mockTimezoneOffset(name);
mockToTimeString(name);
test.equal(moment.tz.guess(true), name);
}
if (mostPopulatedFor.abbr) {
mockTimezoneOffset(name);
mockToTimeString(name, 'HH:mm:ss [GMT]ZZ (z)');
test.equal(moment.tz.guess(true), name);
}
Date.prototype.getTimezoneOffset = getTimezoneOffset;
Date.prototype.toTimeString = toTimeString;
parent.Intl = oldIntl;
test.done();
}
module.exports = {
makeTestYear : function (name, expected) {
return function (test) {
testYear(test, name, expected);
};
},
makeTestGuess : function (name, mostPopulatedFor) {
return function (test) {
testGuess(test, name, mostPopulatedFor);
};
},
getUTCOffset : getUTCOffset
};