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



38
39
40
41
42
43
44
45
46
47
# File 'lib/rdt.rb', line 38

def self.conv(str)
  case str
  when 'yesterday' then $today.add(-1)
  when 'today' then $today
  when 'tomorrow' then $today.add(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

.from_epoch(s) ⇒ RDT

Create a new RDT from seconds since epoch

Parameters:

  • s (Integer)

    Seconds since the Unix epoch (Thu Jan 1 00:00:00 UTC 1970)

Returns:

  • (RDT)

    a new RDT



19
20
21
# File 'lib/rdt.rb', line 19

def self.from_epoch(s)
  from_time(Time.at(s))
end

.from_itime(s) ⇒ RDT

Create a new RDT from seconds since iCal epoch

Parameters:

  • s (Integer)

    Seconds since the iCal epoch (Jan 1 00:00:00 UTC 2001)

Returns:

  • (RDT)

    a new RDT



28
29
30
# File 'lib/rdt.rb', line 28

def self.from_itime(s)
  from_epoch(s + ITIME)
end

.from_time(t) ⇒ RDT

Create a new RDT from a Time object

Parameters:

  • t (Time)

    The Time object

Returns:

  • (RDT)

    a new RDT



10
11
12
# File 'lib/rdt.rb', line 10

def self.from_time(t)
  new(*t.to_a[0..5].reverse, Rational((t.gmt_offset / 3600), 24))
end

Instance Method Details

#add(days) ⇒ RDT

Add a number of days accounting for daylight saving time changes

Parameters:

  • days (Integer)

    Number of days to add

Returns:

  • (RDT)

    A new RDT



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

def add(days)
  n = self + days
  t = Time.parse("#{n.year}-#{n.month}-#{n.day} #{n.hour}:#{n.min}:#{n.sec}")
  RDT.from_time(t)
end

#day_end(z = zone) ⇒ RDT

Returns Self at 23:59:59.

Parameters:

  • z (Integer) (defaults to: zone)

    Optional UTC offset

Returns:

  • (RDT)

    Self at 23:59:59



101
102
103
# File 'lib/rdt.rb', line 101

def day_end(z = zone)
  RDT.new(year, month, day, 23, 59, 59, z)
end

#day_start(z = zone) ⇒ RDT

Returns Self at 00:00:00.

Parameters:

  • z (Integer) (defaults to: zone)

    Optional UTC offset

Returns:

  • (RDT)

    Self at 00:00:00



95
96
97
# File 'lib/rdt.rb', line 95

def day_start(z = zone)
  RDT.new(year, month, day, 0, 0, 0, z)
end

#hmsArray

Returns Only the hour, min and sec of self.

Returns:

  • (Array)

    Only the hour, min and sec of self



111
112
113
# File 'lib/rdt.rb', line 111

def hms
  [ hour, min, sec ]
end

#to_aArray

Returns Self as an array.

Returns:

  • (Array)

    Self as an array

See Also:

  • Time.to_a


84
85
86
# File 'lib/rdt.rb', line 84

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

#to_iInteger

Returns Seconds since epoch.

Returns:

  • (Integer)

    Seconds since epoch



89
90
91
# File 'lib/rdt.rb', line 89

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.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rdt.rb', line 65

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

  case (self - $today).floor
  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 && $opts[:df]
  end
end

#ymdArray

Returns Only the year, month and day of self.

Returns:

  • (Array)

    Only the year, month and day of self



106
107
108
# File 'lib/rdt.rb', line 106

def ymd
  [ year, month, day ]
end