Class: ToiletTracker::Services::TimezoneService

Inherits:
Object
  • Object
show all
Defined in:
lib/toilet_tracker/services/timezone_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = ToiletTracker.configuration) ⇒ TimezoneService

Returns a new instance of TimezoneService.



10
11
12
# File 'lib/toilet_tracker/services/timezone_service.rb', line 10

def initialize(config = ToiletTracker.configuration)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/toilet_tracker/services/timezone_service.rb', line 8

def config
  @config
end

Instance Method Details

#apply_shift(time, shift_hours) ⇒ Object



25
26
27
28
29
# File 'lib/toilet_tracker/services/timezone_service.rb', line 25

def apply_shift(time, shift_hours)
  return time if shift_hours.zero?

  time + shift_hours.hours
end

#calculate_shift(from_timezone, to_timezone, at_time = Time.current) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/toilet_tracker/services/timezone_service.rb', line 14

def calculate_shift(from_timezone, to_timezone, at_time = Time.current)
  from_zone = resolve_timezone(from_timezone)
  to_zone = resolve_timezone(to_timezone)

  from_offset = from_zone.at(at_time).utc_offset
  to_offset = to_zone.at(at_time).utc_offset

  shift = (to_offset - from_offset) / 3600.0
  (shift == shift.to_i) ? shift.to_i : shift
end

#resolve_timezone(timezone_spec) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/toilet_tracker/services/timezone_service.rb', line 31

def resolve_timezone(timezone_spec)
  case timezone_spec
  when String
    resolve_timezone_string(timezone_spec)
  when ActiveSupport::TimeZone
    timezone_spec
  else
    raise Core::InvalidTimezone, timezone_spec
  end
end

#timezone_abbreviationsObject



42
43
44
# File 'lib/toilet_tracker/services/timezone_service.rb', line 42

def timezone_abbreviations
  @timezone_abbreviations ||= build_abbreviation_map
end