Пример #1
0
    def _addAvailabilityComponent(self, component, lowerLimit, upperLimit):
        """
        Add the specified master VAVAILABILITY Component to the instance list, expanding it
        within the supplied time range. VAVAILABILITY components are not recurring, they have an
        optional DTSTART and DTEND/DURATION defining a single time-range which may be bounded
        depending on the presence of the properties. If unbounded at one or both ends, we will
        set the time to 1/1/1900 in the past and 1/1/3000 in the future.
        @param component: the Component to expand
        @param limit: the end L{DateTime} for expansion
        """

        start = component.getStartDateUTC()

        if start:
            lowerLimit, upperLimit = self._setupLimits(start, lowerLimit,
                                                       upperLimit)

        if start is not None and start >= upperLimit:
            # If the availability is beyond the end of the range we want, ignore it
            return
        if start is None:
            start = DateTime(1900, 1, 1, 0, 0, 0, tzid=Timezone(utc=True))
        start = self.normalizeFunction(start)

        end = component.getEndDateUTC()
        if lowerLimit is not None and end is not None and end < lowerLimit:
            # If the availability is before the start of the range we want, ignore it
            return
        if end is None:
            end = DateTime(2100, 1, 1, 0, 0, 0, tzid=Timezone(utc=True))
        end = self.normalizeFunction(end)

        self.addInstance(Instance(component, start, end))
Пример #2
0
 def _deserialize(self, data):
     """
     Convert a JSON compatible serialization of this object into the actual object.
     """
     self.start = DateTime.parseText(data["start"]) if data["start"] else None
     self.end = DateTime.parseText(data["end"]) if data["end"] else None
     self.tzinfo = Timezone(tzid=data["tzinfo"]) if data["tzinfo"] else None
Пример #3
0
 def timeZoneSecondsOffset(self, relative_to_utc=False):
     if self.mTZUTC:
         return 0
     if self.mTZOffset is None:
         tz = Timezone(utc=self.mTZUTC, tzid=self.mTZID)
         self.mTZOffset = tz.timeZoneSecondsOffset(self, relative_to_utc)
     return self.mTZOffset
Пример #4
0
 def timeZoneSecondsOffset(self, relative_to_utc=False):
     if self.mTZUTC:
         return 0
     if self.mTZOffset is None:
         tz = Timezone(utc=self.mTZUTC, tzid=self.mTZID)
         self.mTZOffset = tz.timeZoneSecondsOffset(self, relative_to_utc)
     return self.mTZOffset
Пример #5
0
 def setDefaultTimezoneID(self, tzid):
     # Check for UTC
     if tzid == "UTC":
         temp = Timezone(utc=True)
         self.setDefaultTimezone(temp)
     else:
         temp = Timezone(utc=False, tzid=tzid)
         self.setDefaultTimezone(temp)
Пример #6
0
    def testDuplicateInSet(self):

        s = set((
            DateTime(2011, 1, 1, 0, 0, 0, tzid=Timezone(utc=True)),
            DateTime(2011, 1, 2, 0, 0, 0, tzid=Timezone(utc=True)),
        ))

        self.assertTrue(
            DateTime(2011, 1, 1, 0, 0, 0, tzid=Timezone(utc=True)) in s)
        self.assertFalse(
            DateTime(2011, 1, 3, 0, 0, 0, tzid=Timezone(utc=True)) in s)
Пример #7
0
    def test_truncatedApr(self):
        """
        Custom VTZ with truncated standard time - 2006. Daylight 2007 OK.
        """

        TimezoneCache.create(empty=True)
        TimezoneCache.clear()

        self.doTest("TruncatedApr01.ics",
                    DateTime(2007, 04, 01, 16, 0, 0, Timezone(utc=True)),
                    DateTime(2007, 04, 01, 17, 0, 0, Timezone(utc=True)))
