Created
August 10, 2014 23:31
-
-
Save mikem/23a555a21adaadb1a5a0 to your computer and use it in GitHub Desktop.
Create a Time in a specific timezone in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
def format_time time_str | |
old_tz = ENV['TZ'] | |
return if time_str.nil? | |
ENV['TZ'] = 'America/New_York' | |
Time.parse time_str | |
ensure | |
ENV['TZ'] = old_tz | |
end | |
Time.parse 'Aug 04' | |
#=> 2014-08-04 00:00:00 -0700 | |
format_time 'Aug 04' | |
#=> 2014-08-04 00:00:00 -0400 | |
Time.parse 'Aug 04' | |
#=> 2014-08-04 00:00:00 -0700 | |
ENV['TZ'] = 'Europe/Berlin' | |
#=> "Europe/Berlin" | |
Time.parse 'Aug 04' | |
#=> 2014-08-04 00:00:00 +0200 | |
format_time 'Aug 04' | |
#=> 2014-08-04 00:00:00 -0400 | |
Time.parse 'Aug 04' | |
#=> 2014-08-04 00:00:00 +0200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment