README.markdown

Timezone

A simple way to get accurate current and historical timezone information based
on zone or latitude and longitude coordinates. This gem uses the
tz database for historical timezone information. It also uses the
geonames API or the Google Timezone API for
timezone latitude and longitude lookup.

Installation

Add the following to your Gemfile:

gem 'timezone'

Then install your bundle.

bundle install

Getting Started

Getting the current time or any historical time in any timezone, with daylight
savings time taken into consideration, is easy:

Time is always returned in the UTC timezone when using the time function, but
it accurately reflects the actual time in the specified timezone. The reason for
this is that this function also takes into account daylight savings time and
historical changes in timezone, which can alter the offset. If you want a time
with the appropriate offset at the given time, then use the time_with_offset
function as shown above.

You can use the timezone object to convert local times into the best UTC
estimate. The reason this is an estimate is that some local times do not
actually map to UTC times (for example when time jumps forward) and some
local times map to multiple UTC times (for example when time falls back).

You can customize what is placed in the :title key in the configuration block. This would be useful in the case of an HTML select list that you would like to display different values than the default name. For example, the following configuration will set the :title key in the list hash to "Chicago" rather than "America/Chicago".

Finally, by default the Zone#list method will order the results by the timezone's UTC offset. You can customize this behavior this way:

Timezone::Configure.begin do |c|
# this can equal any hash key returned by the Zone#list method
c.order_list_by = :title
end

Using Your Own HTTP Client

If you have non-standard http request needs or want to have more control over API calls to Geonames and Google, you can write your own very simple http client wrapper instead of using the built-in default.
Be aware that the Google timezone API uses https protocol.

class MyHTTPClient
def initialize(protocol, host)
end
# Return a response object that responds to #body and #code
def get(url)
end
end
Timezone::Configure.begin do |c|
c.http_client = MyHTTPClient
end

For an example, see Timezone::NetHTTPClient which uses the standard Net::HTTP library to perform API calls.