Skip to content

Instantly share code, notes, and snippets.

@wnuqui
Created July 3, 2012 09:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wnuqui/3038677 to your computer and use it in GitHub Desktop.
Save wnuqui/3038677 to your computer and use it in GitHub Desktop.
in the zone
# default zone
Time.zone # => (GMT+00:00) UTC
# then we can assign zone like use "Hawaii"
Time.zone = "Hawaii" # => "Hawaii"
# then do local using zone
local = Time.zone.local(2012, 8, 1, 15, 30, 45) # => Wed, 01 Aug 2012 15:30:45 HST -10:00
# then save this local to a column and save
# (it will be converted auto to UTC by rails when save in table)
# take note our Time.zone is still "Hawaii" so
activity = Activity.first
activity.update_attribute :created_at, Time.zone.local(2007, 2, 1, 15, 30, 45)
# and when
activity.created_at # => Wed, 01 Aug 2007 15:30:45 HST -10:00
# now change back Time.zone to UTC
Time.zone = 'UTC'
# and do
activity.created_at # => Thu, 02 Aug 2007 01:30:45 UTC +00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment