Class: DateOperations

Inherits:
Object
  • Object
show all
Defined in:
lib/dateoperations.rb

Overview

Different date operations, primarily around business days, based on the Holidays Gem.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.countryObject

Switches to the calendar of the respective country:

  • :de = Germany

  • :us = USA



16
17
18
# File 'lib/dateoperations.rb', line 16

def country
  @country
end

Class Method Details

.business_days_between(start_date, end_date) ⇒ Object

Returns an array with dates for the business days (week days (Monday to Friday) without holidays). between a given start and end date.



33
34
35
36
# File 'lib/dateoperations.rb', line 33

def self.business_days_between(start_date, end_date)
  week_days = week_days_between(start_date, end_date)
  week_days.reject { |b| holiday?(b) }
end

.holiday?(date) ⇒ Boolean

Checks whether a given date is a holiday based on the calendar for the respective country.

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/dateoperations.rb', line 51

def self.holiday?(date)
  return false if Holidays.on(date, DateOperations.country, :informal).empty?
  true
end

.number_of_business_days_between(start_date, end_date) ⇒ Object

Returns the number of business days (week days (Monday to Friday) without holidays). between a given start and end date.



40
41
42
43
# File 'lib/dateoperations.rb', line 40

def self.number_of_business_days_between(start_date, end_date)
  business_days = business_days_between(start_date, end_date)
  business_days.length
end

.number_of_week_days_between(start_date, end_date) ⇒ Object

returns the number of week days (Monday to Friday) between a given start and end date.



26
27
28
29
# File 'lib/dateoperations.rb', line 26

def self.number_of_week_days_between(start_date, end_date)
  week_days = week_days_between(start_date, end_date)
  week_days.length
end

.week_days_between(start_date, end_date) ⇒ Object

Returns an array with dates for the week days (Monday to Friday) between a given start and end date.



20
21
22
23
# File 'lib/dateoperations.rb', line 20

def self.week_days_between(start_date, end_date)
  week_day_nbrs = (1..5)
  (start_date..end_date).select { |w| week_day_nbrs.include?(w.wday) }
end

.weekend?(date) ⇒ Boolean

Checks whether a given date is a weekend (Saturday, Sunday).

Returns:

  • (Boolean)


46
47
48
# File 'lib/dateoperations.rb', line 46

def self.weekend?(date)
  date.saturday? || date.sunday?
end