Exemplo n.º 1
0
def test_datatypes_datehelpers_datepartequals():
    assert TimexDateHelpers.date_part_equal(datetime(2017, 5, 29),
                                            datetime(2017, 5, 29)) is True
    assert TimexDateHelpers.date_part_equal(datetime(2017, 5, 29, 19, 30, 0),
                                            datetime(2017, 5, 29)) is False
    assert TimexDateHelpers.date_part_equal(datetime(2017, 5, 29),
                                            datetime(2017, 11, 15)) is False
Exemplo n.º 2
0
def test_datatypes_datehelpers_tomorrow():
    assert TimexDateHelpers.tomorrow(datetime(2016, 12,
                                              31)) == datetime(2017, 1, 1)
    assert TimexDateHelpers.tomorrow(datetime(2017, 1,
                                              1)) == datetime(2017, 1, 2)
    assert TimexDateHelpers.tomorrow(datetime(2017, 2,
                                              28)) == datetime(2017, 3, 1)
    assert TimexDateHelpers.tomorrow(datetime(2016, 2,
                                              28)) == datetime(2016, 2, 29)
Exemplo n.º 3
0
def test_datatypes_datehelpers_yesterday():
    assert TimexDateHelpers.yesterday(datetime(2017, 1,
                                               1)) == datetime(2016, 12, 31)
    assert TimexDateHelpers.yesterday(datetime(2017, 1,
                                               2)) == datetime(2017, 1, 1)
    assert TimexDateHelpers.yesterday(datetime(2017, 3,
                                               1)) == datetime(2017, 2, 28)
    assert TimexDateHelpers.yesterday(datetime(2016, 2,
                                               29)) == datetime(2016, 2, 28)
Exemplo n.º 4
0
def test_datatypes_datehelpers_weekofyear():
    assert TimexDateHelpers.week_of_year(datetime(2017, 1, 1)) == 1
    assert TimexDateHelpers.week_of_year(datetime(2017, 1, 2)) == 2
    assert TimexDateHelpers.week_of_year(datetime(2017, 2, 23)) == 9
    assert TimexDateHelpers.week_of_year(datetime(2017, 3, 15)) == 12
    assert TimexDateHelpers.week_of_year(datetime(2017, 9, 25)) == 40
    assert TimexDateHelpers.week_of_year(datetime(2017, 12, 31)) == 53
    assert TimexDateHelpers.week_of_year(datetime(2018, 1, 1)) == 1
    assert TimexDateHelpers.week_of_year(datetime(2018, 1, 2)) == 1
    assert TimexDateHelpers.week_of_year(datetime(2018, 1, 7)) == 1
    assert TimexDateHelpers.week_of_year(datetime(2018, 1, 8)) == 2
Exemplo n.º 5
0
    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))
Exemplo n.º 6
0
    def resolve_date_against_constraint(timex: Timex, constraint: DateRange):
        if (timex.month and timex.day_of_month) is not None:
            result = []
            year = constraint.start.year
            while year - 1 != constraint.end.year:
                t = copy.copy(timex)
                t.year = year
                result.extend(
                    TimexRangeResolver.resolve_definite_against_constraint(
                        t, constraint))
                year = year + 1
            return filter(lambda x: x != '', result)

        if timex.day_of_week is not None:
            day = timex.day_of_week - 1
            dates = TimexDateHelpers.dates_matching_day(
                day, constraint.start, constraint.end)
            result = []

            for date in dates:
                t = copy.copy(timex)
                t.day_of_week = None
                t.year = date.year
                t.month = date.month
                t.day_of_month = date.day
                result.append(t.timex_value())
            return result

        return ['']
Exemplo n.º 7
0
    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))
Exemplo n.º 8
0
def test_datatypes_datehelpers_datesmatchingday():
    day = Constants.DAYS['THURSDAY']
    start = datetime(2017, 3, 1)
    end = datetime(2017, 4, 1)
    result = TimexDateHelpers.dates_matching_day(day, start, end)

    assert len(result) == 5
    assert result[0] == datetime(2017, 3, 2)
    assert result[1] == datetime(2017, 3, 9)
    assert result[2] == datetime(2017, 3, 16)
    assert result[3] == datetime(2017, 3, 23)
    assert result[4] == datetime(2017, 3, 30)
Exemplo n.º 9
0
    def next_date_value(timex: Timex, date: datetime):
        if (timex.month and timex.day_of_month) is not None:
            return TimexValue.date_value(
                Timex(year=date.year,
                      month=timex.month,
                      day_of_month=timex.day_of_month))

        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_next_day(day - 1, date)
            return TimexValue.date_value(
                Timex(year=result.year,
                      month=result.month,
                      day_of_month=result.day))

        return ''
Exemplo n.º 10
0
def test_datatypes_datehelpers_dateofnextday_wednesday_nextweek():
    day = Constants.DAYS['WEDNESDAY']
    date = datetime(2017, 9, 28)

    assert datetime(2017, 10,
                    4) == TimexDateHelpers.date_of_next_day(day, date)
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
def test_datatypes_datehelpers_invariance():
    d = datetime(2017, 8, 25)
    before = d

    TimexDateHelpers.tomorrow(d)
    TimexDateHelpers.yesterday(d)
    TimexDateHelpers.date_part_equal(datetime.now(), d)
    TimexDateHelpers.date_part_equal(d, datetime.now())
    TimexDateHelpers.is_next_week(d, datetime.now())
    TimexDateHelpers.is_next_week(datetime.now(), d)
    TimexDateHelpers.is_last_week(datetime.now(), d)
    TimexDateHelpers.week_of_year(d)

    after = d
    assert after is before
Exemplo n.º 13
0
def test_datatypes_datehelpers_islastweek():
    today = datetime(2017, 9, 25)

    assert TimexDateHelpers.is_last_week(datetime(2017, 9, 20), today) is True
    assert TimexDateHelpers.is_last_week(datetime(2017, 9, 4), today) is False
    assert TimexDateHelpers.is_last_week(today, today) is False
Exemplo n.º 14
0
def test_datatypes_datehelpers_dateofnextday_today():
    day = Constants.DAYS['THURSDAY']
    date = datetime(2017, 9, 28)

    assert date != TimexDateHelpers.date_of_next_day(day, date)