"""Utils functions related to dates manipulations.""" import datetime def get_last_monday(): """Get the datetime of the last monday at midnight. Args: None Returns: datetime.datetime: the datetime of the last monday at midnight """ today = datetime.date.today() monday = today - datetime.timedelta(days=today.weekday()) midnight = datetime.time() return datetime.datetime.combine(monday, midnight) def get_friday_of_last_week(): """Get the datetime of the last friday at midnight. Args: None Returns: datetime.datetime: the datetime of the last friday """ today = datetime.date.today() last_friday = ( today - datetime.timedelta(days=today.weekday(), weeks=1) + datetime.timedelta(days=4) ) midnight = datetime.time() return datetime.datetime.combine(last_friday, midnight) def get_friday_of_two_weeks_ago(): """Get the datetime of the friday before the last friday at midnight. Args: None Returns: datetime.datetime: the datetime of the friday before last """ return get_friday_of_last_week() - datetime.timedelta(weeks=1) def get_last_thursday(): """Get the datetime of the last thursday at midnight. Args: None Returns: datetime.datetime: the datetime of the last thursday """ today = datetime.date.today() weekday = today.weekday() thursday_week_day = 3 midnight = datetime.time() if weekday == thursday_week_day: last_thursday = today - datetime.timedelta(weeks=1) elif weekday < thursday_week_day: last_thursday = ( today - datetime.timedelta(days=today.weekday(), weeks=1) + datetime.timedelta(days=thursday_week_day) ) else: diff = weekday - thursday_week_day last_thursday = today - datetime.timedelta(days=diff) return datetime.datetime.combine(last_thursday, midnight) def get_friday_before_last(): """Get the datetime of the friday before the last at midnight. Args: None Returns: datetime.datetime: the datetime of the friday before the last """ today = datetime.date.today() weekday = today.weekday() friday_week_day = 4 if weekday <= friday_week_day: return get_friday_of_two_weeks_ago() else: return get_friday_of_last_week()