def days_of_week(self, date, include_weekends=True): date = util.validate_dtype(date) daynumber = date.weekday() weekstart = date - datetime.timedelta(daynumber) weekend = date + datetime.timedelta(6 - daynumber) return self.all_days_between(weekstart, weekend, include_weekends)
def holiday_name(self, date): if self.is_holiday(date): date = util.validate_dtype(date) return self.holidays_dict.get(date) else: return False
def all_days_between(from_date, to_date, include_weekends=True): f_date = util.validate_dtype(date=from_date) t_date = util.validate_dtype(date=to_date) num = f_date - t_date number_of_days = abs(num.days) all_dates_between = [ f_date + datetime.timedelta(days=x) for x in range(1, number_of_days + 1) ] if include_weekends is False: clean_dates = [] for d in all_dates_between: if d.weekday() != 5 and d.weekday() != 6: clean_dates.append(d) all_dates_between = clean_dates return all_dates_between
def week_holidays(self, date, include_weekends=False): date = util.validate_dtype(date) weekdays = self.days_of_week(date=date) month_holidays = self.month_holidays(date.year, date.month, include_weekends) week_holidays = [] for weekday in weekdays: for holiday in month_holidays: if holiday == weekday: week_holidays.append(holiday) return week_holidays
def get_next_holiday(self, date=None, include_weekends=False): date = util.validate_dtype(date=date) next_holiday = self.__next_or_prev_holiday_from_list(date, next_h=True) if include_weekends: day_number = date.weekday() if day_number < 5: for_next_weekend = 5 - day_number next_weekend = date + datetime.timedelta(days=for_next_weekend) if day_number == 5: next_weekend = date + datetime.timedelta(days=1) if day_number == 6: next_weekend = date + datetime.timedelta(days=6) if next_holiday > next_weekend: next_holiday = next_weekend return next_holiday
def get_previous_holiday(self, date=None, include_weekends=False): if date is None: date = datetime.datetime.now() else: date = util.validate_dtype(date) last_holiday = self.__next_or_prev_holiday_from_list(date, False) if include_weekends: day_number = date.weekday() if day_number < 5: for_last_weekend = 5 - day_number last_weekend = date - datetime.timedelta(days=for_last_weekend) if day_number == 5: last_weekend = date - datetime.timedelta(days=6) if day_number == 6: last_weekend = date - datetime.timedelta(days=1) if last_holiday < last_weekend: last_holiday = last_weekend return last_holiday
def is_weekend(date): date = util.validate_dtype(date=date) if date.weekday() == 5 or date.weekday() == 6: return True else: return False
def get_the_day(date): date = util.validate_dtype(date=date) return date.strftime("%A")
def days_of_year(self, date, include_weekends=True): date = util.validate_dtype(str, date) start_date = datetime.datetime(date.year, 1, 1) end_date = datetime.datetime(date.year, 12, 31) return self.all_days_between(start_date, end_date, include_weekends)
def days_of_month(self, date, include_weekends=True): date = util.validate_dtype(str, date) start_date = datetime.datetime(date.year, date.month, 1) ndays = calendar.monthrange(date.year, date.month)[1] end_date = datetime.datetime(date.year, date.month, ndays) return self.all_days_between(start_date, end_date, include_weekends)
def __get_dates_as_obj(self): h_list = self.holidays_dict.keys() dates = [util.validate_dtype(date=x) for x in h_list] return sorted(dates)
def number_of_days_to_previous_holiday(self, date, include_weekends=False): date = util.validate_dtype(date=date) last_holiday = self.get_previous_holiday(date, include_weekends) diff = date - last_holiday return diff.days
def number_of_days_to_next_holiday(self, date, include_weekends=False): date = util.validate_dtype(date=date) next_holiday = self.get_next_holiday(date, include_weekends) diff = next_holiday - date return diff.days