28

I need the utc offset from the timezone specified.

Lucas Renan
  • 3,787
  • 3
  • 28
  • 35

8 Answers8

44

If you need to take daylight savings time into account, you can use something like this:

Time.now.in_time_zone('America/New_York').utc_offset
lee
  • 2,351
  • 1
  • 16
  • 18
33
require 'time'

p Time.zone_offset('EST') #=> -18000  #(-5*60*60)
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
steenslag
  • 79,051
  • 16
  • 138
  • 171
  • 10
    Time.zone.utc_offset saved my day =) – Lucas Renan Mar 30 '11 at 17:36
  • 1
    Don't forget that in order for this to work properly, you have to set `config.time_zone = 'Berlin'` and `config.active_record.default_timezone = 'Berlin'` in application.rb (in my case, without that it didn't reflect daylight savings adjustments correctly) – Dennis Hackethal Jul 16 '13 at 13:21
  • 1
    @Charles, I had to change `config.active_record.default_timezone` to `:local` instead of `'Santiago'`, otherwise every DateTime field change to nil. Is `Time.zone_offset` going to deal with daylight savings correctly? – Sebastialonso Aug 17 '14 at 15:47
  • 1
    Lucas' comment to the answer is the best... what steenslag proposes gets borked by daylight savings time if you rely on that offset changing when that happens. (EST vs EDT in this instance). – Wes Johnson Mar 05 '15 at 18:26
  • `Time.now.utc_offset` also works with DST while `Time.zone.utc_offset` does not. – mb21 Apr 20 '18 at 19:39
  • Doesn't offset generally depend on a date? – x-yuri Apr 28 '18 at 16:00
17
Time.now.in_time_zone('Europe/London').formatted_offset
# => "+01:00"

For a string arguably more usable...

colemerrick
  • 1,118
  • 12
  • 17
11
my_date_time = DateTime.new(2011, 3, 29, 20)
#=> Tue, 29 Mar 2011 20:00:00 +0000

#will return the same time but with another offset
my_date_time.change(:offset => "+0100")
#=> Tue, 29 Mar 2011 20:00:00 +0100

#will return time for other offset
my_date_time.in_time_zone(-1)
#=>  Tue, 29 Mar 2011 19:00:00 -0100
fl00r
  • 82,987
  • 33
  • 217
  • 237
6

If you are storing a user's timezone as a Time.zone.name string such as 'Eastern Time (US & Canada)' as outlined in http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html and want to get the timezone offset you can do this:

ActiveSupport::TimeZone.new(user.time_zone).utc_offset / 3600
# => -5
timmcliu
  • 1,769
  • 1
  • 13
  • 12
  • 1
    Keep in mind, this solution only returns the canonical offset and doesn't contemplate daylight saving time. So, to be safe, is better to use `Time.now.in_time_zone(user.time_zone).utc_offset / 3600` – Alter Lagos Mar 16 '21 at 20:21
5

I use Time.zone.utc_offset / 1.hour to get the offset in hours (e.g.: -8 for San Francisco)

Dorian
  • 22,759
  • 8
  • 120
  • 116
5

If you have a ActiveSupport::TimeWithZone object, you can call time_zone.utc_offset on it. So, for example, Time.zone.now.time_zone.utc_offset.

EDIT: You can also use the gmt_offset instance method on normal Time objects.

William
  • 3,511
  • 27
  • 35
1

Just to complete the loop here, right now I use:

@course.start.utc_offset/60/60 in order to get the # of hours needed. Jquery countdown needs just -5 or +3 in their timezone argument.

Your welcome google.

pjammer
  • 9,489
  • 5
  • 46
  • 56