28

I have a variable foo that contains a time, lets say 4pm today, but the zone offset is wrong, i.e. it is in the wrong time zone. How do I change the time zone?

When I print it I get

Fri Jun 26 07:00:00 UTC 2009

So there is no offset, and I would like to set the offset to -4 or Eastern Standard Time.

I would expect to be able to just set the offset as a property of the Time object, but that doesn't seem to be available?

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Janak
  • 5,025
  • 8
  • 34
  • 43
  • I'd be interested to hear how you're setting the variable `foo` to begin with. If you're parsing a time string that doesn't have a time zone (what I was doing) then you can do the following to force the time zone during the parsing: `"Fri Jun 26 2019 07:00:00".in_time_zone( "Eastern Time (US & Canada)" ) # => => Wed, 26 Jun 2019 07:00:00 EDT -04:00` – Joshua Pinter Oct 20 '18 at 14:43

13 Answers13

18

You don't explicitly say how you get the actual variable but since you mention the Time class so I'll assume you got the time using that and I'll refer to that in my answer

The timezone is actually part of the Time class (in your case the timezone is shown as UTC). Time.now will return the offset from UTC as part of the Time.now response.

>> local = Time.now
=> 2012-08-13 08:36:50 +0000
>> local.hour
=> 8
>> local.min
=> 36
>> 


... in this case I happen to be in the same timezone as GMT

Converting between timezones

The easiest way that I've found is to change the offset using '+/-HH:MM' format to the getlocal method. Let's pretend I want to convert between the time in Dublin and the time in New York

?> dublin = Time.now
=> 2012-08-13 08:36:50 +0000
>> new_york = dublin + Time.zone_offset('EST')
=> 2012-08-13 08:36:50 +0000
>> dublin.hour
=> 8
>> new_york.hour
=> 3

Assuming that 'EST' is the name of the Timezone for New York, as Dan points out sometimes 'EDT' is the correct TZ.

Chris McCauley
  • 25,824
  • 8
  • 48
  • 65
15

This takes advantage of the fact that Time#asctime doesn't include the zone.

Given a time:

>> time = Time.now
=> 2013-03-13 13:01:48 -0500

Force it to another zone (this returns an ActiveSupport::TimeWithZone):

>> ActiveSupport::TimeZone['US/Pacific'].parse(time.asctime)
=> Wed, 13 Mar 2013 13:01:48 PDT -07:00

Note that the original zone is ignored completely. If I convert the original time to utc, the result will be different:

>> ActiveSupport::TimeZone['US/Pacific'].parse(time.getutc.asctime)
=> Wed, 13 Mar 2013 18:01:48 PDT -07:00

You can use to_time or to_datetime on the result to get a corresponding Time or DateTime.

This question uses an interesting approach with DateTime#change to set the tz offset. (Remember that ActiveSupport makes it easy to convert between Time and DateTime.) The downside is that there's no DST detection; you have to do that manually by using TZInfo's current_period.

Community
  • 1
  • 1
Kelvin
  • 20,119
  • 3
  • 60
  • 68
15

If given:

2011-10-25 07:21:35 -700

you want:

2011-10-25 07:21:35 UTC

then do:

Time.parse(Time.now.strftime('%Y-%m-%d %H:%M:%S UTC')).to_s
Brian Knight
  • 4,970
  • 28
  • 34
99miles
  • 10,942
  • 18
  • 78
  • 123
  • 1
    Very helpful trick, thanks, to be able to create a time object in an arbitrary timezone WITHOUT using the (unsafe IMO) approach of changing Time.zone – jpw Nov 05 '12 at 07:19
  • 1
    Note: `require 'time'` may be needed – Dr1Ku Oct 08 '14 at 19:55
11

...

>> Time.at(Time.now.utc + Time.zone_offset('PST'))
=> Mon Jun 07 22:46:22 UTC 2010
>> Time.at(Time.now.utc + Time.zone_offset('PDT'))
=> Mon Jun 07 23:46:26 UTC 2010
>> Time.at(Time.now.utc + Time.zone_offset('CST'))
=> Tue Jun 08 00:46:32 UTC 2010

One note: make sure that the current time object is set to UTC time first, otherwise Ruby will try and convert the Time object to your local timezone, thus throwing the calculation. You can always get the adjusted time by applying ".utc" to the end of the above statements in that case.

ALW
  • 1,007
  • 9
  • 13
  • 5
    the problem here is the returned value shows UTC instead of the specific time zone you are trying to convert into. – Clark Apr 26 '12 at 19:50