Пример #8
0
    def test_truncatedDec(self):
        """
        Custom VTZ valid from 2007. Daylight 2007 OK.
        """

        TimezoneCache.create(empty=True)
        TimezoneCache.clear()

        self.doTest("TruncatedDec10.ics",
                    DateTime(2007, 12, 10, 17, 0, 0, Timezone(utc=True)),
                    DateTime(2007, 12, 10, 18, 0, 0, Timezone(utc=True)))
Пример #9
0
    def test_truncatedDecThenApr(self):
        """
        Custom VTZ valid from 2007 loaded first. Daylight 2007 OK.
        """

        TimezoneCache.create(empty=True)
        TimezoneCache.clear()

        self.doTest("TruncatedDec10.ics",
                    DateTime(2007, 12, 10, 17, 0, 0, Timezone(utc=True)),
                    DateTime(2007, 12, 10, 18, 0, 0, Timezone(utc=True)))
        self.doTest("TruncatedApr01.ics",
                    DateTime(2007, 04, 01, 16, 0, 0, Timezone(utc=True)),
                    DateTime(2007, 04, 01, 17, 0, 0, Timezone(utc=True)))
Пример #10
0
    def test_normalizeForExpand(self):
        """
        Test that dateops.normalizeForExpand works correctly on all four types of date/time: date only, floating, UTC and local time.
        """

        data = (
            (DateTime(2012, 1, 1), DateTime(2012, 1, 1)),
            (DateTime(2012, 1, 1, 10, 0, 0), DateTime(2012, 1, 1, 10, 0, 0)),
            (DateTime(2012, 1, 1, 11, 0, 0, tzid=Timezone(utc=True)), DateTime(2012, 1, 1, 11, 0, 0, tzid=Timezone(utc=True))),
            (DateTime(2012, 1, 1, 12, 0, 0, tzid=Timezone(tzid="America/New_York")), DateTime(2012, 1, 1, 17, 0, 0, tzid=Timezone(utc=True))),
        )

        for value, result in data:
            self.assertEqual(normalizeForExpand(value), result)
Пример #11
0
 def _deserialize(self, data):
     """
     Convert a JSON compatible serialization of this object into the actual object.
     """
     self.start = DateTime.parseText(data["start"]) if data["start"] else None
     self.end = DateTime.parseText(data["end"]) if data["end"] else None
     self.tzinfo = Timezone(tzid=data["tzinfo"]) if data["tzinfo"] else None
Пример #12
0
    def getNowUTC():
        # Get from posix time
        now = time.time()
        now_tm = time.gmtime(now)
        tzid = Timezone(utc=True)

        return DateTime(year=now_tm.tm_year, month=now_tm.tm_mon, day=now_tm.tm_mday, hours=now_tm.tm_hour, minutes=now_tm.tm_min, seconds=now_tm.tm_sec, tzid=tzid)
Пример #13
0
    def compareDateTime(self, comp):
        if comp is None:
            return 1
        # If either are date only, then just do date compare
        if self.mDateOnly or comp.mDateOnly:
            c = cmp(self.mYear, comp.mYear)
            if c == 0:
                c = cmp(self.mMonth, comp.mMonth)
                if c == 0:
                    c = cmp(self.mDay, comp.mDay)
            return c

        # If they have the same timezone do simple compare - no posix calc
        # needed
        elif (Timezone.same(self.mTZUTC, self.mTZID, comp.mTZUTC, comp.mTZID)):
            c = cmp(self.mYear, comp.mYear)
            if c == 0:
                c = cmp(self.mMonth, comp.mMonth)
                if c == 0:
                    c = cmp(self.mDay, comp.mDay)
                    if c == 0:
                        c = cmp(self.mHours, comp.mHours)
                        if c == 0:
                            c = cmp(self.mMinutes, comp.mMinutes)
                            if c == 0:
                                c = cmp(self.mSeconds, comp.mSeconds)
            return c

        else:
            return cmp(self.getPosixTime(), comp.getPosixTime())
