Module: Runt::TExprUtils

Included in:
DIMonth, WIMonth
Defined in:
lib/runt/temporalexpression.rb

Overview

Utility methods common to some expressions

Instance Method Summary collapse

Instance Method Details

#days_left_in_month(date) ⇒ Object



247
248
249
# File 'lib/runt/temporalexpression.rb', line 247

def days_left_in_month(date)
  return max_day_of_month(date) - date.day
end

#max_day_of_month(date) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/runt/temporalexpression.rb', line 251

def max_day_of_month(date)
  result = 1
  next_month = nil
  if(date.mon==12)
    next_month = Date.new(date.year+1,1,1)
  else
    next_month = Date.new(date.year,date.mon+1,1)
  end
  date.step(next_month,1){ |d| result=d.day unless d.day < result }
  result
end

#week_from_end_matches?(index, date) ⇒ Boolean

Returns:

  • (Boolean)


275
276
277
278
# File 'lib/runt/temporalexpression.rb', line 275

def week_from_end_matches?(index,date)
  n = days_left_in_month(date) + 1
  week_in_month(n)==index.abs
end

#week_from_start_matches?(index, date) ⇒ Boolean

Returns:

  • (Boolean)


271
272
273
# File 'lib/runt/temporalexpression.rb', line 271

def week_from_start_matches?(index,date)
  week_in_month(date.day)==index
end

#week_in_month(day_in_month) ⇒ Object



243
244
245
# File 'lib/runt/temporalexpression.rb', line 243

def week_in_month(day_in_month)
  ((day_in_month - 1) / 7) + 1
end

#week_matches?(index, date) ⇒ Boolean

Returns:

  • (Boolean)


263
264
265
266
267
268
269
# File 'lib/runt/temporalexpression.rb', line 263

def week_matches?(index,date)
  if(index > 0)
    return week_from_start_matches?(index,date)
  else
    return week_from_end_matches?(index,date)
  end
end