简体   繁体   中英

Parse Rails timezone to_s on the JVM

I am writing a JVM based client for Shopify, and need to get the timezone info from the API. The data returned is of the form:

"(GMT-05:00) Eastern Time (US & Canada)"

Which doesn't actually describe the timezone. The standard format for timezones is generally of the form:

"America/New_York"

and is described here . I am using Joda-time to manage the timezones on the JVM side, and need to parse that string. The best I can do right now is to copy the list in the documentation into my code, strip the offset from the string and look it up. My problem with this is that the list looks rather incomplete and will be subject to change.

I rejected using the GMT-05:00 part since that fails to take into account DST and therefore doesn't describe the timezone, so it will be wrong for 6 months of the year, which is unacceptable.

Alternatively, it would be useful the Shopify API just provided the timezone, but I have searched the docs and can't see where that is. If someone can see it and I'm just being dumb, that would be great.

PS I'm not a ruby programmer, so it's entirely possible I'm missing something really obvious. Thanks.

I had the same problem before, but I was able to keep a copy of the Java compatible time zone identifier along with the Ruby/Rails one. You will probably have to build a conversion table to convert "Eastern Time (US & Canada)" to "America/New_York".

I have a similar product. On the Android side I use TimeZone.getDefault().getId() . It is generally already in the TZ database form you're asking for. On the ruby side I've found the timezone gem to be invaluable for scheduling, though I had to modify the gem a bit to meet my performance needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM