我的意思是有相同名称的不同时区,如CST (美国为-06:00,澳大利亚为+09:30 )。因此,转换为utc会给阿德莱德带来错误的结果。
有什么优雅的方法来解决这个问题吗?
发布于 2011-10-01 12:52:17
一种简单的方法是使用另一种命名时区的命名方法。例如,对于澳大利亚而不是CST,你可以使用“UTC+9:30 /Adelaide”
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",并在脚本中:
require 'rubygems'
require 'tzinfo'
require 'active_support'https://stackoverflow.com/questions/7618221
复制相似问题