首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby Time.zone.now.zone错误

Ruby Time.zone.now.zone错误
EN

Stack Overflow用户
提问于 2011-10-01 12:43:53
回答 1查看 354关注 0票数 0

我的意思是有相同名称的不同时区,如CST (美国为-06:00,澳大利亚为+09:30 )。因此,转换为utc会给阿德莱德带来错误的结果。

有什么优雅的方法来解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-01 12:52:17

一种简单的方法是使用另一种命名时区的命名方法。例如,对于澳大利亚而不是CST,你可以使用“UTC+9:30 /Adelaide”

代码语言:javascript
复制
tz = TZInfo::Timezone.get("Australia/Adelaide")

t = Time.now   # you could also get the UTC time here with Time.now.utc
t.zone
 => "PDT" 
t.utc_offset
 => -25200 
t.in_time_zone(tz)    # this is a Rails extension of the Time class
 => Sat, 01 Oct 2011 14:31:05 CST +09:30 
t.in_time_zone(tz).zone
 => "CST" 
t.in_time_zone(tz).utc_offset
 => 34200 

请参阅:http://api.rubyonrails.org/classes/Time.html

http://tzinfo.rubyforge.org/doc/

http://en.wikipedia.org/wiki/List_of_tz_database_time_zones

执行"gem install activesupport",并在脚本中:

代码语言:javascript
复制
require 'rubygems'
require 'tzinfo'
require 'active_support'
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7618221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档