Skip to content

Instantly share code, notes, and snippets.

@bjeanes
Created April 29, 2009 04:09
Show Gist options
  • Select an option

  • Save bjeanes/103571 to your computer and use it in GitHub Desktop.

Select an option

Save bjeanes/103571 to your computer and use it in GitHub Desktop.
# If it is going to make it a UTC time, it should
# at least convert it but it just assumes it is
# UTC without even looking at the time zone
time = "2007-08-05T20:00:00-04:00"
Time.parse(time) # => Mon Aug 06 10:00:00 1000 2007
time.to_time # => Sun Aug 05 20:00:00 UTC 2007
# Fix:
class String
def to_time(form = :utc)
time = Time.parse(self)
form.to_sym == :utc ? time.utc : time
end
end
puts time.to_time # => Mon Aug 06 00:00:00 UTC 2007
puts time.to_time(:local) # => Mon Aug 06 10:00:00 1000 2007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment