def test_infer_tz_compat(infer_setup): eastern, _, start, end, start_naive, end_naive = infer_setup assert (timezones.infer_tzinfo(start, end) is conversion.localize_pydatetime(start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(start, None) is conversion.localize_pydatetime(start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(None, end) is conversion.localize_pydatetime(end_naive, eastern).tzinfo)
def test_infer_tz_mismatch(infer_setup, ordered): eastern, _, _, _, start_naive, end_naive = infer_setup msg = "Inputs must both have the same timezone" utc = pytz.utc start = utc.localize(start_naive) end = conversion.localize_pydatetime(end_naive, eastern) args = (start, end) if ordered else (end, start) with pytest.raises(AssertionError, match=msg): timezones.infer_tzinfo(*args)
def test_infer_tz(eastern, localize): utc = pytz.utc start_naive = datetime(2001, 1, 1) end_naive = datetime(2009, 1, 1) start = localize(eastern, start_naive) end = localize(eastern, end_naive) assert (timezones.infer_tzinfo(start, end) is tslib._localize_pydatetime( start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(start, None) is tslib._localize_pydatetime( start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(None, end) is tslib._localize_pydatetime( end_naive, eastern).tzinfo) start = utc.localize(start_naive) end = utc.localize(end_naive) assert timezones.infer_tzinfo(start, end) is utc end = tslib._localize_pydatetime(end_naive, eastern) with pytest.raises(Exception): timezones.infer_tzinfo(start, end) with pytest.raises(Exception): timezones.infer_tzinfo(end, start)
def test_infer_tz(eastern, localize): utc = pytz.utc start_naive = datetime(2001, 1, 1) end_naive = datetime(2009, 1, 1) start = localize(eastern, start_naive) end = localize(eastern, end_naive) assert (timezones.infer_tzinfo(start, end) is tslib._localize_pydatetime(start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(start, None) is tslib._localize_pydatetime(start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(None, end) is tslib._localize_pydatetime(end_naive, eastern).tzinfo) start = utc.localize(start_naive) end = utc.localize(end_naive) assert timezones.infer_tzinfo(start, end) is utc end = tslib._localize_pydatetime(end_naive, eastern) with pytest.raises(Exception): timezones.infer_tzinfo(start, end) with pytest.raises(Exception): timezones.infer_tzinfo(end, start)
def test_infer_tz_utc_localize(infer_setup): _, _, start, end, start_naive, end_naive = infer_setup utc = pytz.utc start = utc.localize(start_naive) end = utc.localize(end_naive) assert timezones.infer_tzinfo(start, end) is utc
def test_infer_tz(self): eastern = self.tz('US/Eastern') utc = pytz.utc _start = datetime(2001, 1, 1) _end = datetime(2009, 1, 1) start = self.localize(eastern, _start) end = self.localize(eastern, _end) assert (timezones.infer_tzinfo(start, end) is self.localize(eastern, _start).tzinfo) assert (timezones.infer_tzinfo(start, None) is self.localize(eastern, _start).tzinfo) assert (timezones.infer_tzinfo(None, end) is self.localize(eastern, _end).tzinfo) start = utc.localize(_start) end = utc.localize(_end) assert (timezones.infer_tzinfo(start, end) is utc) end = self.localize(eastern, _end) pytest.raises(Exception, timezones.infer_tzinfo, start, end) pytest.raises(Exception, timezones.infer_tzinfo, end, start)
def test_infer_tz(self): eastern = self.tz('US/Eastern') utc = pytz.utc _start = datetime(2001, 1, 1) _end = datetime(2009, 1, 1) start = self.localize(eastern, _start) end = self.localize(eastern, _end) assert (timezones.infer_tzinfo(start, end) is self.localize( eastern, _start).tzinfo) assert (timezones.infer_tzinfo(start, None) is self.localize( eastern, _start).tzinfo) assert (timezones.infer_tzinfo(None, end) is self.localize( eastern, _end).tzinfo) start = utc.localize(_start) end = utc.localize(_end) assert (timezones.infer_tzinfo(start, end) is utc) end = self.localize(eastern, _end) pytest.raises(Exception, timezones.infer_tzinfo, start, end) pytest.raises(Exception, timezones.infer_tzinfo, end, start)
def _infer_tz_from_endpoints(start, end, tz): """ If a timezone is not explicitly given via `tz`, see if one can be inferred from the `start` and `end` endpoints. If more than one of these inputs provides a timezone, require that they all agree. Parameters ---------- start : Timestamp end : Timestamp tz : tzinfo or None Returns ------- tz : tzinfo or None inferred_tz : tzinfo or None Raises ------ TypeError : if start and end timezones do not agree """ try: inferred_tz = timezones.infer_tzinfo(start, end) except Exception: raise TypeError('Start and end cannot both be tz-aware with ' 'different timezones') inferred_tz = timezones.maybe_get_tz(inferred_tz) tz = timezones.maybe_get_tz(tz) if tz is not None and inferred_tz is not None: if not timezones.tz_compare(inferred_tz, tz): raise AssertionError("Inferred time zone not equal to passed " "time zone") elif inferred_tz is not None: tz = inferred_tz return tz, inferred_tz
def _infer_tz_from_endpoints(start, end, tz): # pragma: no cover """ If a timezone is not explicitly given via `tz`, see if one can be inferred from the `start` and `end` endpoints. If more than one of these inputs provides a timezone, require that they all agree. Parameters ---------- start : Timestamp end : Timestamp tz : tzinfo or None Returns ------- tz : tzinfo or None Raises ------ TypeError : if start and end timezones do not agree """ try: inferred_tz = timezones.infer_tzinfo(start, end) except AssertionError: # infer_tzinfo raises AssertionError if passed mismatched timezones raise TypeError( "Start and end cannot both be tz-aware with different timezones") inferred_tz = timezones.maybe_get_tz(inferred_tz) tz = timezones.maybe_get_tz(tz) if tz is not None and inferred_tz is not None: if not timezones.tz_compare(inferred_tz, tz): raise AssertionError( "Inferred time zone not equal to passed time zone") elif inferred_tz is not None: tz = inferred_tz return tz