module RequestInfo::Detectors::TimezoneDetector

Detects Timezone related information

Public Instance Methods

analyze(_env) click to toggle source
Calls superclass method
# File lib/request_info/detectors/timezone_detector.rb, line 10
def analyze(_env)
  super

  results = RequestInfo.results
  tzinfo_id, tzinfo = get_tzinfo_from_ipinfo(results.ipinfo)
  return unless tzinfo_id && tzinfo

  results.timezone = tzinfo
  results.timezone_id = tzinfo_id
  results.timezone_offset = calculate_utc_offset(tzinfo)
  results.timezone_desc = tz_description(tzinfo)
end

Private Instance Methods

calculate_utc_offset(tzinfo) click to toggle source

Total offset is UTC + DST

# File lib/request_info/detectors/timezone_detector.rb, line 36
def calculate_utc_offset(tzinfo)
  tzinfo.current_period.utc_total_offset / 3600.0
end
get_tzinfo_from_ipinfo(ipinfo) click to toggle source

Return time zone identifier and object basing on what has been found by GeoIP.

# File lib/request_info/detectors/timezone_detector.rb, line 27
def get_tzinfo_from_ipinfo(ipinfo)
  tzinfo_id = ipinfo && ipinfo["time_zone"]
  tzinfo = tzinfo_id && TZInfo::Timezone.get(tzinfo_id)
  tzinfo ? [tzinfo_id, tzinfo] : nil
rescue TZInfo::InvalidTimezoneIdentifier
  nil
end
tz_description(tzinfo) click to toggle source

TODO: i18n this

# File lib/request_info/detectors/timezone_detector.rb, line 41
def tz_description(tzinfo)
  offset = calculate_utc_offset(tzinfo)
  offset_string = "#{offset > 0 ? '+' : ''}#{offset}"
  "GMT(#{offset_string}) #{tzinfo.friendly_identifier}"
end