Module: UkWorkingDays::DateExtensions

Included in:
Date, Time
Defined in:
lib/uk_working_days/date_extensions.rb

Overview

Extensions to the Date and DateTime classes

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#next_working_day(count = 1) ⇒ Object

Returns the next (or count’th) working day



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/uk_working_days/date_extensions.rb', line 25

def next_working_day(count = 1)
  negative = count < 0
  count = count.abs
  date = negative ? yesterday : tomorrow
  
  loop do
    count -= 1 if date.working_day?
    return date if count.zero? 
    
    date += negative ? -1 : 1
  end
end

#previous_working_day(count = 1) ⇒ Object

The previous (or count’th) working day



39
40
41
# File 'lib/uk_working_days/date_extensions.rb', line 39

def previous_working_day(count = 1)
  next_working_day(-count)
end

#public_holiday?Boolean

Returns true if this day is a bank holiday

Returns:

  • (Boolean)


15
16
17
# File 'lib/uk_working_days/date_extensions.rb', line 15

def public_holiday?
  Date.public_holidays(year).include?(to_date)
end

#weekday?Boolean

Returns true if this day is in the week

Returns:

  • (Boolean)


5
6
7
# File 'lib/uk_working_days/date_extensions.rb', line 5

def weekday?
  ! weekend?
end

#weekend?Boolean

Returns true if this day is on the weekend

Returns:

  • (Boolean)


10
11
12
# File 'lib/uk_working_days/date_extensions.rb', line 10

def weekend?
  wday == 0 || wday == 6
end

#working_day?Boolean

Returns true if this day is a normal weekday

Returns:

  • (Boolean)


20
21
22
# File 'lib/uk_working_days/date_extensions.rb', line 20

def working_day?
  weekday? && ! public_holiday?
end