-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimezoned.js
125 lines (118 loc) · 4.53 KB
/
timezoned.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// https://developers.google.com/maps/documentation/timezone
var gtzUrl = 'https://maps.googleapis.com/maps/api/timezone/json';
var baseParamsTZ = {sensor: false};
function callTZAPI(data, callback) {
data = _.extend({}, baseParamsTZ, data);
if (callback) {
return HTTP.get(gtzUrl, {params: data}, function (error, tz) {
callback(error, tz.data && tz.data.timeZoneId);
});
} else {
var tz = HTTP.get(gtzUrl, {params: data});
return tz.data && tz.data.timeZoneId;
}
}
function checkCallback(cb) {
if (Meteor.isClient && typeof cb !== "function") {
throw new Error("TimeZoned: You must provide a callback on the client");
}
}
if (Meteor.isServer) {
Meteor.methods({
"_TimeZoned_getTimeZoneForAddress": function (address) {
this.unblock();
return TimeZoned.getTimeZoneForAddress(address);
}
});
}
TimeZoned = {
// Can add supported params like key, language, client, or signature here
config: function (options) {
options = options || {};
_.extend(baseParamsTZ, options.gtzParams);
},
getTimeZoneForAddress: function getTimeZoneForAddress(address, callback) {
checkCallback(callback);
// On the client, we actually proxy through the server
if (Meteor.isClient) {
Meteor.call("_TimeZoned_getTimeZoneForAddress", address, callback);
return;
}
var geo = new GeoCoder();
if (callback) {
return geo.geocode(address, function (error, result) {
if (error) {
callback(error);
} else {
if (!result || !result[0] || !result[0].latitude || !result[0].longitude) {
callback(new Error('TimeZoned: Unable to geocode "' + address + '"'));
} else {
TimeZoned.getTimeZoneForCoords(result[0].latitude, result[0].longitude, callback);
}
}
});
} else {
var result = geo.geocode(address);
if (!result || !result[0] || !result[0].latitude || !result[0].longitude) {
throw new Error('TimeZoned: Unable to geocode "' + address + '"');
}
return TimeZoned.getTimeZoneForCoords(result[0].latitude, result[0].longitude);
}
},
getTimeZoneForCoords: function getTimeZoneForCoords(lat, lon, callback) {
checkCallback(callback);
var timestamp = Math.round(new Date().getTime()/1000.0);
return callTZAPI({ timestamp: timestamp, location: lat+','+lon }, callback);
},
// http://www.w3.org/TR/html-markup/datatypes.html#form.data.datetime-local
getOffsetStringForTimeZone: function getOffsetStringForTimeZone(localDateTimeString, timezone) {
return moment.tz(localDateTimeString, timezone).format("ZZ");
},
// http://www.w3.org/TR/html-markup/datatypes.html#form.data.datetime-local
getDateObjectForTimeZone: function getDateObjectForTimeZone(localDateTimeString, timezone) {
var offset = TimeZoned.getOffsetStringForTimeZone(localDateTimeString, timezone);
return new Date(localDateTimeString + offset);
},
// http://www.w3.org/TR/html-markup/datatypes.html#form.data.datetime-local
getDateObjectForAddress: function getDateObjectForAddress(localDateTimeString, address, callback) {
checkCallback(callback);
if (callback) {
TimeZoned.getTimeZoneForAddress(address, function (error, timezone) {
if (error) {
callback(error);
} else if (!timezone) {
callback(new Error("getDateObjectForAddress: Couldn't get timezone for address. Possibly due to Google API throttling."));
} else {
callback(null, TimeZoned.getDateObjectForTimeZone(localDateTimeString, timezone));
}
});
} else {
var timezone = TimeZoned.getTimeZoneForAddress(address);
if (!timezone) {
throw new Error("getDateObjectForAddress: Couldn't get timezone for address. Possibly due to Google API throttling.");
}
return TimeZoned.getDateObjectForTimeZone(localDateTimeString, timezone);
}
},
// http://www.w3.org/TR/html-markup/datatypes.html#form.data.datetime-local
getDateObjectForCoords: function getDateObjectForCoords(localDateTimeString, lat, lon, callback) {
checkCallback(callback);
if (callback) {
TimeZoned.getTimeZoneForCoords(lat, lon, function (error, timezone) {
if (error) {
callback(error);
} else if (!timezone) {
callback(new Error("getDateObjectForCoords: Couldn't get timezone for address. Possibly due to Google API throttling."));
} else {
callback(null, TimeZoned.getDateObjectForTimeZone(localDateTimeString, timezone));
}
});
} else {
var timezone = TimeZoned.getTimeZoneForCoords(lat, lon);
if (!timezone) {
throw new Error("getDateObjectForCoords: Couldn't get timezone for address. Possibly due to Google API throttling.");
}
return TimeZoned.getDateObjectForTimeZone(localDateTimeString, timezone);
}
}
};