def test_copyPackage(self): """ Test that copying of the tz package works. """ self.patch(config, "UsePackageTimezones", True) TimezoneCache.clear() TimezoneCache.create() self.assertFalse( os.path.exists(os.path.join(config.DataRoot, "zoneinfo"))) self.assertFalse( os.path.exists( os.path.join(config.DataRoot, "zoneinfo", "America", "New_York.ics"))) pkg_tz = readTZ("America/New_York") self.patch(config, "UsePackageTimezones", False) TimezoneCache.clear() TimezoneCache.create() self.assertTrue( os.path.exists(os.path.join(config.DataRoot, "zoneinfo"))) self.assertTrue( os.path.exists( os.path.join(config.DataRoot, "zoneinfo", "America", "New_York.ics"))) copy_tz = readTZ("America/New_York") self.assertEqual(str(pkg_tz), str(copy_tz))
def test_ReadTZCached(self): self.assertTrue( readTZ("America/New_York").find("TZID:America/New_York") != -1) self.assertTrue( readTZ("America/New_York").find("TZID:America/New_York") != -1) self.assertRaises(TimezoneException, readTZ, "America/Pittsburgh") self.assertRaises(TimezoneException, readTZ, "America/Pittsburgh")
def doPOSTGet(self, request): """ Return the specified timezone data. """ tzid = request.args.get("tzid", ()) if len(tzid) != 1: raise HTTPError(ErrorResponse( responsecode.BAD_REQUEST, (calendarserver_namespace, "valid-timezone"), "Invalid tzid query parameter", )) tzid = tzid[0] try: tzdata = readTZ(tzid) except TimezoneException: raise HTTPError(ErrorResponse( responsecode.NOT_FOUND, (calendarserver_namespace, "timezone-available"), "Timezone not found", )) response = Response() response.stream = MemoryStream(tzdata) response.headers.setHeader("content-type", MimeType.fromString("text/calendar; charset=utf-8")) return response
def doPOSTGet(self, request): """ Return the specified timezone data. """ tzid = request.args.get("tzid", ()) if len(tzid) != 1: raise HTTPError( ErrorResponse( responsecode.BAD_REQUEST, (calendarserver_namespace, "valid-timezone"), "Invalid tzid query parameter", )) tzid = tzid[0] try: tzdata = readTZ(tzid) except TimezoneException: raise HTTPError( ErrorResponse( responsecode.NOT_FOUND, (calendarserver_namespace, "timezone-available"), "Timezone not found", )) response = Response() response.stream = MemoryStream(tzdata) response.headers.setHeader( "content-type", MimeType.fromString("text/calendar; charset=utf-8")) return response
def doPOSTExpand(self, request): """ Expand a timezone within specified start/end dates. """ tzid = request.args.get("tzid", ()) if len(tzid) != 1: raise HTTPError(ErrorResponse( responsecode.BAD_REQUEST, (calendarserver_namespace, "valid-timezone"), "Invalid tzid query parameter", )) tzid = tzid[0] try: tzdata = readTZ(tzid) except TimezoneException: raise HTTPError(ErrorResponse( responsecode.NOT_FOUND, (calendarserver_namespace, "timezone-available"), "Timezone not found", )) try: start = request.args.get("start", ()) if len(start) != 1: raise ValueError() start = PyCalendarDateTime.parseText(start[0]) except ValueError: raise HTTPError(ErrorResponse( responsecode.BAD_REQUEST, (calendarserver_namespace, "valid-start-date"), "Invalid start query parameter", )) try: end = request.args.get("end", ()) if len(end) != 1: raise ValueError() end = PyCalendarDateTime.parseText(end[0]) if end <= start: raise ValueError() except ValueError: raise HTTPError(ErrorResponse( responsecode.BAD_REQUEST, (calendarserver_namespace, "valid-end-date"), "Invalid end query parameter", )) # Now do the expansion (but use a cache to avoid re-calculating TZs) observances = self.cache.get((tzid, start, end), None) if observances is None: observances = tzexpand(tzdata, start, end) self.cache[(tzid, start, end)] = observances # Turn into XML result = customxml.TZData( *[customxml.Observance(customxml.Onset(onset), customxml.UTCOffset(utc_offset)) for onset, utc_offset in observances] ) return XMLResponse(responsecode.OK, result)
def doPOSTExpand(self, request): """ Expand a timezone within specified start/end dates. """ tzid = request.args.get("tzid", ()) if len(tzid) != 1: raise HTTPError(ErrorResponse( responsecode.BAD_REQUEST, (calendarserver_namespace, "valid-timezone"), "Invalid tzid query parameter", )) tzid = tzid[0] try: tzdata = readTZ(tzid) except TimezoneException: raise HTTPError(ErrorResponse( responsecode.NOT_FOUND, (calendarserver_namespace, "timezone-available"), "Timezone not found", )) try: start = request.args.get("start", ()) if len(start) != 1: raise ValueError() start = DateTime.parseText(start[0]) except ValueError: raise HTTPError(ErrorResponse( responsecode.BAD_REQUEST, (calendarserver_namespace, "valid-start-date"), "Invalid start query parameter", )) try: end = request.args.get("end", ()) if len(end) != 1: raise ValueError() end = DateTime.parseText(end[0]) if end <= start: raise ValueError() except ValueError: raise HTTPError(ErrorResponse( responsecode.BAD_REQUEST, (calendarserver_namespace, "valid-end-date"), "Invalid end query parameter", )) # Now do the expansion (but use a cache to avoid re-calculating TZs) observances = self.cache.get((tzid, start, end), None) if observances is None: observances = tzexpand(tzdata, start, end) self.cache[(tzid, start, end)] = observances # Turn into XML result = customxml.TZData( *[customxml.Observance(customxml.Onset(onset), customxml.UTCOffset(utc_offset)) for onset, utc_offset in observances] ) return XMLResponse(responsecode.OK, result)
def test_copyPackage(self): """ Test that copying of the tz package works. """ self.patch(config, "UsePackageTimezones", True) TimezoneCache.clear() TimezoneCache.create() self.assertFalse(os.path.exists(os.path.join(config.DataRoot, "zoneinfo"))) self.assertFalse(os.path.exists(os.path.join(config.DataRoot, "zoneinfo", "America", "New_York.ics"))) pkg_tz = readTZ("America/New_York") self.patch(config, "UsePackageTimezones", False) TimezoneCache.clear() TimezoneCache.create() self.assertTrue(os.path.exists(os.path.join(config.DataRoot, "zoneinfo"))) self.assertTrue(os.path.exists(os.path.join(config.DataRoot, "zoneinfo", "America", "New_York.ics"))) copy_tz = readTZ("America/New_York") self.assertEqual(str(pkg_tz), str(copy_tz))
def test_basic(self): TimezoneCache.create() self.assertTrue(readTZ("America/New_York")) self.assertTrue(readTZ("US/Eastern"))
def test_ReadTZCached(self): self.assertTrue(readTZ("America/New_York").find("TZID:America/New_York") != -1) self.assertTrue(readTZ("America/New_York").find("TZID:America/New_York") != -1) self.assertRaises(TimezoneException, readTZ, "America/Pittsburgh") self.assertRaises(TimezoneException, readTZ, "America/Pittsburgh")