Module: TimeTraveler

Defined in:
lib/time_traveler.rb,
lib/time_traveler/utils.rb,
lib/time_traveler/version.rb

Overview

Find the time zone for a given geographical coordinate.

Author:

Defined Under Namespace

Classes: Utils

Constant Summary collapse

DATA_DIR =
Pathname.new(__FILE__).join('../data').freeze
VERSION =

Current version

'24.5.24'

Class Method Summary collapse

Class Method Details

.find_timezone(longitude, latitude) ⇒ TZInfo::Timezone

Find the timezone for a given set of coordinates.

Parameters:

  • longitude (Float)

    the longitude.

  • latitude (Float)

    the latitude.

Returns:

  • (TZInfo::Timezone)

    an object describing the timezone.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/time_traveler.rb', line 17

def self.find_timezone(longitude, latitude)
  world = Utils.load_timezone_data
  entry_point = Quadtree::Point.new(longitude, latitude)
  radius = 6
  zones = []
  while zones.size.zero? || (zones.size > 1)
    points = world.query_range(Quadtree::AxisAlignedBoundingBox.new(entry_point, radius))
    zones = points.map { |p| TZInfo::Timezone.get(p.data) }.uniq
    radius -= 0.5
  end
  zones.first
end