def test_create_from_timezone_name(self): raw = dict(foo="bar") tz = from_timezone_name(self.timezone_name, raw) self.assertEqual(tz.raw['foo'], 'bar') self.assertIsInstance(tz.pytz_timezone, pytz.tzinfo.BaseTzInfo) self.assertIsInstance(tz.pytz_timezone, datetime.tzinfo)
def test_picklable(self): raw = dict(foo="bar") tz = from_timezone_name(self.timezone_name, raw) # https://docs.python.org/2/library/pickle.html#data-stream-format for protocol in (0, 1, 2, -1): pickled = pickle.dumps(tz, protocol=protocol) tz_unp = pickle.loads(pickled) self.assertEqual(tz, tz_unp)
def test_with_unpicklable_raw(self): some_class = type('some_class', (object,), {}) raw_unpicklable = dict(missing=some_class()) del some_class tz_unpicklable = from_timezone_name(self.timezone_name, raw_unpicklable) for protocol in (0, 1, 2, -1): with self.assertRaises((AttributeError, pickle.PicklingError)): pickle.dumps(tz_unpicklable, protocol=protocol)
def _parse_json_timezone(self, response): self._raise_for_error(response) timezone_id = response.get("timezoneId") if timezone_id is None: # Sometimes (e.g. for Antarctica) GeoNames doesn't return # a `timezoneId` value, but it returns GMT offsets. # Apparently GeoNames always returns these offsets -- for # every single point on the globe. raw_offset = response["rawOffset"] return from_fixed_gmt_offset(raw_offset, raw=response) else: return from_timezone_name(timezone_id, raw=response)
def _parse_json_timezone(self, response): status = response.get('status') if status != 'OK': self._check_status(status) timezone_id = response.get("timeZoneId") if timezone_id is None: # Google returns `status: ZERO_RESULTS` for uncovered # points (e.g. for Antarctica), so there's nothing # meaningful to be returned as the `raw` response, # hence we return `None`. return None return from_timezone_name(timezone_id, raw=response)
def test_string(self): raw = dict(foo="bar") tz = from_timezone_name(self.timezone_name, raw) self.assertEqual(str(tz), self.timezone_name)
def _parse_json_timezone(self, response): return from_timezone_name(response["timeZoneId"], raw=response)