module Kalendor::DateHelper
Constants
- MONTH_SIZES
Public Instance Methods
array_wrap(thing ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 5 def array_wrap thing ; thing.is_a?(Array) ? thing : [thing] ; end
beginning_of_month(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 46 def beginning_of_month d ; build(d.year, d.month, 1) ; end
beginning_of_week(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 51 def beginning_of_week d ; d - ((d.wday - 1) % 7) ; end
beginning_of_year(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 41 def beginning_of_year d ; build(d.year, 1, 1) ; end
build(y, m, d)
click to toggle source
# File lib/kalendor/date_helper.rb, line 16 def build y, m, d if m < 1 build( y - 1, m + 12, d ) elsif m > 12 build( y + 1, m - 12, d ) else s = MONTH_SIZES[m] if d < 1 build(y, m - 1, d + s) elsif d > s build(y, m + 1, d - s) else ::Date.new(y, m, d) end end end
date_set(schedule, from, upto ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 6 def date_set schedule, from, upto ; Set.new(schedule.get_dates(from, upto)) ; end
day(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 36 def day d ; d.day ; end
end_of_month(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 47 def end_of_month d ; build(d.year, d.month, last_day_of_month(d.month)) ; end
end_of_week(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 52 def end_of_week d ; d + ((7 - d.wday) % 7) ; end
end_of_year(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 42 def end_of_year d ; build(d.year, 12, 31) ; end
last_day_of_month(m ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 33 def last_day_of_month m ; MONTH_SIZES[m] ; end
last_month(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 44 def last_month d ; build(d.year, m - 1, d.day) ; end
last_week(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 49 def last_week d ; d - 7 ; end
last_year(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 39 def last_year d ; build(d.year - 1, d.month, d.day) ; end
month(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 35 def month d ; d.month ; end
next_month(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 45 def next_month d ; d.next_month ; end
next_week(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 50 def next_week d ; d + 7 ; end
next_year(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 40 def next_year d ; d.next_year ; end
nth_day_of_month(d)
click to toggle source
# File lib/kalendor/date_helper.rb, line 57 def nth_day_of_month d 1 + ( (d.mday - 1) / 7 ) end
nth_day_of_month?(d, n)
click to toggle source
return true if this is the nth of this day within the month, for example, if n is 2, and this is the second wednesday of the month, return true. If n is -1, and this is the last saturday of the month, return true. It doesn't matter which day it is, it matters whether it's the first, second, third, etc, or if it's the last, second last, third last, etc
# File lib/kalendor/date_helper.rb, line 74 def nth_day_of_month? d, n case n <=> 0 when -1 nth_last_day_of_month(d) == n when 0 raise ArgumentError.new("must be non-zero integer") when 1 nth_day_of_month(d) == n end end
nth_last_day_of_month(d)
click to toggle source
# File lib/kalendor/date_helper.rb, line 61 def nth_last_day_of_month d last_day = end_of_month(d).mday - 1 - (last_day - d.mday) / 7 end
to_date(d)
click to toggle source
# File lib/kalendor/date_helper.rb, line 8 def to_date d if d.is_a?(Date) ; d elsif d.respond_to?(:to_date) ; d.to_date elsif d.is_a?(Array) ; Date.new(*(d.map(&:to_i))) elsif d ; Date.parse(d.to_s) end end
tomorrow(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 55 def tomorrow d ; d + 1 ; end
week_day(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 37 def week_day d ; d.wday ; end
year(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 34 def year d ; d.year ; end
yesterday(d ;)
click to toggle source
# File lib/kalendor/date_helper.rb, line 54 def yesterday d ; d - 1 ; end