js-joda / js-joda-timezone

!This package is deprecated!
Other
27 stars 4 forks source link

Timezone table #38

Closed ovidius72 closed 6 years ago

ovidius72 commented 6 years ago

Excellent work. Is it possible to get a list of all the available time zones with their offsets like in this page http://joda-time.sourceforge.net/timezones.html ? I'm able to get the list of timezone with JSJoda.ZoneId.getAvailableZoneIds() but I'd like to know how to show the offset as well.

Thanks.

phueper commented 6 years ago

Hi @ovidius72 ,

thanks for using js-joda and js-joda-timezone... i am not 100% sure about my solution below, @pithu maybe you can check it?

I think what you can do is:

const joda = require('js-joda').use(require('js-joda-timezone'));

const { ChronoField, Instant, ZoneId, ZoneOffset } = joda;

ZoneId.getAvailableZoneIds().forEach(function(zoneId) {
    console.log(zoneId, ZoneId.of(zoneId).rules().offset(Instant.now()).get(ChronoField.OFFSET_SECONDS))
})

This gives the offset in seconds for the current Instant. The offset maybe different depending on the Instant that you apply it on (e.g. wether it is Daylight Savings Time or not) ... so this is why you need to calculate it for an Instant (like .now()) i think

Hope this helps you?

phueper commented 6 years ago

Hi again,

a little simpler solution would be to

console.log(zoneId, ZoneId.of(zoneId).rules().offset(Instant.now()).toString());

That gives you the offset as a "+/-HH:mm" kind of string... probably easier to read than seconds :)

Cheers, Pattrick

pithu commented 6 years ago

Hi @phueper

Yes correct, the example gives you the offset for all available zone ids at a certain time.

But that is not exactly the same what you see at the joda example mentioned by @ovidius72 . There the standard offset is shown as provided by ZoneRules.standardOffset(). Unfortunately this is not (yet) implemented. We are using a reduced set of the tzdbdata provided by moment and this information is missing there.

see https://js-joda.github.io/js-joda/esdoc/class/src/zone/ZoneRules.js~ZoneRules.html#instance-method-standardOffset for more information about what the standard offset is.

ovidius72 commented 6 years ago

I was able to get a list with the below code. I'm using it in the browser.

var zones = JSJoda.ZoneId.getAvailableZoneIds().map(z => ({name: z, offset: moment.tz(moment(), z).format('Z')}))

However I've been using moment to get the offset. Probably i have to replace it with Instant

Thanks a lot for your suggestions.

pithu commented 6 years ago

Please feel free to reopen if there are any further questions