Пример #14
0
    def compareDateTime(self, comp):
        if comp is None:
            return 1
        # If either are date only, then just do date compare
        if self.mDateOnly or comp.mDateOnly:
            c = cmp(self.mYear, comp.mYear)
            if c == 0:
                c = cmp(self.mMonth, comp.mMonth)
                if c == 0:
                    c = cmp(self.mDay, comp.mDay)
            return c

        # If they have the same timezone do simple compare - no posix calc
        # needed
        elif (Timezone.same(self.mTZUTC, self.mTZID, comp.mTZUTC, comp.mTZID)):
            c = cmp(self.mYear, comp.mYear)
            if c == 0:
                c = cmp(self.mMonth, comp.mMonth)
                if c == 0:
                    c = cmp(self.mDay, comp.mDay)
                    if c == 0:
                        c = cmp(self.mHours, comp.mHours)
                        if c == 0:
                            c = cmp(self.mMinutes, comp.mMinutes)
                            if c == 0:
                                c = cmp(self.mSeconds, comp.mSeconds)
            return c

        else:
            return cmp(self.getPosixTime(), comp.getPosixTime())
Пример #15
0
    def test_query_not_extended(self):
        """
        Query test - two terms not anyof
        """

        filter = caldavxml.Filter(
            caldavxml.ComponentFilter(
                *[
                    caldavxml.ComponentFilter(**{"name": ("VEVENT")}),
                    caldavxml.ComponentFilter(**{"name": ("VTODO")}),
                ], **{"name": "VCALENDAR"}))
        filter = Filter(filter)
        filter.child.settzinfo(Timezone(tzid="America/New_York"))

        expression = buildExpression(filter, self._queryFields)
        sql = CalDAVSQLQueryGenerator(expression, self, 1234)
        select, args, usedtimerange = sql.generate()

        self.assertEqual(
            select.toSQL(),
            SQLFragment(
                "select distinct RESOURCE_NAME, ICALENDAR_UID, ICALENDAR_TYPE from CALENDAR_OBJECT where CALENDAR_RESOURCE_ID = ? and ICALENDAR_TYPE = ? and ICALENDAR_TYPE = ?",
                [1234, "VEVENT", "VTODO"]))
        self.assertEqual(args, {})
        self.assertEqual(usedtimerange, False)
Пример #16
0
    def test_timerange_query(self):
        """
        Basic query test with time range
        """

        filter = caldavxml.Filter(
            caldavxml.ComponentFilter(
                *[
                    caldavxml.ComponentFilter(
                        *[
                            caldavxml.TimeRange(
                                **{
                                    "start": "20060605T160000Z",
                                    "end": "20060605T170000Z"
                                })
                        ],
                        **{"name": ("VEVENT", "VFREEBUSY", "VAVAILABILITY")})
                ], **{"name": "VCALENDAR"}))
        filter = Filter(filter)
        filter.child.settzinfo(Timezone(tzid="America/New_York"))
        j = filter.serialize()
        self.assertEqual(j["type"], "Filter")

        f = FilterBase.deserialize(j)
        self.assertTrue(isinstance(f, Filter))
        self.assertTrue(isinstance(f.child.filters[0].qualifier, TimeRange))
        self.assertTrue(
            isinstance(f.child.filters[0].qualifier.tzinfo, Timezone))
        self.assertEqual(f.child.filters[0].qualifier.tzinfo.getTimezoneID(),
                         "America/New_York")
