当前位置: 首页>>代码示例>>Python>>正文


Python pytz.AmbiguousTimeError方法代码示例

本文整理汇总了Python中pytz.AmbiguousTimeError方法的典型用法代码示例。如果您正苦于以下问题:Python pytz.AmbiguousTimeError方法的具体用法?Python pytz.AmbiguousTimeError怎么用?Python pytz.AmbiguousTimeError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pytz的用法示例。


在下文中一共展示了pytz.AmbiguousTimeError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_dti_tz_localize_ambiguous_infer

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_dti_tz_localize_ambiguous_infer(self, tz):
        # November 6, 2011, fall back, repeat 2 AM hour
        # With no repeated hours, we cannot infer the transition
        dr = date_range(datetime(2011, 11, 6, 0), periods=5,
                        freq=pd.offsets.Hour())
        with pytest.raises(pytz.AmbiguousTimeError):
            dr.tz_localize(tz)

        # With repeated hours, we can infer the transition
        dr = date_range(datetime(2011, 11, 6, 0), periods=5,
                        freq=pd.offsets.Hour(), tz=tz)
        times = ['11/06/2011 00:00', '11/06/2011 01:00', '11/06/2011 01:00',
                 '11/06/2011 02:00', '11/06/2011 03:00']
        di = DatetimeIndex(times)
        localized = di.tz_localize(tz, ambiguous='infer')
        tm.assert_index_equal(dr, localized)
        tm.assert_index_equal(dr, DatetimeIndex(times, tz=tz,
                                                ambiguous='infer'))

        # When there is no dst transition, nothing special happens
        dr = date_range(datetime(2011, 6, 1, 0), periods=10,
                        freq=pd.offsets.Hour())
        localized = dr.tz_localize(tz)
        localized_infer = dr.tz_localize(tz, ambiguous='infer')
        tm.assert_index_equal(localized, localized_infer) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:27,代码来源:test_timezones.py

示例2: test_dti_tz_localize_ambiguous_times

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_dti_tz_localize_ambiguous_times(self, tz):
        # March 13, 2011, spring forward, skip from 2 AM to 3 AM
        dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3,
                        freq=pd.offsets.Hour())
        with pytest.raises(pytz.NonExistentTimeError):
            dr.tz_localize(tz)

        # after dst transition, it works
        dr = date_range(datetime(2011, 3, 13, 3, 30), periods=3,
                        freq=pd.offsets.Hour(), tz=tz)

        # November 6, 2011, fall back, repeat 2 AM hour
        dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3,
                        freq=pd.offsets.Hour())
        with pytest.raises(pytz.AmbiguousTimeError):
            dr.tz_localize(tz)

        # UTC is OK
        dr = date_range(datetime(2011, 3, 13), periods=48,
                        freq=pd.offsets.Minute(30), tz=pytz.utc) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:22,代码来源:test_timezones.py

示例3: test_dti_tz_localize

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_dti_tz_localize(self, prefix):
        tzstr = prefix + 'US/Eastern'
        dti = pd.date_range(start='1/1/2005', end='1/1/2005 0:00:30.256',
                            freq='L')
        dti2 = dti.tz_localize(tzstr)

        dti_utc = pd.date_range(start='1/1/2005 05:00',
                                end='1/1/2005 5:00:30.256', freq='L', tz='utc')

        tm.assert_numpy_array_equal(dti2.values, dti_utc.values)

        dti3 = dti2.tz_convert(prefix + 'US/Pacific')
        tm.assert_numpy_array_equal(dti3.values, dti_utc.values)

        dti = pd.date_range(start='11/6/2011 1:59', end='11/6/2011 2:00',
                            freq='L')
        with pytest.raises(pytz.AmbiguousTimeError):
            dti.tz_localize(tzstr)

        dti = pd.date_range(start='3/13/2011 1:59', end='3/13/2011 2:00',
                            freq='L')
        with pytest.raises(pytz.NonExistentTimeError):
            dti.tz_localize(tzstr) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:25,代码来源:test_timezones.py

