class Cronofy::DateOrTime

Public Class Methods

coerce(value) click to toggle source
# File lib/cronofy/types.rb, line 118
def self.coerce(value)
  begin
    time = ISO8601Time.coerce(value)
  rescue
    begin
      date = Date.strptime(value, '%Y-%m-%d')
    rescue
    end
  end

  coerced = self.new(time: time, date: date)

  raise "Failed to coerce \"#{value}\"" unless coerced.time? or coerced.date?

  coerced
end
new(args) click to toggle source
# File lib/cronofy/types.rb, line 109
def initialize(args)
  # Prefer time if both provided as it is more accurate
  if args[:time]
    @time = args[:time]
  else
    @date = args[:date]
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/cronofy/types.rb, line 168
def ==(other)
  case other
  when DateOrTime
    if self.time?
      other.time? and self.time == other.time
    elsif self.date?
      other.date? and self.date == other.date
    else
      # Both neither date nor time
      self.time? == other.time? and self.date? == other.date?
    end
  else
    false
  end
end
date() click to toggle source
# File lib/cronofy/types.rb, line 135
def date
  @date
end
date?() click to toggle source
# File lib/cronofy/types.rb, line 139
def date?
  !!@date
end
inspect() click to toggle source
# File lib/cronofy/types.rb, line 184
def inspect
  to_s
end
time() click to toggle source
# File lib/cronofy/types.rb, line 143
def time
  @time
end
time?() click to toggle source
# File lib/cronofy/types.rb, line 147
def time?
  !!@time
end
to_date() click to toggle source
# File lib/cronofy/types.rb, line 151
def to_date
  if date?
    date
  else
    time.to_date
  end
end
to_s() click to toggle source
# File lib/cronofy/types.rb, line 188
def to_s
  if time?
    "<#{self.class} time=#{self.time}>"
  elsif date?
    "<#{self.class} date=#{self.date}>"
  else
    "<#{self.class} empty>"
  end
end
to_time() click to toggle source
# File lib/cronofy/types.rb, line 159
def to_time
  if time?
    time
  else
    # Convert dates to UTC time, not local time
    Time.utc(date.year, date.month, date.day)
  end
end