Module: Service::TimeZone
- Defined in:
- lib/service/time_zone.rb
Constant Summary collapse
- ZONES =
{ 'ao' => 'Africa/Casablanca', # Angola 'bw' => 'Africa/Harare', # Botswana 'cd' => 'Africa/Casablanca', # Democratic Republic of the Congo 'sz' => 'Africa/Harare', # Eswatini 'gh' => 'Atlantic/Azores', # Ghana 'intl' => 'Atlantic/Azores', # International (Generic UTC) 'ci' => 'Atlantic/Azores', # Ivory Coast 'je' => 'Europe/London', # Jersey 'ke' => 'Africa/Nairobi', # Kenya 'ls' => 'Africa/Harare', # Maseru Lesotho 'mw' => 'Africa/Harare', # Malawi 'mu' => 'Asia/Muscat', # Mauritius 'mz' => 'Africa/Harare', # Mozambique 'na' => 'Africa/Harare', # Namibia 'ng' => 'Africa/Casablanca', # Nigeria 'za' => 'Africa/Harare', # South Africa 'tz' => 'Africa/Nairobi', # Tanzania 'ug' => 'Africa/Nairobi', # Uganda 'zm' => 'Africa/Nairobi', # Zambia 'zw' => 'Africa/Harare', # Zimbabwe 'us' => 'America/Los_Angeles' }
Class Method Summary collapse
- .duration_in_hours(start_day, start_hour, end_day, end_hour) ⇒ Object
-
.iso8601(country_code:, local_time:) ⇒ Object
return the local time in the country return nil if country_code is not found in [ZONES].
-
.iso8601_with_zone_id(zone_id, local_time) ⇒ Object
Format the time to ISO 8601 with the zone identifier (example: “America/New_York”).
Class Method Details
.duration_in_hours(start_day, start_hour, end_day, end_hour) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/service/time_zone.rb', line 49 def self.duration_in_hours(start_day, start_hour, end_day, end_hour) start_index = Date::DAYNAMES.index(start_day.capitalize) end_index = Date::DAYNAMES.index(end_day.capitalize) raise ArgumentError, 'Invalid day provided' if start_index.nil? || end_index.nil? end_index += 7 if start_index > end_index start_hours = start_index * 24 + start_hour.to_i end_hours = end_index * 24 + end_hour.to_i end_hours - start_hours end |
.iso8601(country_code:, local_time:) ⇒ Object
return the local time in the country return nil if country_code is not found in [ZONES]
36 37 38 39 |
# File 'lib/service/time_zone.rb', line 36 def self.iso8601(country_code:, local_time:) zone_id = ZONES[country_code] || 'GMT' iso8601_with_zone_id(zone_id, local_time) end |
.iso8601_with_zone_id(zone_id, local_time) ⇒ Object
Format the time to ISO 8601 with the zone identifier (example: “America/New_York”)
43 44 45 46 47 |
# File 'lib/service/time_zone.rb', line 43 def self.iso8601_with_zone_id(zone_id, local_time) zone = ActiveSupport::TimeZone[zone_id] datetime = zone.local(local_time.year, local_time.month, local_time.day, local_time.hour, local_time.minute) datetime.iso8601 + "[#{zone.name}]" end |