示例4: test_round_dst_border_ambiguous

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_round_dst_border_ambiguous(self, method):
        # GH 18946 round near "fall back" DST
        ts = Timestamp('2017-10-29 00:00:00', tz='UTC').tz_convert(
            'Europe/Madrid'
        )
        #
        result = getattr(ts, method)('H', ambiguous=True)
        assert result == ts

        result = getattr(ts, method)('H', ambiguous=False)
        expected = Timestamp('2017-10-29 01:00:00', tz='UTC').tz_convert(
            'Europe/Madrid'
        )
        assert result == expected

        result = getattr(ts, method)('H', ambiguous='NaT')
        assert result is NaT

        with pytest.raises(pytz.AmbiguousTimeError):
            getattr(ts, method)('H', ambiguous='raise') 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:22,代码来源:test_unary_ops.py

示例5: test_dti_tz_localize

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_dti_tz_localize(self, prefix):
        tzstr = prefix + 'US/Eastern'
        dti = DatetimeIndex(start='1/1/2005', end='1/1/2005 0:00:30.256',
                            freq='L')
        dti2 = dti.tz_localize(tzstr)

        dti_utc = DatetimeIndex(start='1/1/2005 05:00',
                                end='1/1/2005 5:00:30.256', freq='L', tz='utc')

        tm.assert_numpy_array_equal(dti2.values, dti_utc.values)

        dti3 = dti2.tz_convert(prefix + 'US/Pacific')
        tm.assert_numpy_array_equal(dti3.values, dti_utc.values)

        dti = DatetimeIndex(start='11/6/2011 1:59', end='11/6/2011 2:00',
                            freq='L')
        with pytest.raises(pytz.AmbiguousTimeError):
            dti.tz_localize(tzstr)

        dti = DatetimeIndex(start='3/13/2011 1:59', end='3/13/2011 2:00',
                            freq='L')
        with pytest.raises(pytz.NonExistentTimeError):
            dti.tz_localize(tzstr) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:25,代码来源:test_timezones.py

示例6: test_tz_localize_dti

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_tz_localize_dti(self):
        from pandas.tseries.offsets import Hour

        dti = DatetimeIndex(start='1/1/2005', end='1/1/2005 0:00:30.256',
                            freq='L')
        dti2 = dti.tz_localize('US/Eastern')

        dti_utc = DatetimeIndex(start='1/1/2005 05:00',
                                end='1/1/2005 5:00:30.256', freq='L',
                                tz='utc')

        self.assert_(np.array_equal(dti2.values, dti_utc.values))

        dti3 = dti2.tz_convert('US/Pacific')
        self.assert_(np.array_equal(dti3.values, dti_utc.values))

        dti = DatetimeIndex(start='11/6/2011 1:59',
                            end='11/6/2011 2:00', freq='L')
        self.assertRaises(pytz.AmbiguousTimeError, dti.tz_localize,
                          'US/Eastern')

        dti = DatetimeIndex(start='3/13/2011 1:59', end='3/13/2011 2:00',
                            freq='L')
        self.assertRaises(
            pytz.NonExistentTimeError, dti.tz_localize, 'US/Eastern') 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:27,代码来源:test_timezones.py

示例7: test_with_tz_ambiguous_times

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_with_tz_ambiguous_times(self):
        tz = pytz.timezone('US/Eastern')

        rng = bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1))

        # March 13, 2011, spring forward, skip from 2 AM to 3 AM
        dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3,
                        freq=datetools.Hour())
        self.assertRaises(pytz.NonExistentTimeError, dr.tz_localize, tz)

        # after dst transition, it works
        dr = date_range(datetime(2011, 3, 13, 3, 30), periods=3,
                        freq=datetools.Hour(), tz=tz)

        # November 6, 2011, fall back, repeat 2 AM hour
        dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3,
                        freq=datetools.Hour())
        self.assertRaises(pytz.AmbiguousTimeError, dr.tz_localize, tz)

        # UTC is OK
        dr = date_range(datetime(2011, 3, 13), periods=48,
                        freq=datetools.Minute(30), tz=pytz.utc) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:24,代码来源:test_timezones.py

