0218fde3a78fadbadb566bdb40d7b0dd

I need to get the Daylight-Savings-Time-adjusted UTC offset for time zones. So, for instance, Central Time US is currently [1] UTC -5 since it is DST. During Standard Time, it is UTC -6.

The easiest way I found to do this is to leverage everything from tzinfo_timezone [2] to tztime[3]. I'm only using tztime for access to it's "dst?" method. Wondering if (a) there's an easier way to do this that I'm missing or (b) if there is a more direct way to do this without requiring two plugins and a gem (tzinfo).

TIA

[1] http://www.timeanddate.com/worldclock/results.html?query=minneapolis
[2] http://agilewebdevelopment.com/plugins/tzinfo_timezone
[3] http://weblog.jamisbuck.org/2007/2/2/introducing-tztime

script/console output for utc_offset

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Note: Kuwait does not use DST.  
# See how the utc_offset value represents 
# the Standard Time offset for Dublin.

>> TimeZone["Kuwait"].class
=> TzinfoTimezone
>> TimeZone["Kuwait"].utc_offset/60/60
=> 3
>> TimeZone["Dublin"].utc_offset/60/60
=> 0
>> Time.now.utc
=> Thu Oct 25 04:09:57 UTC 2007
>> TimeZone["Kuwait"].now
=> Thu Oct 25 07:10:14 UTC 2007
>> TimeZone["Dublin"].now
=> Thu Oct 25 05:10:08 UTC 2007

Method to return DST adjusted UTC offset

1
2
3
4
5
6
def dst_utc_offset(zone_name)
  zone        = TimeZone[zone_name]
  TzTime.zone = zone
  # If Daylight Savings Time, add an hour (3600 seconds) to the offset.
  TzTime.now.dst? ? zone.utc_offset + 3600 : zone.utc_offset
end

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel