Class: ICalPal::RDT

Inherits:
DateTime
  • Object
show all
Defined in:
lib/rdt.rb

Overview

Child class of DateTime that adds support for relative dates (RelativeDateTime).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.conv(str) ⇒ RDT

Convert a String to an RDT

Parameters:

  • str (String)

    can be yesterday, today, tomorrow, +N, or -N. Otherwise use DateTime.parse.

Returns:

  • (RDT)

    a new RDT



13
14
15
16
17
18
19
20
21
22
# File 'lib/rdt.rb', line 13

def self.conv(str)
  case str
  when 'yesterday' then $today - 1
  when 'today' then $today
  when 'tomorrow' then $today + 1
  when /^\+([0-9]+)/ then $today + Regexp.last_match(1).to_i
  when /^-([0-9]+)/ then $today - Regexp.last_match(1).to_i
  else parse(str)
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • to_s


60
61
62
# File 'lib/rdt.rb', line 60

def ==(other)
  self.to_s == other.to_s
end

#to_aArray

Returns Self as an array.

Returns:

  • (Array)

    Self as an array

See Also:

  • Time.to_a


48
49
50
# File 'lib/rdt.rb', line 48

def to_a
  [ year, month, day, hour, min, sec ]
end

#to_iInteger

Returns Seconds since epoch.

Returns:

  • (Integer)

    Seconds since epoch



53
54
55
# File 'lib/rdt.rb', line 53

def to_i
  to_time.to_i
end

#to_sString Also known as: inspect

Values can be day before yesterday, yesterday, today, tomorrow, day after tomorrow, or the result from strftime

Returns:

  • (String)

    A string representation of self relative to today.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rdt.rb', line 30

def to_s
  return strftime($opts[:df]) if $opts[:nrd] && $opts[:df]

  case Integer(RDT.new(year, month, day) - $today)
  when -2 then 'day before yesterday'
  when -1 then 'yesterday'
  when 0 then 'today'
  when 1 then 'tomorrow'
  when 2 then 'day after tomorrow'
  else strftime($opts[:df]) if $opts[:df]
  end
end