示例8: test_tz_localize_dti

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_tz_localize_dti(self):
        dti = DatetimeIndex(start='1/1/2005', end='1/1/2005 0:00:30.256',
                            freq='L')
        dti2 = dti.tz_localize(self.tzstr('US/Eastern'))

        dti_utc = DatetimeIndex(start='1/1/2005 05:00',
                                end='1/1/2005 5:00:30.256', freq='L', tz='utc')

        tm.assert_numpy_array_equal(dti2.values, dti_utc.values)

        dti3 = dti2.tz_convert(self.tzstr('US/Pacific'))
        tm.assert_numpy_array_equal(dti3.values, dti_utc.values)

        dti = DatetimeIndex(start='11/6/2011 1:59', end='11/6/2011 2:00',
                            freq='L')
        pytest.raises(pytz.AmbiguousTimeError, dti.tz_localize,
                      self.tzstr('US/Eastern'))

        dti = DatetimeIndex(start='3/13/2011 1:59', end='3/13/2011 2:00',
                            freq='L')
        pytest.raises(pytz.NonExistentTimeError, dti.tz_localize,
                      self.tzstr('US/Eastern')) 
开发者ID:securityclippy,项目名称:elasticintel,代码行数:24,代码来源:test_timezones.py

示例9: test_with_tz_ambiguous_times

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_with_tz_ambiguous_times(self):
        tz = self.tz('US/Eastern')

        # March 13, 2011, spring forward, skip from 2 AM to 3 AM
        dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3,
                        freq=offsets.Hour())
        pytest.raises(pytz.NonExistentTimeError, dr.tz_localize, tz)

        # after dst transition, it works
        dr = date_range(datetime(2011, 3, 13, 3, 30), periods=3,
                        freq=offsets.Hour(), tz=tz)

        # November 6, 2011, fall back, repeat 2 AM hour
        dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3,
                        freq=offsets.Hour())
        pytest.raises(pytz.AmbiguousTimeError, dr.tz_localize, tz)

        # UTC is OK
        dr = date_range(datetime(2011, 3, 13), periods=48,
                        freq=offsets.Minute(30), tz=pytz.utc) 
开发者ID:securityclippy,项目名称:elasticintel,代码行数:22,代码来源:test_timezones.py

示例10: test_ambiguous_timestamps_should_not_crash_server

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_ambiguous_timestamps_should_not_crash_server(self):
        sensor = self.get_a_sensor()
        sensor_data = self.get_resource(
            sensor.links['ch:dataHistory'].href)
        data_url = sensor_data.links.createForm.href
        data = {
            'value': 20,
            'timestamp': datetime(2015, 11, 1, 1, 0, 0).isoformat()
            }
        mime_type = 'application/hal+json'
        accept_header = mime_type + ',' + ACCEPT_TAIL
        try:
            response = self.client.post(data_url,
                                        json.dumps(data),
                                        content_type=mime_type,
                                        HTTP_ACCEPT=accept_header,
                                        HTTP_HOST='localhost')
        except AmbiguousTimeError:
            self.assertTrue(False)
        self.assertEqual(response.status_code, HTTP_STATUS_BAD_REQUEST)
        self.assertEqual(response['Content-Type'], "application/json") 
开发者ID:ResEnv,项目名称:chain-api,代码行数:23,代码来源:test_core.py

示例11: create_list

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def create_list(cls, data, request):
        response_data = []
        for item in data:
            try:
                response_data.append(cls.create_resource(item, request))
            except IntegrityError:
                return render_error(
                    400, 'Error storing object. Either required fields are '
                    'missing data or a matching object already exists',
                    request)
            except AmbiguousTimeError:
                return render_error(
                    400, 'Error storing object. Timestamp is ambiguous',
                    request)
        return cls.render_response(response_data, request,
                                   status=HTTP_STATUS_CREATED) 
开发者ID:ResEnv,项目名称:chain-api,代码行数:18,代码来源:api.py

