def testGetActiveRecurrenceId(self):
        # use a RECCURRENCE-ID with TZID, just to stress the code some more
        data = '''
BEGIN:VCALENDAR
PRODID:-//My calendar product//mxm.dk//
VERSION:2.0
BEGIN:VEVENT
DTSTART:20150404T060000Z
DTEND:20150404T070000Z
RRULE:FREQ=WEEKLY
SUMMARY:Test calendar
UID:uid
END:VEVENT
BEGIN:VEVENT
DTSTART:20150411T070000Z
DTEND:20150411T080000Z
RECURRENCE-ID;TZID=UTC:20150411T060000
SUMMARY:changed event one hour later
UID:uid
END:VEVENT
END:VCALENDAR
        '''
        ical = icalendar.Calendar.from_string(data)

        start = datetime(2015, 4, 4, 6, 0, 0, tzinfo=UTC)
        cal = eventcalendar.fromICalendar(ical)

        # check that the first one is here
        instances = cal.getActiveEventInstances(
            start + timedelta(seconds=1))
        self.assertEquals(len(instances), 1)
        self.assertEquals(instances[0].start, start)

        second = timedelta(seconds=1)

        # check that the second one is not there exactly a week later,
        # because it also moved an hour
        delta = timedelta(days=7)
        instances = cal.getActiveEventInstances(start + delta + second)
        self.assertEquals(len(instances), 0)

        # check that the second one is there where it should be, an hour later
        delta = timedelta(days=7, hours=1)
        instances = cal.getActiveEventInstances(start + delta + second)
        self.assertEquals(len(instances), 1)
        self.assertEquals(instances[0].start, start + delta)

        # check that the third one is back on the hour again
        delta = timedelta(days=14)
        instances = cal.getActiveEventInstances(start + delta + second)
        self.assertEquals(len(instances), 1)
        self.assertEquals(instances[0].start, start + delta)
    def testGetActiveRecurrenceId(self):
        # use a RECCURRENCE-ID with TZID, just to stress the code some more
        data = '''
BEGIN:VCALENDAR
PRODID:-//My calendar product//mxm.dk//
VERSION:2.0
BEGIN:VEVENT
DTSTART:20150404T060000Z
DTEND:20150404T070000Z
RRULE:FREQ=WEEKLY
SUMMARY:Test calendar
UID:uid
END:VEVENT
BEGIN:VEVENT
DTSTART:20150411T070000Z
DTEND:20150411T080000Z
RECURRENCE-ID;TZID=UTC:20150411T060000
SUMMARY:changed event one hour later
UID:uid
END:VEVENT
END:VCALENDAR
        '''
        ical = icalendar.Calendar.from_string(data)

        start = datetime(2015, 4, 4, 6, 0, 0, tzinfo=UTC)
        cal = eventcalendar.fromICalendar(ical)

        # check that the first one is here
        instances = cal.getActiveEventInstances(start + timedelta(seconds=1))
        self.assertEquals(len(instances), 1)
        self.assertEquals(instances[0].start, start)

        second = timedelta(seconds=1)

        # check that the second one is not there exactly a week later,
        # because it also moved an hour
        delta = timedelta(days=7)
        instances = cal.getActiveEventInstances(start + delta + second)
        self.assertEquals(len(instances), 0)

        # check that the second one is there where it should be, an hour later
        delta = timedelta(days=7, hours=1)
        instances = cal.getActiveEventInstances(start + delta + second)
        self.assertEquals(len(instances), 1)
        self.assertEquals(instances[0].start, start + delta)

        # check that the third one is back on the hour again
        delta = timedelta(days=14)
        instances = cal.getActiveEventInstances(start + delta + second)
        self.assertEquals(len(instances), 1)
        self.assertEquals(instances[0].start, start + delta)
    def testParseCalendarWithDates(self):
        event = icalendar.Event()
        startExpected = datetime(2015, 4, 4, 0, 0, 0, tzinfo=UTC)
        endExpected = datetime(2015, 4, 5, 0, 0, 0, tzinfo=UTC)
        event['summary'] = 'Test calendar'
        event['uid'] = '42'
        event.set('dtstart', date(2015, 4, 4))
        event.set('dtend', date(2015, 4, 5))

        self._icalendar.add_component(event)

        self._calendar = eventcalendar.fromICalendar(self._icalendar)

        self.assertOneEventExpected(startExpected, endExpected)
    def testParseCalendarWithStartDateTimeAndEndDate(self):
        event = icalendar.Event()
        startExpected = datetime.datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL)
        endExpected = datetime.datetime(2015, 4, 5, 0, 0, 0, tzinfo=UTC)
        event["summary"] = "Test calendar"
        event["uid"] = "42"
        event.set("dtstart", datetime.datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL))
        event.set("dtend", datetime.date(2015, 4, 5))

        self._icalendar.add_component(event)

        self._calendar = eventcalendar.fromICalendar(self._icalendar)

        self.assertOneEventExpected(startExpected, endExpected)
    def testParseCalendarWithStartDateTimeAndEndDate(self):
        event = icalendar.Event()
        startExpected = datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL)
        endExpected = datetime(2015, 4, 5, 0, 0, 0, tzinfo=UTC)
        event['summary'] = 'Test calendar'
        event['uid'] = '42'
        event.set('dtstart', datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL))
        event.set('dtend', date(2015, 4, 5))

        self._icalendar.add_component(event)

        self._calendar = eventcalendar.fromICalendar(self._icalendar)

        self.assertOneEventExpected(startExpected, endExpected)
    def testParseCalendarWithStartDateTimeAndDuration(self):
        event = icalendar.Event()
        startExpected = datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL)
        duration = timedelta(hours=1, minutes=38, seconds=23)
        endExpected = startExpected + duration
        event['summary'] = 'Test calendar'
        event['uid'] = '42'
        event.set('dtstart', datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL))
        event.set('duration', timedelta(hours=1, minutes=38, seconds=23))

        self._icalendar.add_component(event)

        self._calendar = eventcalendar.fromICalendar(self._icalendar)

        self.assertOneEventExpected(startExpected, endExpected)
    def testParseCalendarWithStartDateTimeAndDuration(self):
        event = icalendar.Event()
        startExpected = datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL)
        duration = timedelta(hours=1, minutes=38, seconds=23)
        endExpected = startExpected + duration
        event["summary"] = "Test calendar"
        event["uid"] = "42"
        event.set("dtstart", datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL))
        event.set("duration", timedelta(hours=1, minutes=38, seconds=23))

        self._icalendar.add_component(event)

        self._calendar = eventcalendar.fromICalendar(self._icalendar)

        self.assertOneEventExpected(startExpected, endExpected)
    def testParseTimezones(self):
        # Create a calendar in Europe/Brussels timezone,
        # with an event starting at 1:00 on 26/10, and ending at 4:00 on 26/10
        # this event should be 4 hours long since there is a daylight
        # savings time switch from 3:00 to 2:00 during the night
        data = '''
BEGIN:VCALENDAR
PRODID:-//My calendar product//mxm.dk//
VERSION:2.0
BEGIN:VTIMEZONE
TZID:my_location
BEGIN:DAYLIGHT
TZOFFSETFROM:+0400
TZOFFSETTO:+0500
TZNAME:MOJITO
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0500
TZOFFSETTO:+0400
TZNAME:TEA
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART;TZID=my_location:20080926T010000
DTEND;TZID=my_location:20081126T040000
SUMMARY:4 hour event due to time zone
UID:uid
END:VEVENT
END:VCALENDAR
        '''
        ical = icalendar.Calendar.from_string(data)
        cal = eventcalendar.fromICalendar(ical)
        dateTime = datetime(2008, 9, 25, 19, 0, 0, tzinfo=UTC)
        points = cal.getPoints(dateTime, timedelta(weeks=15))
        point1 = points[0].dt
        point2 = points[1].dt
        self.failUnless(str(point1.tzinfo == 'my_location'))
        self.failUnless(point1.utcoffset() == timedelta(hours=5))
        self.failUnless(point1.dst() == timedelta(hours=1))
        self.failUnless(point1.tzname() == 'MOJITO')
        self.failUnless(str(point2.tzinfo == 'my_location'))
        self.failUnless(point2.utcoffset() == timedelta(hours=4))
        self.failUnless(point2.dst() == timedelta(hours=0))
        self.failUnless(point2.tzname() == 'TEA')
    def testParseTimezones(self):
        # Create a calendar in Europe/Brussels timezone,
        # with an event starting at 1:00 on 26/10, and ending at 4:00 on 26/10
        # this event should be 4 hours long since there is a daylight
        # savings time switch from 3:00 to 2:00 during the night
        data = '''
BEGIN:VCALENDAR
PRODID:-//My calendar product//mxm.dk//
VERSION:2.0
BEGIN:VTIMEZONE
TZID:my_location
BEGIN:DAYLIGHT
TZOFFSETFROM:+0400
TZOFFSETTO:+0500
TZNAME:MOJITO
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0500
TZOFFSETTO:+0400
TZNAME:TEA
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART;TZID=my_location:20080926T010000
DTEND;TZID=my_location:20081126T040000
SUMMARY:4 hour event due to time zone
UID:uid
END:VEVENT
END:VCALENDAR
        '''
        ical = icalendar.Calendar.from_string(data)
        cal = eventcalendar.fromICalendar(ical)
        dateTime = datetime(2008, 9, 25, 19, 0, 0, tzinfo=UTC)
        points = cal.getPoints(dateTime, timedelta(weeks=15))
        point1 = points[0].dt
        point2 = points[1].dt
        self.failUnless(str(point1.tzinfo == 'my_location'))
        self.failUnless(point1.utcoffset() == timedelta(hours=5))
        self.failUnless(point1.dst() == timedelta(hours=1))
        self.failUnless(point1.tzname() == 'MOJITO')
        self.failUnless(str(point2.tzinfo == 'my_location'))
        self.failUnless(point2.utcoffset() == timedelta(hours=4))
        self.failUnless(point2.dst() == timedelta(hours=0))
        self.failUnless(point2.tzname() == 'TEA')
    def testDaylightSavingsChange(self):
        # Create a calendar in Europe/Brussels timezone,
        # with an event starting at 1:00 on 26/10, and ending at 4:00 on 26/10
        # this event should be 4 hours long since there is a daylight
        # savings time switch from 3:00 to 2:00 during the night
        data = '''
BEGIN:VCALENDAR
PRODID:-//My calendar product//mxm.dk//
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Europe/Brussels
X-LIC-LOCATION:Europe/Brussels
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART;TZID=Europe/Brussels:20081026T010000
DTEND;TZID=Europe/Brussels:20081026T040000
SUMMARY:4 hour event due to time zone
UID:uid
END:VEVENT
END:VCALENDAR
        '''
        ical = icalendar.Calendar.from_string(data)
        cal = eventcalendar.fromICalendar(ical)
        dateTime = datetime(2008, 10, 25, 23, 0, 0, tzinfo=UTC)
        points = cal.getPoints(dateTime,
            timedelta(hours=5))
        self.assertEquals(len(points), 2)
        delta = points[1].dt - points[0].dt
        self.assertEquals(delta, timedelta(hours=4))
    def testDaylightSavingsChange(self):
        # Create a calendar in Europe/Brussels timezone,
        # with an event starting at 1:00 on 26/10, and ending at 4:00 on 26/10
        # this event should be 4 hours long since there is a daylight
        # savings time switch from 3:00 to 2:00 during the night
        data = '''
BEGIN:VCALENDAR
PRODID:-//My calendar product//mxm.dk//
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Europe/Brussels
X-LIC-LOCATION:Europe/Brussels
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART;TZID=Europe/Brussels:20081026T010000
DTEND;TZID=Europe/Brussels:20081026T040000
SUMMARY:4 hour event due to time zone
UID:uid
END:VEVENT
END:VCALENDAR
        '''
        ical = icalendar.Calendar.from_string(data)
        cal = eventcalendar.fromICalendar(ical)
        dateTime = datetime(2008, 10, 25, 23, 0, 0, tzinfo=UTC)
        points = cal.getPoints(dateTime, timedelta(hours=5))
        self.assertEquals(len(points), 2)
        delta = points[1].dt - points[0].dt
        self.assertEquals(delta, timedelta(hours=4))
    def testParseCalendar(self):
        event = icalendar.Event()
        startExpected = datetime.datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL)
        endExpected = datetime.datetime(2015, 4, 5, 0, 0, 0, tzinfo=UTC)
        contentExpected = "Test calendar"
        event["summary"] = contentExpected
        uidExpected = "uid"
        event["uid"] = uidExpected
        event.set("dtstart", datetime.datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL))
        event.set("dtend", datetime.date(2015, 4, 5))

        self._icalendar.add_component(event)

        self._calendar = eventcalendar.fromICalendar(self._icalendar)

        self.assertOneEventExpected(startExpected, endExpected)

        # FIXME: poking at internals
        eventSets = self._calendar._eventSets
        events = eventSets.values()[0].getEvents()
        self.assertEquals(events[0].content, contentExpected)
        self.assertEquals(events[0].uid, uidExpected)
    def testParseCalendar(self):
        event = icalendar.Event()
        startExpected = datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL)
        endExpected = datetime(2015, 4, 5, 0, 0, 0, tzinfo=UTC)
        contentExpected = 'Test calendar'
        event['summary'] = contentExpected
        uidExpected = 'uid'
        event['uid'] = uidExpected
        event.set('dtstart', datetime(2015, 4, 4, 8, 0, 0, tzinfo=LOCAL))
        event.set('dtend', date(2015, 4, 5))

        self._icalendar.add_component(event)

        self._calendar = eventcalendar.fromICalendar(self._icalendar)

        self.assertOneEventExpected(startExpected, endExpected)

        # FIXME: poking at internals
        eventSets = self._calendar._eventSets
        events = eventSets.values()[0].getEvents()
        self.assertEquals(events[0].content, contentExpected)
        self.assertEquals(events[0].uid, uidExpected)