Пример #17
0
    def test_query(self):
        """
        Basic query test - no time range
        """

        filter = caldavxml.Filter(
            caldavxml.ComponentFilter(
                *[
                    caldavxml.ComponentFilter(
                        **{"name": ("VEVENT", "VFREEBUSY", "VAVAILABILITY")})
                ], **{"name": "VCALENDAR"}))
        filter = Filter(filter)
        filter.child.settzinfo(Timezone(tzid="America/New_York"))

        expression = buildExpression(filter, self._queryFields)
        sql = CalDAVSQLQueryGenerator(expression, self, 1234)
        select, args, usedtimerange = sql.generate()

        self.assertEqual(
            select.toSQL(),
            SQLFragment(
                "select distinct RESOURCE_NAME, ICALENDAR_UID, ICALENDAR_TYPE from CALENDAR_OBJECT where CALENDAR_RESOURCE_ID = ? and ICALENDAR_TYPE in (?, ?, ?)",
                [1234, Parameter('arg1', 3)]))
        self.assertEqual(args,
                         {"arg1": ("VEVENT", "VFREEBUSY", "VAVAILABILITY")})
        self.assertEqual(usedtimerange, False)
Пример #18
0
    def match(self, component, access=None):
        """
        Returns True if the given calendar component matches this filter, False
        otherwise.
        """

        # We only care about certain access restrictions.
        if access not in (Component.ACCESS_CONFIDENTIAL,
                          Component.ACCESS_RESTRICTED):
            access = None

        # We need to prepare ourselves for a time-range query by pre-calculating
        # the set of instances up to the latest time-range limit. That way we can
        # avoid having to do some form of recurrence expansion for each query sub-part.
        maxend, isStartTime = self.getmaxtimerange()
        if maxend:
            if isStartTime:
                if component.isRecurringUnbounded():
                    # Unbounded recurrence is always within a start-only time-range
                    instances = None
                else:
                    # Expand the instances up to infinity
                    instances = component.expandTimeRanges(
                        DateTime(2100, 1, 1, 0, 0, 0, tzid=Timezone(utc=True)),
                        ignoreInvalidInstances=True)
            else:
                instances = component.expandTimeRanges(
                    maxend, ignoreInvalidInstances=True)
        else:
            instances = None
        self.child.setInstances(instances)

        # <filter> contains exactly one <comp-filter>
        return self.child.match(component, access)
Пример #19
0
    def test_query_text(self):
        """
        Basic query test with time range
        """

        filter = caldavxml.Filter(
            caldavxml.ComponentFilter(
                *[
                    caldavxml.ComponentFilter(
                        caldavxml.PropertyFilter(
                            caldavxml.TextMatch.fromString("1234", False),
                            name="UID",
                        ), **{"name": ("VEVENT")}),
                ], **{
                    "name": "VCALENDAR",
                    "test": "anyof"
                }))
        filter = Filter(filter)
        filter.child.settzinfo(Timezone(tzid="America/New_York"))
        j = filter.serialize()
        self.assertEqual(j["type"], "Filter")

        f = FilterBase.deserialize(j)
        self.assertTrue(isinstance(f, Filter))
        self.assertTrue(
            isinstance(f.child.filters[0].filters[0], PropertyFilter))
        self.assertTrue(
            isinstance(f.child.filters[0].filters[0].qualifier, TextMatch))
        self.assertEqual(f.child.filters[0].filters[0].qualifier.text, "1234")
Пример #20
0
    def test_query_extended(self):
        """
        Basic query test with time range
        """

        filter = caldavxml.Filter(
            caldavxml.ComponentFilter(
                *[
                    caldavxml.ComponentFilter(
                        *[
                            caldavxml.TimeRange(**{
                                "start": "20060605T160000Z",
                            })
                        ], **{"name": ("VEVENT")}),
                    caldavxml.ComponentFilter(**{"name": ("VTODO")}),
                ], **{
                    "name": "VCALENDAR",
                    "test": "anyof"
                }))
        filter = Filter(filter)
        filter.child.settzinfo(Timezone(tzid="America/New_York"))
        j = filter.serialize()
        self.assertEqual(j["type"], "Filter")

        f = FilterBase.deserialize(j)
        self.assertTrue(isinstance(f, Filter))
        self.assertEqual(len(f.child.filters), 2)
        self.assertTrue(isinstance(f.child.filters[0].qualifier, TimeRange))