示例12: time_zone

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def time_zone(self):
        import pytz
        from datetime import timedelta

        tz = self.timezone
        if tz is None:
            return 0

        now = datetime.now()
        try:
            offset = pytz.timezone(tz).utcoffset(now)
        except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
            # If you're really unluckly, this offset results in a time that
            # doesn't actually exist because it's within the hour that gets
            # skipped when you enter DST.
            offset = pytz.timezone(tz).utcoffset(now + timedelta(hours=1))

        return offset.seconds / 3600

    # PYBBM fields 
开发者ID:astrobin,项目名称:astrobin,代码行数:22,代码来源:models.py

示例13: test_dti_construction_ambiguous_endpoint

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_dti_construction_ambiguous_endpoint(self, tz):
        # construction with an ambiguous end-point
        # GH#11626

        # FIXME: This next block fails to raise; it was taken from an older
        # version of this test that had an indention mistake that caused it
        # to not get executed.
        # with pytest.raises(pytz.AmbiguousTimeError):
        #    date_range("2013-10-26 23:00", "2013-10-27 01:00",
        #               tz="Europe/London", freq="H")

        times = date_range("2013-10-26 23:00", "2013-10-27 01:00", freq="H",
                           tz=tz, ambiguous='infer')
        assert times[0] == Timestamp('2013-10-26 23:00', tz=tz, freq="H")

        if str(tz).startswith('dateutil'):
            if LooseVersion(dateutil.__version__) < LooseVersion('2.6.0'):
                # see GH#14621
                assert times[-1] == Timestamp('2013-10-27 01:00:00+0000',
                                              tz=tz, freq="H")
            elif LooseVersion(dateutil.__version__) > LooseVersion('2.6.0'):
                # fixed ambiguous behavior
                assert times[-1] == Timestamp('2013-10-27 01:00:00+0100',
                                              tz=tz, freq="H")
        else:
            assert times[-1] == Timestamp('2013-10-27 01:00:00+0000',
                                          tz=tz, freq="H") 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:29,代码来源:test_timezones.py

示例14: test_dt_round_tz_ambiguous

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_dt_round_tz_ambiguous(self, method):
        # GH 18946 round near "fall back" DST
        df1 = pd.DataFrame([
            pd.to_datetime('2017-10-29 02:00:00+02:00', utc=True),
            pd.to_datetime('2017-10-29 02:00:00+01:00', utc=True),
            pd.to_datetime('2017-10-29 03:00:00+01:00', utc=True)
        ],
            columns=['date'])
        df1['date'] = df1['date'].dt.tz_convert('Europe/Madrid')
        # infer
        result = getattr(df1.date.dt, method)('H', ambiguous='infer')
        expected = df1['date']
        tm.assert_series_equal(result, expected)

        # bool-array
        result = getattr(df1.date.dt, method)(
            'H', ambiguous=[True, False, False]
        )
        tm.assert_series_equal(result, expected)

        # NaT
        result = getattr(df1.date.dt, method)('H', ambiguous='NaT')
        expected = df1['date'].copy()
        expected.iloc[0:2] = pd.NaT
        tm.assert_series_equal(result, expected)

        # raise
        with pytest.raises(pytz.AmbiguousTimeError):
            getattr(df1.date.dt, method)('H', ambiguous='raise') 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:31,代码来源:test_datetime_values.py

示例15: test_tz_localize_ambiguous_bool

# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import AmbiguousTimeError [as 别名]
def test_tz_localize_ambiguous_bool(self):
        # make sure that we are correctly accepting bool values as ambiguous
        # GH#14402
        ts = Timestamp('2015-11-01 01:00:03')
        expected0 = Timestamp('2015-11-01 01:00:03-0500', tz='US/Central')
        expected1 = Timestamp('2015-11-01 01:00:03-0600', tz='US/Central')

        with pytest.raises(pytz.AmbiguousTimeError):
            ts.tz_localize('US/Central')

        result = ts.tz_localize('US/Central', ambiguous=True)
        assert result == expected0

        result = ts.tz_localize('US/Central', ambiguous=False)
        assert result == expected1 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:17,代码来源:test_timezones.py


注:本文中的pytz.AmbiguousTimeError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。