Class: ICalPal::RDT
- Inherits:
-
DateTime
- Object
- DateTime
- ICalPal::RDT
- Defined in:
- lib/rdt.rb
Overview
Child class of DateTime that adds support for relative dates (RelativeDateTime).
Class Method Summary collapse
-
.conv(str) ⇒ RDT
Convert a String to an RDT.
-
.from_epoch(s) ⇒ RDT
Create a new RDT from seconds since epoch.
-
.from_itime(s) ⇒ RDT
Create a new RDT from seconds since iCal epoch.
-
.from_time(t) ⇒ RDT
Create a new RDT from a Time object.
Instance Method Summary collapse
-
#add(days) ⇒ RDT
Add a number of days accounting for daylight saving time changes.
-
#day_end(z = zone) ⇒ RDT
Self at 23:59:59.
-
#day_start(z = zone) ⇒ RDT
Self at 00:00:00.
-
#hms ⇒ Array
Only the hour, min and sec of self.
-
#to_a ⇒ Array
Self as an array.
-
#to_i ⇒ Integer
Seconds since epoch.
-
#to_s ⇒ String
(also: #inspect)
Values can be day before yesterday,
yesterday,today,tomorrow, day after tomorrow, or the result from strftime. -
#ymd ⇒ Array
Only the year, month and day of self.
Class Method Details
.conv(str) ⇒ RDT
Convert a String to an 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
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
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
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
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.
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.
95 96 97 |
# File 'lib/rdt.rb', line 95 def day_start(z = zone) RDT.new(year, month, day, 0, 0, 0, z) end |
#hms ⇒ Array
Returns Only the hour, min and sec of self.
111 112 113 |
# File 'lib/rdt.rb', line 111 def hms [ hour, min, sec ] end |
#to_a ⇒ Array
Returns Self as an array.
84 85 86 |
# File 'lib/rdt.rb', line 84 def to_a [ year, month, day, hour, min, sec ] end |
#to_i ⇒ Integer
Returns Seconds since epoch.
89 90 91 |
# File 'lib/rdt.rb', line 89 def to_i to_time.to_i end |
#to_s ⇒ String Also known as: inspect
Values can be day before yesterday, yesterday, today, tomorrow, day after tomorrow, or the result from strftime
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 |
#ymd ⇒ Array
Returns Only the year, month and day of self.
106 107 108 |
# File 'lib/rdt.rb', line 106 def ymd [ year, month, day ] end |