1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
module IceCube
class IcalBuilder
ICAL_DAYS = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA']
def initialize
@hash = {}
end
def self.fixnum_to_ical_day(num)
ICAL_DAYS[num]
end
def [](key)
@hash[key] ||= []
end
# Build for a single rule entry
def to_s
arr = []
if freq = @hash.delete('FREQ')
arr << "FREQ=#{freq.join(',')}"
end
arr.concat(@hash.map do |key, value|
if value.is_a?(Array)
"#{key}=#{value.join(',')}"
end
end.compact)
arr.join(';')
end
def self.ical_utc_format(time)
time = time.dup.utc
IceCube::I18n.l(time, format: '%Y%m%dT%H%M%SZ') # utc time
end
def self.ical_format(time, force_utc)
time = time.dup.utc if force_utc
if time.utc?
":#{IceCube::I18n.l(time, format: '%Y%m%dT%H%M%SZ')}" # utc time
else
";TZID=#{IceCube::I18n.l(time, format: '%Z:%Y%m%dT%H%M%S')}" # local time specified
end
end
def self.ical_duration(duration)
hours = duration / 3600; duration %= 3600
minutes = duration / 60; duration %= 60
repr = ''
repr << "#{hours}H" if hours > 0
repr << "#{minutes}M" if minutes > 0
repr << "#{duration}S" if duration > 0
"PT#{repr}"
end
end
end
|