Class: Icalendar::Values::DateTime

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

Defined Under Namespace

Classes: FormatError

Constant Summary collapse

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

Constants inherited from Icalendar::Value

Icalendar::Value::VALUE_TYPE_GSUB_REGEX_1, Icalendar::Value::VALUE_TYPE_GSUB_REGEX_2

Instance Attribute Summary

Attributes included from Helpers::TimeWithZone

#timezone_store, #tz_utc

Attributes inherited from Icalendar::Value

#ical_params

Instance Method Summary collapse

Methods included from Helpers::TimeWithZone

#__getobj__, #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.



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

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



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

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:



48
49
50
# File 'lib/icalendar/values/date_time.rb', line 48

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

#value_icalObject



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

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