简体   繁体   English

在JVM上解析Rails时区to_s

[英]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. 我正在为Shopify写一个基于JVM的客户端,并且需要从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. 我正在使用Joda-time在JVM端管理时区,并且需要解析该字符串。 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. 我拒绝使用GMT-05:00部分,因为该部分未考虑DST,因此未描述时区,因此一年中的6个月是错误的,这是不可接受的。

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. 另外,Shopify API只是提供了时区,这很有用,但是我搜索了文档 ,看不到它在哪里。 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. PS:我不是一个红宝石程序员,所以我很可能错过了一些明显的东西。 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. 之前我也遇到过同样的问题,但是我能够与Ruby / Rails一起保留Java兼容时区标识符的副本。 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() . 在Android方面,我使用TimeZone.getDefault().getId() It is generally already in the TZ database form you're asking for. 它通常已经在您要的TZ数据库格式中。 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. 在红宝石方面,我发现时区宝石对于调度非常有用,尽管我不得不对宝石进行一些修改以满足我的性能需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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