10

For those that came across this while looking for a non-rails solution (as I did), TZInfo solved it for me...

require 'tzinfo'
def adjust_time time, time_zone="America/Los_Angeles"
    return TZInfo::Timezone.get(time_zone).utc_to_local(time.utc)
end

puts adjust_time(Time.now) 
#=> PST or PDT
puts adjust_time(Time.now, "America/New_York")
#=> EST or EDT

This also handles DST, which is what I needed that wasn't handled above.

See: http://tzinfo.rubyforge.org/

jmervine
  • 723
  • 7
  • 15
5

in you environment.rb search for the following line.

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names.
config.time_zone = 'UTC'

Keep in mind ActiveRecord and Rails always handle Time as UTC internally.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
2

I'm using Rails 2.0 before they added the code that makes weppos solution work. Here's what I did

# Silly hack, because sometimes the input_date is in the wrong timezone
temp = input_date.to_time.to_a
temp[8] = true
temp[9] = "Eastern Daylight Time"
input_date = Time.local(*temp)

I break the time down into a 10 element array, change the timezone and then convert the array back into a time.

Janak
  • 5,025
  • 8
  • 34
  • 43
  • I'd vote this up, since it looks like a workable solution, but it's not working AT ALL for me, and I don't have time to fiddle with it to get it working, since I have another (unfortunately less generalized) solution. Are you sure this ever worked for you? I'd bet a beer (if you're in Chicago) that the problem is on the last line. – iconoclast Sep 10 '10 at 16:12
1

Here is what worked for me...

def convert_zones(to_zone)
   to_zone_time = to_zone.localtime
end


# have your time set as time

time = convert_zones(time)
time.strftime("%b #{day}, %Y (%a) #{hour}:%M %p %Z")
1

This is what I did, as I am not using Rails and don't want to use any non-core gems.

t = Time.now # my local time - which is GMT
zone_offset = 3600 # offset for CET - which is my target time zone
zone_offset += 3600 if t.dst? # an extra hour offset in summer
time_cet = Time.mktime(t.sec, t.min, t.hour, t.mday, t.mon, t.year, nil, nil, t.dst?, zone_offset)
Chris Kimpton
  • 5,546
  • 6
  • 45
  • 72
1

You can do:

DateTime.parse('Fri Jun 26 07:00:00 UTC 2009').change(offset: '-0400')

Which returns:

Fri, 26 Jun 2009 07:00:00 -0400
Christian Fazzini
  • 19,613
  • 21
  • 110
  • 215
0

Option 1

Use date_time_attribute gem:

my_date_time = DateTimeAttribute::Container.new(Time.zone.now)
my_date_time.date_time           # => 2001-02-03 22:00:00 KRAT +0700
my_date_time.time_zone = 'Moscow'
my_date_time.date_time           # => 2001-02-03 22:00:00 MSK +0400

Option 2

If time is used as an attribute, you can use the same date_time_attribute gem:

class Task
  include DateTimeAttribute
  date_time_attribute :due_at
end

task = Task.new
task.due_at_time_zone = 'Moscow'
task.due_at                      # => Mon, 03 Feb 2013 22:00:00 MSK +04:00
task.due_at_time_zone = 'London'
task.due_at                      # => Mon, 03 Feb 2013 22:00:00 GMT +00:00
Sergei Zinin
  • 161
  • 1
  • 3
0

It's probably a good idea to store the time as UTC and then show it in a specific time zone when it is displayed. Here's an easy way to do that (works in Rails 4, unsure about earlier versions).

t = Time.now.utc

=> 2016-04-19 20:18:33 UTC

t.in_time_zone("EST")

=> Tue, 19 Apr 2016 15:18:33 EST -05:00

But if you really want to store it in a specific timezone, you can just set the initial Time object to itself.in_time_zone like this:

t = t.in_time_zone("EST")
ryan0
  • 1,482
  • 16
  • 21
0

When Parsing a Time

I'd be interested to hear how you're setting the variable foo to begin with.

If you're parsing a time string that doesn't have a time zone (what I was doing during a data import) then you can use String#in_time_zone to force the time zone during the parsing:

"Fri Jun 26 2019 07:00:00".in_time_zone( "Eastern Time (US & Canada)" ) 
# => Wed, 26 Jun 2019 07:00:00 EDT -04:00

Works like a charm and is super clean.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245