Пример #21
0
    def test_not_in_cache(self):

        TimezoneCache.create()

        data = """BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VTIMEZONE
TZID:US-Eastern
LAST-MODIFIED:19870101T000000Z
BEGIN:STANDARD
DTSTART:19671029T020000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
TZNAME:Eastern Standard Time (US & Canada)
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:19870405T020000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
TZNAME:Eastern Daylight Time (US & Canada)
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
UID:12345-67890
DTSTART;TZID="US-Eastern":20071225T000000
DTEND;TZID="US-Eastern":20071225T010000
ATTENDEE:mailto:[email protected]
ATTENDEE:mailto:[email protected]
END:VEVENT
END:VCALENDAR
"""

        calendar = Component.fromString(data)
        if calendar.name() != "VCALENDAR":
            self.fail("Calendar is not a VCALENDAR")
        instances = calendar.expandTimeRanges(DateTime(2100, 1, 1))
        for key in instances:
            instance = instances[key]
            start = instance.start
            end = instance.end
            self.assertEqual(
                start, DateTime(2007, 12, 25, 05, 0, 0, Timezone(utc=True)))
            self.assertEqual(
                end, DateTime(2007, 12, 25, 06, 0, 0, Timezone(utc=True)))
            break
Пример #22
0
    def testDuplicateASUTC(self):

        items = ((
            DateTime(2011, 1, 1, 0, 0, 0, tzid=Timezone(utc=True)),
            DateTime(2011, 1, 1, 0, 0, 0, tzid=Timezone(utc=True)),
        ), (
            DateTime(2011, 1, 1, 0, 0, 0),
            DateTime(2011, 1, 1, 0, 0, 0),
        ), (
            DateTime(2011, 1, 1),
            DateTime(2011, 1, 1),
        ))

        for item, result in items:
            dup = item.duplicateAsUTC()
            self.assertEqual(str(dup), str(result),
                             "Failed to convert '%s'" % (item, ))
Пример #23
0
    def test_truncatedAprThenDecOK(self):
        """
        VTZ loaded from std timezone DB. 2007 OK.
        """

        TimezoneCache.create()

        self.doTest(
            "TruncatedApr01.ics",
            DateTime(2007, 04, 01, 16, 0, 0, Timezone(utc=True)),
            DateTime(2007, 04, 01, 17, 0, 0, Timezone(utc=True)),
        )
        self.doTest(
            "TruncatedDec10.ics",
            DateTime(2007, 12, 10, 17, 0, 0, Timezone(utc=True)),
            DateTime(2007, 12, 10, 18, 0, 0, Timezone(utc=True)),
        )
Пример #24
0
    def test_truncatedAprThenDecFail(self):
        """
        Custom VTZ with truncated standard time - 2006 loaded first. Daylight 2007 OK, standard 2007 wrong.
        """

        TimezoneCache.create(empty=True)
        TimezoneCache.clear()

        self.doTest(
            "TruncatedApr01.ics",
            DateTime(2007, 04, 01, 16, 0, 0, Timezone(utc=True)),
            DateTime(2007, 04, 01, 17, 0, 0, Timezone(utc=True)),
        )
        self.doTest("TruncatedDec10.ics",
                    DateTime(2007, 12, 10, 17, 0, 0, Timezone(utc=True)),
                    DateTime(2007, 12, 10, 18, 0, 0, Timezone(utc=True)),
                    testEqual=False)
