Ruby timezone offset problem
I need to assign a timezone offset to a T开发者_运维百科ime to get current day of the week for a specified offset. This is not with rails so I need a pure Ruby formatter/parser to do this.
Thanks.
This is what I found:
require 'date'
local = DateTime.now
new_offset = Rational(0, 24) #put the offset you want as first argument
utc = local.new_offset(new_offset)
Returns the offset in seconds between the timezone of time and UTC.
t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
t.gmt_offset #=> 0
l = t.getlocal #=> 2000-01-01 14:15:01 -0600
l.gmt_offset #=> -21600
#As a string
t = Time.new(2011,6,27,14,10,0, "+07:00")
# or in seconds from UTC
t = Time.new(2011,6,27,14,10,0, 7*60*60)
精彩评论