-
Notifications
You must be signed in to change notification settings - Fork 1
/
linode-near-location.js
83 lines (74 loc) · 2.42 KB
/
linode-near-location.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
(function (root, factory) {
// Support for AMD, CommonJS, and browser global.
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.linodeNearLocation = factory();
}
}(this, function () {
function stdTimezoneOffset(date) {
var jan = new Date(date.getFullYear(), 0, 1);
var jul = new Date(date.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
function dst(date) {
return date.getTimezoneOffset() < stdTimezoneOffset(date);
}
return function linodeNearLocation() {
// Add Daylight Savings Time detection, all most all dst countries are northern hemisphere
// http://javascript.about.com/library/bldst.htm
var today = new Date();
var timezoneOffset = today.getTimezoneOffset() / 60;
var linode_location;
if (dst(today)) {
timezoneOffset++; // add one if we are currently in daylight savings time
}
// convert to actual value because signs are reversed in getTimezoneOffset
timezoneOffset = Math.round(0 - timezoneOffset);
// refer to this picture: https://cloud.githubusercontent.com/assets/3926730/18216404/7a3862a4-7124-11e6-91a1-0d03b2c7f9d5.gif
switch (timezoneOffset) {
case -12:
case -11:
case -10:
case -9:
case -8:
case -7:
linode_location = 'fremont';
break;
case -6:
linode_location = 'dallas';
break;
case -5:
case -4:
case -3:
linode_location = 'newark';
break;
case -2:
case -1:
case 0:
linode_location = 'london';
break;
case 1:
case 2:
case 3:
case 4:
case 5:
linode_location = 'frankfurt';
break;
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
linode_location = 'singapore';
break;
default:
linode_location = 'newark';
}
return linode_location;
}
}));