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



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

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 + $1.to_i
  when /^\-([0-9]+)/ then $today - $1.to_i
  else parse(str)
  end
end

Instance Method Details

#==(obj) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • to_s


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

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

#to_aArray

Returns Self as an array.

Returns:

  • (Array)

    Self as an array

See Also:

  • Time.to_a


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

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

#to_iInteger

Returns Seconds since epoch.

Returns:

  • (Integer)

    Seconds since epoch



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

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.



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

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