Skip to content

Latest commit

 

History

History
42 lines (32 loc) · 1.24 KB

get-utc-offset-for-different-time-zones.md

File metadata and controls

42 lines (32 loc) · 1.24 KB

Get UTC Offset For Different Time Zones

The IANA Time Zone Database uses identifiers like America/Chicago, Asia/Hong_Kong, Africa/Nairobi, etc. as specifiers for notable locations with time zone information.

Most timezones correspond to a notable location and the database records all known clock transitions for that location; some timezones correspond instead to a fixed UTC offset. —Theory and pragmatics of the tz code and data

These identifiers can be used to look up time zone details with the tzinfo gem.

Here is an example of passing one to the #get method and then getting the UTC offset in seconds.

> require 'tzinfo'

> mountain = TZInfo::Timezone.get('America/Denver')
=> #<TZInfo::DataTimezone: America/Denver>
> mountain.utc_offset
=> -21600

We can even get the base UTC offset that doesn't account for DST:

> moutain.base_utc_offset
=> -25200

Notice, this is the same as the standard offset for a time zone like Phoenix that doesn't observe DST.

> phoenix = TZInfo::Timezone.get('America/Phoenix')
=> #<TZInfo::DataTimezone: America/Phoenix>
> phoenix.utc_offset
=> -25200