Module: TpCommon::Timezones

Defined in:
lib/tp_common/timezones.rb,
lib/tp_common/timezones/zone.rb,
lib/tp_common/timezones/config.rb

Overview

Helper methods for handle, display timezones. Use TP’s custom mapping timezones instead of ActiveRecord or TzInfo

Defined Under Namespace

Classes: Config, Zone

Class Method Summary collapse

Class Method Details

.converted_time(time, zone) ⇒ Object

Opposite of #local_to_utc It conver a time to time in zone defined in LIST_ZONES



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tp_common/timezones.rb', line 74

def self.converted_time(time, zone)
  if time
    zone_value = LIST_ZONES[zone]

    if zone_value
      time.in_time_zone(zone_value[:name])
    else
      time
    end
  end
end

.current_date_in_time_zone(time_zone_key) ⇒ Object

Get current date in specific timezone. timezone is LIST_ZONES



13
14
15
# File 'lib/tp_common/timezones.rb', line 13

def self.current_date_in_time_zone(time_zone_key)
  self.converted_time(Time.now.utc, time_zone_key).strftime('%Y-%m-%d %H:%M:%S').to_date
end

.list_for_selectObject

LIST_ZONES which map to format [text_to_display, key_identity] For use in render select tag



54
55
56
# File 'lib/tp_common/timezones.rb', line 54

def self.list_for_select
  LIST_ZONES.map{ |k, v| [self.zone_title(k), k] }
end

.list_zone_offsetsObject

return hash offsets. Ex: => 0, …



60
61
62
# File 'lib/tp_common/timezones.rb', line 60

def self.list_zone_offsets
  Hash[LIST_ZONES.map{ |k, v| [k, Time.now.in_time_zone(v[:name]).utc_offset] }]
end

.local_to_utc(time, zone) ⇒ Object

Convert back a time in zone (defined in LIST_ZONES) to utc time



66
67
68
69
70
# File 'lib/tp_common/timezones.rb', line 66

def self.local_to_utc(time, zone)
  zone_value = LIST_ZONES[zone]
  (ActiveSupport::TimeZone.new(zone_value[:title]) ||
    ActiveSupport::TimeZone.new(zone_value[:name])).local_to_utc(time)
end

.offset_in_words(zone_name) ⇒ Object

Human readable offset: GMT+7, GMT-9,…



18
19
20
21
22
23
24
25
26
27
# File 'lib/tp_common/timezones.rb', line 18

def self.offset_in_words(zone_name)
  offset = Time.now.in_time_zone(zone_name).utc_offset
  in_words = 'GMT'

  hours = offset.abs / (60 * 60)

  in_words << (offset > 0 ? '+' : '-')
  in_words << format('%01d', hours)
  in_words
end

.relative_time_in_time_zone(time, time_zone_key) ⇒ Object

Return a DateTime object of param time in specific timezone decide by time_zone_key



95
96
97
98
# File 'lib/tp_common/timezones.rb', line 95

def self.relative_time_in_time_zone(time, time_zone_key)
  zone = zone_abbreviation(time_zone_key)
  DateTime.parse(time.strftime("%d %b %Y %H:%M:%S #{time.in_time_zone(zone).formatted_offset}"))
end

.zone_abbreviation(time_zone_key) ⇒ Object

Get zone name by time_zone_key



88
89
90
91
# File 'lib/tp_common/timezones.rb', line 88

def self.zone_abbreviation(time_zone_key)
  zone_value = LIST_ZONES[time_zone_key]
  zone_value[:name]
end

.zone_title(zone) ⇒ Object

Zone title which display in select options. if this zone applied DST, display sun_emoji at tail Display as: title + offset_in_words [+ sun_emoji]



33
34
35
36
37
38
39
40
# File 'lib/tp_common/timezones.rb', line 33

def self.zone_title(zone)
  zone_value = LIST_ZONES[zone]
  return nil unless zone_value

  dst_icon = zone_value[:dst] ? '☀️' : ''

  [zone_value[:title], self.offset_in_words(zone_value[:name]), dst_icon].reject{|part| part.empty? }.join(" ")
end

.zone_title_without_dst(zone) ⇒ Object

The same as (see #zone_title) but doesn’t display DST sun_emoji



44
45
46
47
48
49
# File 'lib/tp_common/timezones.rb', line 44

def self.zone_title_without_dst(zone)
  zone_value = LIST_ZONES[zone]
  return nil unless zone_value

  [zone_value[:title], self.offset_in_words(zone_value[:name])].reject{|part| part.empty? }.join(" ")
end