Class: RiCal::Component::TZInfoTimezone::Period

Inherits:
Object
  • Object
show all
Defined in:
lib/ri_cal/component/t_z_info_timezone.rb

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initialize(which, this_period, prev_period) ⇒ Period

Returns a new instance of Period.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ri_cal/component/t_z_info_timezone.rb', line 11

def initialize(which, this_period, prev_period)
  @which = which
  if prev_period
    @onset = period_local_end(prev_period)
    @offset_from = format_rfc2445_offset(prev_period.utc_total_offset)
  else
    @onset = period_local_start(this_period)
    @offset_from = format_rfc2445_offset(this_period.utc_total_offset)
  end
  @offset_to = format_rfc2445_offset(this_period.utc_total_offset)
  @abbreviation = this_period.abbreviation
  @rdates = []
end

Instance Method Details

#add_period(this_period) ⇒ Object



40
41
42
# File 'lib/ri_cal/component/t_z_info_timezone.rb', line 40

def add_period(this_period)
  @rdates << period_local_start(this_period)
end

#daylight?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ri_cal/component/t_z_info_timezone.rb', line 25

def daylight?
  @which == "DAYLIGHT"
end

#export_to(export_stream) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ri_cal/component/t_z_info_timezone.rb', line 53

def export_to(export_stream)
  export_stream.puts "BEGIN:#{@which}"
  export_stream.puts "DTSTART:#{@onset}"
  @rdates.each do |rdate|
    export_stream.puts "RDATE:#{rdate}"
  end
  export_stream.puts "TZOFFSETFROM:#{@offset_from}"
  export_stream.puts "TZOFFSETTO:#{@offset_to}"
  export_stream.puts "TZNAME:#{@abbreviation}"
  export_stream.puts "END:#{@which}"
end

#format_rfc2445_offset(seconds) ⇒ Object

:nodoc:



45
46
47
48
49
50
51
# File 'lib/ri_cal/component/t_z_info_timezone.rb', line 45

def format_rfc2445_offset(seconds) #:nodoc:
  abs_seconds = seconds.abs
  h = (abs_seconds/3600).floor
  m = (abs_seconds - (h * 3600))/60
  h *= -1 if seconds < 0
  sprintf("%+03d%02d", h, m)
end

#period_local_end(period) ⇒ Object



29
30
31
# File 'lib/ri_cal/component/t_z_info_timezone.rb', line 29

def period_local_end(period)
  (period.local_end || DateTime.parse("99990101T000000")).strftime("%Y%m%dT%H%M%S")
end

#period_local_start(period) ⇒ Object

This assumes a 1 hour shift which is why we use the previous period local end when possible



35
36
37
38
# File 'lib/ri_cal/component/t_z_info_timezone.rb', line 35

def period_local_start(period)
  shift = daylight? ? Rational(-1, 24) : Rational(1, 24)
  ((period.local_start || DateTime.parse("16010101T000000")) + shift).strftime("%Y%m%dT%H%M%S")
end