Mapping IANA/Olson timezone database to abbreviations (like EST, PST, etc)

与世无争的帅哥 提交于 2019-12-12 17:06:08

问题


I need to map IANA/Olson timezone id to abbreviations, like EST, PST, etc.

I understand that this is not 1-to-1 mapping and that, for example, for EST there are quite a bunch of IANA timezones.

Is there some kind of database/mapping I can use for this?

PS: JavaScript solution is preferable, but any info that could help me to build this mapping (IANA timezone id -> abbreviation) is appreciated.


回答1:


The IANA TZDB source data does have abbreviations already, but they have to be computed for the date in question. You can see it in the example data here, in the Zone.FORMAT and Rule.LETTER/S columns.

Since time zone abbreviations like CST can be ambiguous, it is only recommended you use them for display to a human. Never attempt to use them going the other direction, because only a few will be recognized by most implementations, and they tend to be valid only for the USA.

Since you asked for code that could do this for you, look at the bottom half of the code in my answer of how to do this using Noda Time in .Net. (The top half is about translating from a Windows zone to an IANA zone first, which you don't need.)

You could look at one of the several TZDB libraries for JavaScript, but I'm not sure if any directly expose the abbreviation data or not. Besides, that's a bit heavy for something so small.




回答2:


In java with joda-time, we can get time-zone abbreviation from iana id as below

DateTimeZone dz = DateTimeZone.forID("America/New_York");
String tzid = dz.getShortName(DateTimeUtils.currentTimeMillis());
//tzid will be 'EST'

String longerTimeZoneName = dz.getName(DateTimeUtils.currentTimeMillis());
//longerTimeZoneName  will be 'Eastern Standard Time'


来源:https://stackoverflow.com/questions/18250699/mapping-iana-olson-timezone-database-to-abbreviations-like-est-pst-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!