Пример #25
0
def processAvailabilityFreeBusy(calendar, fbinfo, timerange):
    """
    Extract free-busy data from a VAVAILABILITY component.
    @param calendar: the L{Component} that is the VCALENDAR containing the VAVAILABILITY's.
    @param fbinfo: the tuple used to store the three types of fb data.
    @param timerange: the time range to restrict free busy data to.
    """

    for vav in [
            x for x in calendar.subcomponents() if x.name() == "VAVAILABILITY"
    ]:

        # Get overall start/end
        start = vav.getStartDateUTC()
        if start is None:
            start = DateTime(1900, 1, 1, 0, 0, 0, tzid=Timezone(utc=True))
        end = vav.getEndDateUTC()
        if end is None:
            end = DateTime(2100, 1, 1, 0, 0, 0, tzid=Timezone(utc=True))
        period = Period(start, end)
        overall = clipPeriod(period, Period(timerange.start, timerange.end))
        if overall is None:
            continue

        # Now get periods for each instance of AVAILABLE sub-components
        periods = processAvailablePeriods(vav, timerange)

        # Now invert the periods and store in accumulator
        busyperiods = []
        last_end = timerange.start
        for period in periods:
            if last_end < period.getStart():
                busyperiods.append(Period(last_end, period.getStart()))
            last_end = period.getEnd()
        if last_end < timerange.end:
            busyperiods.append(Period(last_end, timerange.end))

        # Add to actual results mapped by busy type
        fbtype = vav.propertyValue("BUSYTYPE")
        if fbtype is None:
            fbtype = "BUSY-UNAVAILABLE"

        fbinfo[fbtype_mapper.get(fbtype, 2)].extend(busyperiods)
