Module: WhereTZ

Extended by:
WhereTZ
Included in:
WhereTZ
Defined in:
lib/wheretz.rb

Overview

WhereTZ is quick and simple time zone lookup by geographic point.

Usage:

WhereTZ.lookup(50.004444, 36.231389)
# => 'Europe/Kiev'

WhereTZ.get(50.004444, 36.231389)
# => #<TZInfo::DataTimezone: Europe/Kiev>

Instance Method Summary collapse

Instance Method Details

#get(lat, lng) ⇒ TZInfo::DataTimezone, ...

TZInfo::DataTimezone object by coordinates.

Note that you should add tzinfo to your Gemfile to use this method. wheretz doesn't depend on tzinfo by itself.

Parameters:

  • lat

    Latitude (floating point number)

  • lng

    Longitude (floating point number)

Returns:

  • (TZInfo::DataTimezone, nil, Array<TZInfo::DataTimezone>)

    timezone object or nil if no timezone corresponds to (lat, lng); in rare (yet existing) cases of ambiguous timezones may return an array of timezones



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wheretz.rb', line 57

def get(lat, lng)
  begin
    require 'tzinfo'
  rescue LoadError
    raise LoadError, 'Please install tzinfo for using #get'
  end

  name = lookup(lat, lng)
  case name
  when String
    TZInfo::Timezone.get(name)
  when Array
    name.map(&TZInfo::Timezone.method(:get))
  end
end

#lookup(lat, lng) ⇒ String, ...

Time zone name by coordinates.

Parameters:

  • lat

    Latitude (floating point number)

  • lng

    Longitude (floating point number)

Returns:

  • (String, nil, Array<String>)

    time zone name, or nil if no time zone corresponds to (lat, lng); in rare (yet existing) cases of ambiguous timezones may return an array of names



35
36
37
38
39
40
41
42
43
44
# File 'lib/wheretz.rb', line 35

def lookup(lat, lng)
  candidates = FILES.select { |_f, _z, xr, yr| xr.cover?(lng) && yr.cover?(lat) }

  case candidates.size
  when 0 then nil
  when 1 then candidates.first[1]
  else
    lookup_geo(lat, lng, candidates)
  end
end