Python pytz.exceptions() Examples

The following are 4 code examples of pytz.exceptions(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pytz , or try the search function .
Example #1
Source File: ewsdatetime.py    From exchangelib with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _localize_or_normalize(self, func, dt, is_dst=False):
        """localize() and normalize() have common code paths

        Args:
          func:
          dt:
          is_dst:  (Default value = 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 = getattr(super(EWSTimeZone, self), func)(dt, is_dst=is_dst)
            except pytz.exceptions.AmbiguousTimeError as exc:
                raise AmbiguousTimeError(str(dt)) from exc
            except pytz.exceptions.NonExistentTimeError as exc:
                raise NonExistentTimeError(str(dt)) from exc
        else:
            res = getattr(super(EWSTimeZone, self), func)(dt)
        if not isinstance(res.tzinfo, EWSTimeZone):
            return res.replace(tzinfo=self.from_pytz(res.tzinfo))
        return res 
Example #2
Source File: test_timezones.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_nonexistent_raise_coerce(self):
        # See issue 13057
        from pytz.exceptions import NonExistentTimeError
        times = ['2015-03-08 01:00', '2015-03-08 02:00', '2015-03-08 03:00']
        index = DatetimeIndex(times)
        tz = 'US/Eastern'
        pytest.raises(NonExistentTimeError,
                      index.tz_localize, tz=tz)
        pytest.raises(NonExistentTimeError,
                      index.tz_localize, tz=tz, errors='raise')
        result = index.tz_localize(tz=tz, errors='coerce')
        test_times = ['2015-03-08 01:00-05:00', 'NaT',
                      '2015-03-08 03:00-04:00']
        expected = DatetimeIndex(test_times)\
            .tz_localize('UTC').tz_convert('US/Eastern')
        tm.assert_index_equal(result, expected)

    # test utility methods 
Example #3
Source File: ewsdatetime.py    From exchangelib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def localzone(cls):
        try:
            tz = tzlocal.get_localzone()
        except pytz.exceptions.UnknownTimeZoneError:
            raise UnknownTimeZone("Failed to guess local timezone")
        return cls.from_pytz(tz) 
Example #4
Source File: ewsdatetime.py    From exchangelib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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)