def resolve_time_range(timex: Timex):
        if timex.part_of_day is not None:
            range = TimexResolver.part_of_day_timerange(timex)

            entry = Entry()

            entry.timex = timex.timex_value()
            entry.type = 'timerange'
            entry.start = range[0]
            entry.end = range[1]
            entry.value = None

            return [entry]

        else:
            range = TimexHelpers.expand_time_range(timex)
            entry = Entry()

            entry.timex = timex.timex_value()
            entry.type = 'timerange'
            entry.start = TimexValue.time_value(range.start)
            entry.end = TimexValue.time_value(range.end)
            entry.value = None

            return [entry]
    def resolve_definite_daterange(timex: Timex):
        date_range = TimexHelpers.expand_datetime_range(timex)

        entry = Entry()
        entry.timex = timex.timex_value()
        entry.type = 'daterange'
        entry.start = TimexValue.date_value(date_range.start)
        entry.end = TimexValue.date_value(date_range.end)

        return [entry]
    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 resolve_date_timerange(timex: Timex):
        if timex.part_of_day is not None:
            date = TimexValue.date_value(timex)
            time_range = TimexResolver.part_of_day_timerange(timex)

            entry = Entry()

            entry.timex = timex.timex_value()
            entry.type = 'datetimerange'
            entry.start = f'{date} {time_range[0]}'
            entry.end = f'{date} {time_range[1]}'
            entry.value = None

            return [entry]
        else:
            time_range = TimexHelpers.expand_datetime_range(timex)

            entry = Entry()

            entry.timex = timex.timex_value()
            entry.type = 'datetimerange'
            entry.start = f'{TimexValue.date_value(time_range.start)} {TimexValue.time_value(time_range.start)}'
            entry.end = f'{TimexValue.date_value(time_range.end)} {TimexValue.time_value(time_range.end)}'
            entry.value = None

            return [entry]
    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 ''
    def resolve_definite(timex: Timex):
        entry = Entry()
        entry.timex = timex.timex_value()
        entry.type = 'date'
        entry.value = TimexValue.date_value(timex)
        entry.start = None
        entry.end = None

        return [entry]
    def resolve_duration(timex: Timex):
        entry = Entry()

        entry.timex = timex.timex_value()
        entry.type = 'duration'
        entry.value = TimexValue.duration_value(timex)
        entry.start = None
        entry.end = None

        return [entry]
    def resolve_time(timex: Timex):
        entry = Entry()

        entry.timex = timex.timex_value()
        entry.type = 'time'
        entry.value = TimexValue.time_value(timex)
        entry.start = None
        entry.end = None

        return [entry]
示例#9
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))
 def month_date_range(year: int, month: int):
     return TimexValue.date_value(
         Timex(year=year, month=month,
               day_of_month=1)), TimexValue.date_value(
                   Timex(year=year, month=month + 1, day_of_month=1))
 def year_date_range(year: int) -> (Timex, Timex):
     return TimexValue.date_value(Timex(
         year=year, month=1, day_of_month=1)), TimexValue.date_value(
             Timex(year=year + 1, month=1, day_of_month=1))