Пример #26
0
    def test_vlarm_undefined(self):

        filter = caldavxml.Filter(
            caldavxml.ComponentFilter(
                *[
                    caldavxml.ComponentFilter(
                        *[
                            caldavxml.ComponentFilter(caldavxml.IsNotDefined(),
                                                      **{"name": "VALARM"})
                        ], **{"name": "VEVENT"})
                ], **{"name": "VCALENDAR"}))
        filter = Filter(filter)
        filter.child.settzinfo(Timezone(tzid="America/New_York"))

        self.assertFalse(
            filter.match(
                Component.fromString("""BEGIN:VCALENDAR
CALSCALE:GREGORIAN
PRODID:-//Example Inc.//Example Calendar//EN
VERSION:2.0
BEGIN:VTIMEZONE
LAST-MODIFIED:20040110T032845Z
TZID:US/Eastern
BEGIN:DAYLIGHT
DTSTART:20000404T020000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4
TZNAME:EDT
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
END:DAYLIGHT
BEGIN:STANDARD
DTSTART:20001026T020000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
TZNAME:EST
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTAMP:20051222T210412Z
CREATED:20060102T150000Z
DTSTART;TZID=US/Eastern:20130102T100000
DURATION:PT1H
RRULE:FREQ=DAILY;COUNT=5
SUMMARY:event 5
UID:[email protected]
CATEGORIES:cool,hot
CATEGORIES:warm
BEGIN:VALARM
ACTION:AUDIO
TRIGGER;RELATED=START:-PT10M
END:VALARM
END:VEVENT
END:VCALENDAR
""")))
Пример #27
0
 def sample(self):
     offset = PyDuration(seconds=int(self._helperDistribution.sample()))
     beginning = self.now(Timezone(tzid=self._tzname))
     while offset:
         start, end = self._findWorkAfter(beginning)
         if end - start > offset:
             result = start + offset
             result.setMinutes(result.getMinutes() // 15 * 15)
             result.setSeconds(0)
             return result
         offset.setDuration(offset.getTotalSeconds() - (end - start).getTotalSeconds())
         beginning = end
Пример #28
0
    def gettimezone(self):
        """
        Get the timezone to use. If none, return UTC timezone.

        @return: the L{Timezone} derived from the VTIMEZONE or utc.
        """
        calendar = self.calendar()
        if calendar is not None:
            tz = calendar.gettimezone()
            if tz is not None:
                return tz

        # Default to using utc tzinfo
        return Timezone(utc=True)
Пример #29
0
def floatoffset(dt, pytz):
    """
    Apply the timezone offset to the supplied time, then force tz to utc. This gives the local
    date-time as if the local tz were UTC. It can be used in floating time comparisons with UTC date-times.

    @param dt: a L{DateTime} object to normalize
    @param pytz: a L{Timezone} object to apply offset from
    @return: the normalized DateTime
    """

    if pytz is None:
        pytz = Timezone(utc=True)

    dt = dt.duplicate()
    dt.adjustTimezone(pytz)
    dt.setTimezoneUTC(True)
    return dt
Пример #30
0
    def test_query(self):
        """
        Basic query test - no time range
        """

        filter = caldavxml.Filter(
            caldavxml.ComponentFilter(
                *[
                    caldavxml.ComponentFilter(
                        **{"name": ("VEVENT", "VFREEBUSY", "VAVAILABILITY")})
                ], **{"name": "VCALENDAR"}))
        filter = Filter(filter)
        filter.child.settzinfo(Timezone(tzid="America/New_York"))
        j = filter.serialize()
        self.assertEqual(j["type"], "Filter")

        f = FilterBase.deserialize(j)
        self.assertTrue(isinstance(f, Filter))
Пример #31
0
    def settimezone(self, tzelement):
        """
        Set the default timezone to use with this query.
        @param calendar: a L{Component} for the VCALENDAR containing the one
            VTIMEZONE that we want
        @return: the L{Timezone} derived from the VTIMEZONE or utc.
        """

        if tzelement is None:
            tz = None
        elif isinstance(tzelement, CalDAVTimeZoneElement):
            tz = tzelement.gettimezone()
        elif isinstance(tzelement, Component):
            tz = tzelement.gettimezone()
        if tz is None:
            tz = Timezone(utc=True)
        self.child.settzinfo(tz)
        return tz
Пример #32
0
    def adjustToUTC(self):
        if self.local() and not self.mDateOnly:
            # Cache and restore and adjust the posix value to avoid a recalc since it won't change during this adjust
            tempPosix = self.mPosixTime if self.mPosixTimeCached else None

            utc = Timezone(utc=True)

            offset_from = self.timeZoneSecondsOffset()
            self.setTimezone(utc)

            self.offsetSeconds(-offset_from)

            if tempPosix is not None:
                self.mPosixTimeCached = True
                self.mPosixTime = tempPosix

            self.mTZOffset = 0

        return self
Пример #33
0
    def test_workdistribution(self):
        tzname = "US/Eastern"
        dist = WorkDistribution(["mon", "wed", "thu", "sat"], 10, 20, tzname)
        dist._helperDistribution = UniformDiscreteDistribution([35 * 60 * 60 + 30 * 60])
        dist.now = lambda tzname = None: DateTime(2011, 5, 29, 18, 5, 36, tzid=tzname)
        value = dist.sample()
        self.assertEqual(
            # Move past three workdays - monday, wednesday, thursday - using 30
            # of the hours, and then five and a half hours into the fourth
            # workday, saturday.  Workday starts at 10am, so the sample value
            # is 3:30pm, ie 1530 hours.
            DateTime(2011, 6, 4, 15, 30, 0, tzid=Timezone(tzid=tzname)),
            value
        )

        dist = WorkDistribution(["mon", "tue", "wed", "thu", "fri"], 10, 20, tzname)
        dist._helperDistribution = UniformDiscreteDistribution([35 * 60 * 60 + 30 * 60])
        value = dist.sample()
        self.assertTrue(isinstance(value, DateTime))
Пример #34
0
class TimeRange (FilterBase):
    """
    Specifies a time for testing components against.
    """

    serialized_name = "TimeRange"

    def __init__(self, xml_element):

        super(TimeRange, self).__init__(xml_element)
        if xml_element is None:
            return

        # One of start or end must be present
        if "start" not in xml_element.attributes and "end" not in xml_element.attributes:
            raise ValueError("One of 'start' or 'end' must be present in CALDAV:time-range")

        self.start = DateTime.parseText(xml_element.attributes["start"]) if "start" in xml_element.attributes else None
        self.end = DateTime.parseText(xml_element.attributes["end"]) if "end" in xml_element.attributes else None
        self.tzinfo = None


    def _deserialize(self, data):
        """
        Convert a JSON compatible serialization of this object into the actual object.
        """
        self.start = DateTime.parseText(data["start"]) if data["start"] else None
        self.end = DateTime.parseText(data["end"]) if data["end"] else None
        self.tzinfo = Timezone(tzid=data["tzinfo"]) if data["tzinfo"] else None


    def serialize(self):
        """
        Create a JSON compatible serialization of this object - will be used in a cross-pod request.
        """
        result = super(TimeRange, self).serialize()
        result.update({
            "start": self.start.getText() if self.start else None,
            "end": self.end.getText() if self.end else None,
            "tzinfo": self.tzinfo.getTimezoneID() if self.tzinfo else None,
        })
        return result


    def settzinfo(self, tzinfo):
        """
        Set the default timezone to use with this query.
        @param tzinfo: a L{Timezone} to use.
        """

        # Give tzinfo to any TimeRange we have
        self.tzinfo = tzinfo


    def valid(self, level=0):
        """
        Indicate whether the time-range is valid (must be date-time in UTC).

        @return:      True if valid, False otherwise
        """

        if self.start is not None and self.start.isDateOnly():
            log.info("start attribute in <time-range> is not a date-time: %s" % (self.start,))
            return False
        if self.end is not None and self.end.isDateOnly():
            log.info("end attribute in <time-range> is not a date-time: %s" % (self.end,))
            return False
        if self.start is not None and not self.start.utc():
            log.info("start attribute in <time-range> is not UTC: %s" % (self.start,))
            return False
        if self.end is not None and not self.end.utc():
            log.info("end attribute in <time-range> is not UTC: %s" % (self.end,))
            return False

        # No other tests
        return True


    def match(self, property, access=None):
        """
        NB This is only called when doing a time-range match on a property.
        """
        if property is None:
            return False
        else:
            return property.containsTimeRange(self.start, self.end, self.tzinfo)


    def matchinstance(self, component, instances):
        """
        Test whether this time-range element causes a match to the specified component
        using the specified set of instances to determine the expanded time ranges.
        @param component: the L{Component} to test.
        @param instances: the list of expanded instances.
        @return: True if the time-range query matches, False otherwise.
        """
        if component is None:
            return False

        assert instances is not None or self.end is None, "Failure to expand instance for time-range filter: %r" % (self,)

        # Special case open-ended unbounded
        if instances is None:
            if component.getRecurrenceIDUTC() is None:
                return True
            else:
                # See if the overridden component's start is past the start
                start, _ignore_end = component.getEffectiveStartEnd()
                if start is None:
                    return True
                else:
                    return start >= self.start

        # Handle alarms as a special case
        alarms = (component.name() == "VALARM")
        if alarms:
            testcomponent = component._parent
        else:
            testcomponent = component

        for key in instances:
            instance = instances[key]

            # First make sure components match
            if not testcomponent.same(instance.component):
                continue

            if alarms:
                # Get all the alarm triggers for this instance and test each one
                triggers = instance.getAlarmTriggers()
                for trigger in triggers:
                    if timeRangesOverlap(trigger, None, self.start, self.end, self.tzinfo):
                        return True
            else:
                # Regular instance overlap test
                if timeRangesOverlap(instance.start, instance.end, self.start, self.end, self.tzinfo):
                    return True

        return False
Пример #35
0
 def timeZoneDescriptor(self):
     tz = Timezone(utc=self.mTZUTC, tzid=self.mTZID)
     return tz.timeZoneDescriptor(self)