Class: DateOperations

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

Overview

0.1.3 - Explizite Definition der Metadaten-Links in der Gemspec für RubyGems.org 0.1.2 - Aktualisierung der Dokumentations-Links auf RubyDoc.info 0.1.1 - Umzug des Repositories zu Codeberg und Aktualisierung aller Homepage-URLs 0.1.0 - Anhebung der Ruby-Mindestversion auf >= 3.4.0, Aktualisierung des holidays-Gems und Code-Refactoring

Constant Summary collapse

VERSION =
'0.1.3'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.countryObject

Switches to the calendar of the respective country:

  • :de = Germany
  • :us = USA
  • ...


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

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.



36
37
38
39
# File 'lib/dateoperations.rb', line 36

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)


54
55
56
# File 'lib/dateoperations.rb', line 54

def self.holiday?(date)
  Holidays.on(date, DateOperations.country, :informal).any?
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.



43
44
45
46
# File 'lib/dateoperations.rb', line 43

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.



29
30
31
32
# File 'lib/dateoperations.rb', line 29

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.



23
24
25
26
# File 'lib/dateoperations.rb', line 23

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)


49
50
51
# File 'lib/dateoperations.rb', line 49

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