开发者

Ruby Time.zone.now.zone bug

开发者 https://www.devze.com 2023-04-10 12:00 出处:网络
What I mean is that there are different timezones with the same name, like CST (-06:00 in US and +09:30 in Australia). Accordingly conversion to ut开发者_StackOverflowc gives wrong results for Adelaid

What I mean is that there are different timezones with the same name, like CST (-06:00 in US and +09:30 in Australia). Accordingly conversion to ut开发者_StackOverflowc gives wrong results for Adelaide.

Any elegant way to solve this?


an easy way would be that you use the other nomenclature of naming time zones.. e.g. for UTC+9:30 intstead of using CST, you can use "Australia/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 

See: http://api.rubyonrails.org/classes/Time.html

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

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


do a "gem install activesupport", and in your script:

require 'rubygems'
require 'tzinfo'
require 'active_support'
0

精彩评论

暂无评论...
验证码 换一张
取 消