Reputation: 3289
I'm having trouble finding a package that allows me to query the timezone information for any timezone at any point in time. Or at least the next 10 years or so. Historical values are not of interest in my use case.
Ideally, I would like to have an API like:
var tzInfo = require(tz-info);
var utcOffset = tzInfo.getOffset('Europe/Stockholm', new Date()); //query the current offset from UTC
var allTimezones = tzInfo.getAllTimezoneNames();
I have searched but came up short. Most libraries like moment-timezone seem very focused on expressing one time in different timezones. And that is different from my use case.
In linux you can do:
#get all timezone names
cd /usr/share/zoneinfo ; find
#get all offset changes
zdump -v <timezone>
I guess that is an option and parse the output from zdump to build my own database. But I rather not. Platform independence is very nice to have. But not a deal breaker.
Upvotes: 0
Views: 399
Reputation: 241900
Moment-timezone absolutely can fit this requirement.
var moment = require('moment-timezone');
// query the current offset from UTC
var utcOffset = moment.tz('Europe/Stockholm').utcOffset();
// query the offset from UTC for a specific point in time
// (input can be string, date, moment, timestamp, etc. see the docs)
var utcOffset = moment.tz(input, 'Europe/Stockholm').utcOffset();
This works because moment-timezone is an extension to moment.js. All of the moment.js functions are available, including the utcOffset
function.
And yes, you can also get the names of the time zones:
var allTimezones = moment.tz.names();
However, I'll highlight one flaw:
... Or at least the next 10 years or so ...
There is no system on the planet that can accurately give you a prediction of time zones 10 years into the future. Governments change their minds constantly, and systems have to be updated accordingly. You should plan for this.
The information in the TZDB (as used by both Linux, moment-timezone, and many others) is only as accurate as the current information given by the governments of the world. A prediction 10 years into the future is most certainly going to need to be re-evaluated multiple times.
Upvotes: 2
Reputation: 1790
Have you looked at tz node module here? And its source here.
It exports an object with all possible time zone 'offsets' as keys, and array of applicable timezones as value. You could simply do var tz = requrie('tz');
, iterate over all keys in tz
and then over array associated with the key, plucking out name
from each object.
The resulting list should give you list of all possible timezones.
Upvotes: 0