Skip to content

Instantly share code, notes, and snippets.

@foreverman
Created May 6, 2012 02:49
Show Gist options
  • Save foreverman/2607189 to your computer and use it in GitHub Desktop.
Save foreverman/2607189 to your computer and use it in GitHub Desktop.
Convert time between different time zones
ruby-1.8.7-p352 :002 > #current time zone
ruby-1.8.7-p352 :003 > Time.zone
=> #<ActiveSupport::TimeZone:0x10e5d8c98 @current_period=nil, @name="UTC", @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @utc_offset=nil>
irb(main):009:0> d.created_at
=> Fri, 04 May 2012 06:57:18 UTC +00:00
irb(main):012:0> d.created_at.time_zone.name
=> "UTC"
irb(main):013:0> d.created_at.in_time_zone 8
=> Fri, 04 May 2012 14:57:18 CST +08:00
#we can set current time zone
ruby-1.8.7-p352 :005 > u.created_at
=> Tue, 20 Dec 2011 09:20:37 UTC +00:00
ruby-1.8.7-p352 :006 > Time.zone = ActiveSupport::TimeZone[8]
=> #<ActiveSupport::TimeZone:0x10eb0c520 @current_period=#<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionInfo: #<TZInfo::TimeOrDateTime: 684860400>,#<TZInfo::TimezoneOffsetInfo: 28800,0,CST>>,nil>, @name="Beijing", @tzinfo=#<TZInfo::TimezoneProxy: Asia/Shanghai>, @utc_offset=nil>
ruby-1.8.7-p352 :007 > u.created_at
=> Tue, 20 Dec 2011 17:20:37 CST +08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment