def week_date_range(year: int, week_of_year: int): date_in_week = datetime(year, 1, 1) + \ timedelta(days=(week_of_year-1)*7) start = TimexDateHelpers.date_of_last_day(Constants.DAYS['MONDAY'], date_in_week) end = TimexDateHelpers.date_of_last_day( Constants.DAYS['MONDAY'], date_in_week + timedelta(days=7)) return TimexValue.date_value( Timex(year=start.year, month=start.month, day_of_month=start.day)), TimexValue.date_value( Timex(year=start.year, month=start.month, day_of_month=end.day))
def week_date_range(year: int, week_of_year: int): """Returns start and end dates of the given ISO week number of a given year""" date_in_week = datetime(year, 1, 1) # From Wikipedia: The ISO 8601 definition for week 01 is the week with # the first Thursday of the Gregorian year (i.e. of January) in it. if date_in_week.weekday() <= 3: date_in_week -= timedelta(days=date_in_week.weekday()) else: date_in_week += timedelta(days=7 - date_in_week.weekday()) date_in_week += timedelta(days=week_of_year * 7) start = TimexDateHelpers.date_of_last_day(Constants.DAYS['MONDAY'], date_in_week) end = TimexDateHelpers.date_of_last_day( Constants.DAYS['MONDAY'], date_in_week + timedelta(days=7)) return TimexValue.date_value(Timex(year=start.year, month=start.month, day_of_month=start.day)), \ TimexValue.date_value(Timex(year=start.year, month=start.month, day_of_month=end.day))
def last_date_value(timex: Timex, date: datetime): if (timex.month and timex.day_of_month) is not None: a = TimexValue.date_value( Timex(year=date.year - 1, month=timex.month, day_of_month=timex.day_of_month)) return a if timex.day_of_week is not None: day = Constants.DAYS[ 'SUNDAY'] if timex.day_of_week == 6 else timex.day_of_week result = TimexDateHelpers.date_of_last_day(day - 1, date) return TimexValue.date_value( Timex(year=result.year, month=result.month, day_of_month=result.day)) return ''
def test_datatypes_datehelpers_dateoflastday_friday_lastweek(): day = Constants.DAYS['FRIDAY'] date = datetime(2017, 9, 28) assert datetime(2017, 9, 22) == TimexDateHelpers.date_of_last_day(day, date)