Class: Daru::Offsets::MonthEnd

Inherits:
DateOffset show all
Defined in:
lib/daru/date_time/offsets.rb

Overview

Create a month end offset

Examples:

Create a MonthEnd offset

offset = Daru::Offsets::MonthEnd.new
offset + DateTime.new(2012,5,5)
#=> #<DateTime: 2012-05-31T00:00:00+00:00 ((2456079j,0s,0n),+0s,2299161j)>

Instance Method Summary collapse

Constructor Details

#initialize(n = 1) ⇒ MonthEnd

Returns a new instance of MonthEnd.



292
293
294
# File 'lib/daru/date_time/offsets.rb', line 292

def initialize n=1
  @n = n
end

Instance Method Details

#+(date_time) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/daru/date_time/offsets.rb', line 300

def + date_time
  @n.times do
    date_time     = date_time >> 1 if on_offset?(date_time)
    days_in_month = Daru::MONTH_DAYS[date_time.month]
    days_in_month += 1 if date_time.leap? && date_time.month == 2

    date_time += (days_in_month - date_time.day)
  end

  date_time
end

#-(date_time) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/daru/date_time/offsets.rb', line 312

def - date_time
  @n.times do
    date_time = date_time << 1
    days_in_month = Daru::MONTH_DAYS[date_time.month]
    days_in_month += 1 if date_time.leap? && date_time.month == 2

    date_time += (days_in_month - date_time.day)
  end

  date_time
end

#freq_stringObject



296
297
298
# File 'lib/daru/date_time/offsets.rb', line 296

def freq_string
  (@n == 1 ? '' : @n.to_s) + 'ME'
end

#on_offset?(date_time) ⇒ Boolean

Returns:

  • (Boolean)


324
325
326
# File 'lib/daru/date_time/offsets.rb', line 324

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