Class: Icalendar::Values::DateTime

Inherits:
Icalendar::Value show all
Includes:
TimeWithZone
Defined in:
lib/icalendar/values/date_time.rb

Defined Under Namespace

Classes: FormatError

Constant Summary collapse

FORMAT =
'%Y%m%dT%H%M%S'

Instance Attribute Summary

Attributes included from TimeWithZone

#tz_utc

Attributes inherited from Icalendar::Value

#ical_params

Instance Method Summary collapse

Methods included from TimeWithZone

#params_ical

Methods inherited from Icalendar::Value

#ical_param, #params_ical, #to_ical, #value, value_type, #value_type

Constructor Details

#initialize(value, params = {}) ⇒ DateTime

Returns a new instance of DateTime.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/icalendar/values/date_time.rb', line 12

def initialize(value, params = {})
  if value.is_a? String
    params['tzid'] = 'UTC' if value.end_with? 'Z'

    begin
      parsed_date = ::DateTime.strptime(value, FORMAT)
    rescue ArgumentError => e
      raise FormatError.new("Failed to parse \"#{value}\" - #{e.message}")
    end

    super parsed_date, params
  elsif value.respond_to? :to_datetime
    super value.to_datetime, params
  else
    super
  end
end

Instance Method Details

#<=>(other) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/icalendar/values/date_time.rb', line 38

def <=>(other)
  if other.is_a?(Icalendar::Values::Date) || other.is_a?(Icalendar::Values::DateTime)
    value_ical <=> other.value_ical
  else
    nil
  end
end

#utc?Boolean

Returns:



46
47
48
# File 'lib/icalendar/values/date_time.rb', line 46

def utc?
  value.respond_to?(:utc?) ? value.utc? : value.to_time.utc?
end

#value_icalObject



30
31
32
33
34
35
36
# File 'lib/icalendar/values/date_time.rb', line 30

def value_ical
  if tz_utc
    "#{strftime FORMAT}Z"
  else
    strftime FORMAT
  end
end