Esempio n. 1
0
 def test_utcoffset_normalize(self):
     from plone.event.utils import utcoffset_normalize
     date = mock.Mock()
     date.replace = mock.Mock(side_effect=KeyError)
     self.assertEqual(
         utcoffset_normalize(date),
         date
     )
Esempio n. 2
0
def recurrence_sequence_timedelta(start, delta=None, until=None, count=None,
                                  dst=DSTAUTO):
    """ Calculates a sequence of datetime objects from a timedelta integer,
    which defines the minutes between each occurence.

    :param start: datetime or DateTime instance of the date from which the
                  recurrence sequence is calculated.
    :type start: datetime

    :param delta: Integer which defines the minutes
                  between each date occurence.
    :type delta: integer

    :param until: datetime or DateTime instance of the date, until the
                  recurrence is calculated. If not given,
                  count or MAXDATE limit the recurrence calculation.
    :type until: datetime

    :param count: Integer which defines the number of occurences. If not given,
                  until or MAXDATE limits the recurrence calculation.
    :param count: integer

    :param dst:   Daylight Saving Time crossing behavior. DSTAUTO, DSTADJUST or
                  DSTKEEP. For more information, see
                  plone.event.utils.utcoffset_normalize.
    :param dst: string

    :return: A generator which generates a sequence of datetime instances.
    :rtype: generator

    """
    start = pydt(start)
    yield start

    if delta is None or delta < 1 or until is None:
        return

    until = pydt(until)

    before = start
    delta = datetime.timedelta(minutes=delta)
    cnt = 0
    while True:
        after = before + delta
        after = utcoffset_normalize(after, delta, dst)

        # Limit number of recurrences otherwise calculations take too long
        if MAXCOUNT and cnt + 1 > MAXCOUNT:
            break
        if count and cnt + 1 > count:
            break
        if until and utc(after) > utc(until):
            break
        cnt += 1

        yield after
        before = after
Esempio n. 3
0
def occurrences(item, min_date, max_date):
    """ Returns the occurrences for item between min and max date.
    Will return a list with a single item if the given item has no recurrence.

    """

    if not isinstance(item.start, datetime):
        item_start = dates.to_utc(datetime.utcfromtimestamp(item.start))
    else:
        item_start = item.start

    if not isinstance(item.end, datetime):
        item_end = dates.to_utc(datetime.utcfromtimestamp(item.end))
    else:
        item_end = item.end

    if not item.recurrence:

        if not overlaps(min_date, max_date, item_start, item_end):
            return []
        else:
            return [Occurrence(item, item_start, item_end)]

    tz = pytz.timezone(item.timezone)
    local_start = tz.normalize(item_start)

    _occurrences = recurrence_sequence_ical(
        start=local_start,
        recrule=item.recurrence,
        from_=min_date,
        until=max_date
    )

    result = []
    duration = item_end - item_start

    for start in _occurrences:
        start = utcoffset_normalize(start, dstmode=DSTADJUST)
        result.append(Occurrence(item, start, start + duration))

    return result