我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用pytz.exceptions()。
def localzone(cls): try: tz = tzlocal.get_localzone() except pytz.exceptions.UnknownTimeZoneError: raise UnknownTimeZone("Failed to guess local timezone") return cls.from_pytz(tz)
def timezone(cls, location): # Like pytz.timezone() but returning EWSTimeZone instances try: tz = pytz.timezone(location) except pytz.exceptions.UnknownTimeZoneError: raise UnknownTimeZone("Timezone '%s' is unknown by pytz" % location) return cls.from_pytz(tz)
def normalize(self, dt, is_dst=False): # super() returns a dt.tzinfo of class pytz.tzinfo.FooBar. We need to return type EWSTimeZone if is_dst is not False: # Not all pytz timezones support 'is_dst' argument. Only pass it on if it's set explicitly. try: res = super(EWSTimeZone, self).normalize(dt, is_dst=is_dst) except pytz.exceptions.AmbiguousTimeError: raise AmbiguousTimeError(str(dt)) except pytz.exceptions.NonExistentTimeError: raise NonExistentTimeError(str(dt)) else: res = super(EWSTimeZone, self).normalize(dt) if not isinstance(res.tzinfo, EWSTimeZone): return res.replace(tzinfo=self.from_pytz(res.tzinfo)) return res
def localize(self, dt, is_dst=False): # super() returns a dt.tzinfo of class pytz.tzinfo.FooBar. We need to return type EWSTimeZone if is_dst is not False: # Not all pytz timezones support 'is_dst' argument. Only pass it on if it's set explicitly. try: res = super(EWSTimeZone, self).localize(dt, is_dst=is_dst) except pytz.exceptions.AmbiguousTimeError: raise AmbiguousTimeError(str(dt)) except pytz.exceptions.NonExistentTimeError: raise NonExistentTimeError(str(dt)) else: res = super(EWSTimeZone, self).localize(dt) if not isinstance(res.tzinfo, EWSTimeZone): return res.replace(tzinfo=self.from_pytz(res.tzinfo)) return res