Class: Daru::DateOffset
- Inherits:
-
Object
- Object
- Daru::DateOffset
- Defined in:
- lib/daru/date_time/offsets.rb
Overview
Generic class for generating date offsets.
Direct Known Subclasses
Offsets::MonthBegin, Offsets::MonthEnd, Offsets::Tick, Offsets::Week, Offsets::YearBegin, Offsets::YearEnd
Instance Method Summary collapse
-
#+(date_time) ⇒ Object
Offset a DateTime forward.
-
#-(date_time) ⇒ Object
Offset a DateTime backward.
-
#initialize(opts = {}) ⇒ DateOffset
constructor
A Daru::DateOffset object is created by a passing certain options to the constructor, which determine the kind of offset the object will support.
Constructor Details
#initialize(opts = {}) ⇒ DateOffset
A Daru::DateOffset object is created by a passing certain options to the constructor, which determine the kind of offset the object will support.
You can pass one of the following options followed by their number to the DateOffset constructor:
-
:secs - Create a seconds offset
-
:mins - Create a minutes offset
-
:hours - Create an hours offset
-
:days - Create a days offset
-
:weeks - Create a weeks offset
-
:months - Create a months offset
-
:years - Create a years offset
Additionaly, passing the ‘:n` option will apply the offset that many times.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/daru/date_time/offsets.rb', line 38 def initialize opts={} n = opts[:n] || 1 @offset = case when opts[:secs] Offsets::Second.new(n*opts[:secs]) when opts[:mins] Offsets::Minute.new(n*opts[:mins]) when opts[:hours] Offsets::Hour.new(n*opts[:hours]) when opts[:days] Offsets::Day.new(n*opts[:days]) when opts[:weeks] Offsets::Day.new(7*n*opts[:weeks]) when opts[:months] Offsets::Month.new(n*opts[:months]) when opts[:years] Offsets::Year.new(n*opts[:years]) end end |
Instance Method Details
#+(date_time) ⇒ Object
Offset a DateTime forward.
63 64 65 |
# File 'lib/daru/date_time/offsets.rb', line 63 def + date_time @offset + date_time end |
#-(date_time) ⇒ Object
Offset a DateTime backward.
70 71 72 |
# File 'lib/daru/date_time/offsets.rb', line 70 def - date_time @offset - date_time end |