Class: Daru::Offsets::MonthBegin

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

Overview

Create a month begin offset

Examples:

Create a MonthBegin offset

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

Instance Method Summary collapse

Constructor Details

#initialize(n = 1) ⇒ MonthBegin

Returns a new instance of MonthBegin.



251
252
253
# File 'lib/daru/date_time/offsets.rb', line 251

def initialize n=1
  @n = n
end

Instance Method Details

#+(date_time) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'lib/daru/date_time/offsets.rb', line 259

def + date_time
  @n.times do
    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 + 1)
  end

  date_time
end

#-(date_time) ⇒ Object



269
270
271
272
273
274
275
276
277
# File 'lib/daru/date_time/offsets.rb', line 269

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

  date_time
end

#freq_stringObject



255
256
257
# File 'lib/daru/date_time/offsets.rb', line 255

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

#on_offset?(date_time) ⇒ Boolean

Returns:

  • (Boolean)


279
280
281
# File 'lib/daru/date_time/offsets.rb', line 279

def on_offset? date_time
  date_time.day == 1
end