Class: Clock::Localized::Timezone

Inherits:
Object
  • Object
show all
Defined in:
lib/clock/localized/timezone.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tzinfo_timezone, utc_reference) ⇒ Timezone

Returns a new instance of Timezone.



7
8
9
10
# File 'lib/clock/localized/timezone.rb', line 7

def initialize(tzinfo_timezone, utc_reference)
  @tzinfo_timezone = tzinfo_timezone
  @utc_reference = utc_reference
end

Instance Attribute Details

#tzinfo_timezoneObject (readonly)

Returns the value of attribute tzinfo_timezone.



4
5
6
# File 'lib/clock/localized/timezone.rb', line 4

def tzinfo_timezone
  @tzinfo_timezone
end

#utc_referenceObject (readonly)

Returns the value of attribute utc_reference.



5
6
7
# File 'lib/clock/localized/timezone.rb', line 5

def utc_reference
  @utc_reference
end

Class Method Details

.build(timezone_identifier = nil, utc_reference: nil) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/clock/localized/timezone.rb', line 12

def self.build(timezone_identifier=nil, utc_reference: nil)
  utc_reference ||= Time.now.utc

  timezone_identifier = Defaults.timezone_identifier(timezone_identifier)
  tzinfo_timezone = TZInfo::Timezone.get(timezone_identifier)

  new tzinfo_timezone, utc_reference
end

Instance Method Details

#convert(tzinfo_time) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/clock/localized/timezone.rb', line 35

def convert(tzinfo_time)
  seconds = tzinfo_time.sec + tzinfo_time.subsec

  Time.new(
    tzinfo_time.year,
    tzinfo_time.month,
    tzinfo_time.day,
    tzinfo_time.hour,
    tzinfo_time.min,
    seconds,
    offset
  )
end

#now(tzinfo_time = nil) ⇒ Object



25
26
27
28
# File 'lib/clock/localized/timezone.rb', line 25

def now(tzinfo_time=nil)
  tzinfo_time ||= tzinfo_timezone.now
  convert tzinfo_time
end

#offsetObject



49
50
51
52
53
54
55
56
57
# File 'lib/clock/localized/timezone.rb', line 49

def offset
  total_offset = period.offset.utc_total_offset

  hours, seconds = total_offset.abs.divmod 3600
  minutes = seconds / 60

  sign = total_offset > 0 ? '+' : '-'
  offset = "#{sign}#{hours.to_s.rjust(2, '0')}:#{minutes.to_s.rjust(2, '0')}"
end

#periodObject



21
22
23
# File 'lib/clock/localized/timezone.rb', line 21

def period
  tzinfo_timezone.period_for_utc utc_reference
end

#utc_to_local(utc_time) ⇒ Object



30
31
32
33
# File 'lib/clock/localized/timezone.rb', line 30

def utc_to_local(utc_time)
  tzinfo_time = tzinfo_timezone.utc_to_local utc_time
  convert tzinfo_time
end