Module: WorkCalendar

Defined in:
lib/work_calendar.rb,
lib/work_calendar/version.rb

Constant Summary collapse

SPECIAL_DAYS =
{
  2018 => {
    12 => {
      workdays: [29],
      holidays: [31]
    }
  },
  2019 => {
    1 => {
      holidays: [1, 2, 3, 4, 7, 8]
    },
    3 => {
      holidays: [8]
    },
    5 => {
      holidays: [1, 2, 3, 9, 10]
    },
    6 => {
      holidays: [12]
    },
    11 => {
      holidays: [4]
    }
  },
  2020 => {
    1 => {
      holidays: [1, 2, 3, 6, 7, 8]
    },
    2 => {
      holidays: [24]
    },
    3 => {
      holidays: [9]
    },
    5 => {
      holidays: [1, 4, 5, 11]
    },
    6 => {
      holidays: [12]
    },
    11 => {
      holidays: [4]
    }
  },
  2021 => {
    1 => {
      holidays: [1, 4, 5, 6, 7, 8]
    },
    2 => {
      workdays: [20],
      holidays: [22, 23]
    },
    3 => {
      holidays: [8]
    },
    5 => {
      holidays: [3, 10]
    },
    6 => {
      holidays: [14]
    },
    11 => {
      holidays: [4, 5]
    },
    12 => {
      holidays: [31]
    }
  },
  2022 => {
    1 => {
      holidays: [3, 4, 5, 6, 7]
    },
    2 => {
      holidays: [23]
    },
    3 => {
      workdays: [5],
      holidays: [7, 8]
    },
    5 => {
      holidays: [2, 3, 9, 10]
    },
    6 => {
      holidays: [13]
    },
    11 => {
      holidays: [4]
    }
  },
  2023 => {
    1 => {
      holidays: [2, 3, 4, 5, 6]
    },
    2 => {
      holidays: [23, 24]
    },
    3 => {
      holidays: [8]
    },
    5 => {
      holidays: [1, 8, 9]
    },
    6 => {
      holidays: [12]
    },
    11 => {
      holidays: [6]
    }
  },
  2024 => {
    1 => {
      holidays: [1, 2, 3, 4, 5, 8]
    },
    2 => {
      holidays: [23]
    },
    3 => {
      holidays: [8]
    },
    4 => {
      workdays: [27],
      holidays: [29, 30]
    },
    5 => {
      holidays: [1, 9, 10]
    },
    6 => {
      holidays: [12]
    },
    11 => {
      workdays: [2],
      holidays: [4]
    },
    12 => {
      workdays: [28],
      holidays: [30, 31]
    }
  },
  2025 => {
    1 => {
      holidays: [1, 2, 3, 6, 7, 8]
    },
    5 => {
      holidays: [1, 2, 8, 9]
    },
    6 => {
      holidays: [12, 13]
    },
    11 => {
      workdays: [1],
      holidays: [3, 4]
    },
    12 => {
      holidays: [31]
    }
  }
}.freeze
VERSION =
'1.0.6'

Class Method Summary collapse

Class Method Details

.first_workday_since(date = Time.zone.today) ⇒ Object



191
192
193
194
195
# File 'lib/work_calendar.rb', line 191

def first_workday_since(date = Time.zone.today)
  return date if workday?(date)

  _next_workday(date)
end

.last_workday_since(date = Time.zone.today) ⇒ Object



197
198
199
200
201
# File 'lib/work_calendar.rb', line 197

def last_workday_since(date = Time.zone.today)
  return date if workday?(date)

  _prev_workday(date)
end

.next_workday(num = 1, date = Time.zone.today) ⇒ Object



181
182
183
184
# File 'lib/work_calendar.rb', line 181

def next_workday(num = 1, date = Time.zone.today)
  num.times { date = _next_workday(date) }
  date
end

.prev_workday(num = 1, date = Time.zone.today) ⇒ Object



186
187
188
189
# File 'lib/work_calendar.rb', line 186

def prev_workday(num = 1, date = Time.zone.today)
  num.times { date = _prev_workday(date) }
  date
end

.workday?(date = Time.zone.today) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/work_calendar.rb', line 166

def workday?(date = Time.zone.today)
  year_hash = SPECIAL_DAYS[date.year]
  if year_hash
    month_hash = year_hash[date.month]
    if month_hash
      holidays = month_hash[:holidays]
      return false if holidays&.include?(date.day)

      workdays = month_hash[:workdays]
      return true if workdays&.include?(date.day)
    end
  end
  (1..5).cover?(date.wday)
end