Skip to content

Instantly share code, notes, and snippets.

@mikem
Created August 10, 2014 23:31
Show Gist options
  • Save mikem/23a555a21adaadb1a5a0 to your computer and use it in GitHub Desktop.
Save mikem/23a555a21adaadb1a5a0 to your computer and use it in GitHub Desktop.
Create a Time in a specific timezone in Ruby
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