Class: DaruLite::Offsets::MonthBegin

Inherits:
DateOffsetType show all
Defined in:
lib/daru_lite/date_time/offsets.rb

Overview

Create a month begin offset

Examples:

Create a MonthBegin offset

offset = DaruLite::Offsets::MonthBegin.new(2)
offset + DateTime.new(2012,5,5)
#=> #<DateTime: 2012-07-01T00:00:00+00:00 ((2456110j,0s,0n),+0s,2299161j)>

Constant Summary collapse

FREQ =
'MB'.freeze

Instance Method Summary collapse

Methods inherited from DateOffsetType

#freq_string, #initialize

Methods inherited from DateOffset

#-@, #initialize

Constructor Details

This class inherits a constructor from DaruLite::Offsets::DateOffsetType

Instance Method Details

#+(other) ⇒ Object



266
267
268
269
270
271
272
273
274
# File 'lib/daru_lite/date_time/offsets.rb', line 266

def +(other)
  @n.times do
    days_in_month = DaruLite::MONTH_DAYS[other.month]
    days_in_month += 1 if other.leap? && other.month == 2
    other += (days_in_month - other.day + 1)
  end

  other
end

#-(other) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/daru_lite/date_time/offsets.rb', line 276

def -(other)
  @n.times do
    other <<= 1 if on_offset?(other)
    other = DateTime.new(other.year, other.month, 1,
                         other.hour, other.min, other.sec)
  end

  other
end

#on_offset?(date_time) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
# File 'lib/daru_lite/date_time/offsets.rb', line 286

def on_offset?(date_time)
  date_time.day == 1
end