iansinnott / jstz

🌐Timezone detection for JavaScript
Other
175 stars 33 forks source link

in wechart Mini Program always return America/Chicago #16

Open TinkerDeng opened 6 years ago

TinkerDeng commented 6 years ago

in wechart Mini Program always return America/Chicago, in browser return(Asia/Shanghai) is right

iansinnott commented 6 years ago

So jstz.determine().name() is returning America/Chicago even though it should return Asia/Shanghai?

What browser are you using?

TinkerDeng commented 6 years ago

image wechart Developer Tools
This problem has been solved. I think is Developer Tools problem,In mobile is right.thank you very mush

TinkerDeng commented 6 years ago

I met another problem. I want to add,such as Africa/Harare Africa/Addis_Ababa America/Indianapolis .But I don't know How to calculate

1525829609779

xyxabcdiy commented 6 years ago

+1

iansinnott commented 6 years ago

This problem has been solved.

Glad to hear it 👍

Hm, i've never tried to use the lib in this way, but if you want to calculate the key for the timezones object check out these comments: https://github.com/iansinnott/jstz/blob/master/jstz.main.js#L412-L418

xyxabcdiy commented 6 years ago

@iansinnott Thank for your help!
There is another problem: I found when I run jstz.determine().name() in node.js, the result is Asia/Shanghai, but it should be America/Chicago. I check the source code, if Intl.DateTimeFormat().resolvedOptions().timeZone can return value, it will use this method to get timezone. But it not accurate in some cases.

iansinnott commented 6 years ago

Unfortunately, if Intl.DateTimeFormat().resolvedOptions().timeZone is not accurate then there's nothing this lib can do. It will depend on what browser you're running in. I suspect some browsers handle the Intl.DateTimeFormat().resolvedOptions().timeZone API differently than others.

TinkerDeng commented 6 years ago

Change the order of this method (datermine) is return right.I don't know if there are any other questions. change before image

change after image image

iansinnott commented 6 years ago

That's interesting. It looks like get_from_internationalization_api() is indeed returning a value, but it's not the expected value. This means that Intl.DateTimeFormat().resolvedOptions().timeZone is returning the incorrect value.

It appears your solution works because it skips using the Intl API and calculates lookup_key() which is based on the Date object. This method is meant to be a fallback only if the Intl API is not supported in the browser, not an initial solution.

So it looks like your browser has the Intl API but it returns the wrong timezone. This is unusual but there's not much you can do about it since you can't control what browser your users will be using.

On the other hand, if WeChat is you're only target platform then I would recommend forking this library and modifying it exactly as you suggested.