Class: TZDetect::TimeZone

Inherits:
Object
  • Object
show all
Defined in:
lib/tzdetect/zone.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country_code, city, region = nil, latitude = nil, longitude = nil) ⇒ TimeZone

Constructor

Example

Country with many timezones without position data. System will find location for Miami and then find timezone

tz = TimeZone.new("US", "Miami", "Florida")
tz.timezone.name   #  "America/New_York"
tz.latitude        #   25.774265 
tz.longitude       #  -80.193658

Country with many timezones with position data. System will directly find timezone with position data

tz = TimeZone.new("US", "Miami", "Florida", 25.774265, -80.193658)
tz.timezone.name   #  "America/New_York"

Country with one timezone will not return location data

tz = TimeZone.new("RS", "Novi Sad")
tz.timezone.name   #  "Europe/Belgrade"
tz.latitude        #   nil 
tz.longitude #   nil


46
47
48
49
50
51
52
53
# File 'lib/tzdetect/zone.rb', line 46

def initialize country_code, city, region=nil, latitude=nil, longitude=nil
  @country_code = country_code.upcase
  @city      = city
  @region    = region
  @latitude  = latitude
  @longitude = longitude
  timezone!
end

Instance Attribute Details

#cityObject (readonly)

Returns the value of attribute city.



13
14
15
# File 'lib/tzdetect/zone.rb', line 13

def city
  @city
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



13
14
15
# File 'lib/tzdetect/zone.rb', line 13

def country_code
  @country_code
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



13
14
15
# File 'lib/tzdetect/zone.rb', line 13

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



13
14
15
# File 'lib/tzdetect/zone.rb', line 13

def longitude
  @longitude
end

#regionObject (readonly)

Returns the value of attribute region.



13
14
15
# File 'lib/tzdetect/zone.rb', line 13

def region
  @region
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



13
14
15
# File 'lib/tzdetect/zone.rb', line 13

def timezone
  @timezone
end

Class Method Details

.by_location(latitude, longitude) ⇒ Object

Detect timezone by latitude and longitude Returns TZInfo object, if can’t find timezone returns nil



102
103
104
105
106
107
108
109
# File 'lib/tzdetect/zone.rb', line 102

def by_location latitude, longitude
  begin
    self.by_location! latitude, longitude
  rescue Exception 
    nil
  end

end

.by_location!(latitude, longitude) ⇒ Object

Detect timezone by latitude and longitude Returns TZInfo raise errors



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tzdetect/zone.rb', line 86

def by_location! latitude, longitude
  type = TZDetect::Configuration.service
  case type
  when :google
    geocoder = GoogleParser.new(latitude, longitude)
  when :geoname
    geocoder = GeonameParser.new(latitude, longitude)
  else
    raise TZDetect::Error::Configuration, "wrong configuration for field type"
  end
  TZInfo::Timezone.get geocoder.timezone!
end

.countries_with_many_tzObject

Returns countries codes for countries with more then one timezone



61
62
63
# File 'lib/tzdetect/zone.rb', line 61

def countries_with_many_tz
  TZInfo::Country.all.select{|m| m.zones.count > 1}.map{|m| m.code}
end

.get(country_code, city, region = "", latitude = nil, longitude = nil) ⇒ Object

Detect timezone by country code city region and position data Returns TZInfo object and raise errors



74
75
76
77
78
79
80
81
# File 'lib/tzdetect/zone.rb', line 74

def get country_code, city, region="", latitude=nil, longitude=nil
  begin
    m = self.new country_code, city, region, latitude, longitude
    m.timezone
  rescue Exception 
    nil
  end
end

.get!(country_code, city, region = "", latitude = nil, longitude = nil) ⇒ Object

Detect timezone by country code city region and position data Returns TZInfo object and raise errors



67
68
69
70
# File 'lib/tzdetect/zone.rb', line 67

def get! country_code, city, region="", latitude=nil, longitude=nil
  m = self.new country_code, city, region, latitude, longitude
  m.timezone!
end

.get_location!(country_code, city, region = nil) ⇒ Object

Detect timezone by latitude and longitude Returns TZInfo object, if can’t find timezone returns nil



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/tzdetect/zone.rb', line 114

def get_location!(country_code, city, region=nil)
  type = TZDetect::Configuration.service
  case type
  when :google
    geocoder = GoogleGeocode.new(country_code, city, region)
  when :geoname
    geocoder = GeonameGeocode.new(country_code, city, region)
  else
    raise TZDetect::Error::Configuration, "wrong configuration for field type"
  end
  geocoder.position!
end