test_timezones.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. import dateutil.tz
  4. import pytest
  5. import pytz
  6. from pandas._libs.tslibs import conversion, timezones
  7. from pandas import Timestamp
  8. @pytest.mark.parametrize("tz_name", list(pytz.common_timezones))
  9. def test_cache_keys_are_distinct_for_pytz_vs_dateutil(tz_name):
  10. if tz_name == "UTC":
  11. pytest.skip("UTC: special case in dateutil")
  12. tz_p = timezones.maybe_get_tz(tz_name)
  13. tz_d = timezones.maybe_get_tz("dateutil/" + tz_name)
  14. if tz_d is None:
  15. pytest.skip(tz_name + ": dateutil does not know about this one")
  16. assert timezones._p_tz_cache_key(tz_p) != timezones._p_tz_cache_key(tz_d)
  17. def test_tzlocal_repr():
  18. # see gh-13583
  19. ts = Timestamp("2011-01-01", tz=dateutil.tz.tzlocal())
  20. assert ts.tz == dateutil.tz.tzlocal()
  21. assert "tz='tzlocal()')" in repr(ts)
  22. def test_tzlocal_maybe_get_tz():
  23. # see gh-13583
  24. tz = timezones.maybe_get_tz('tzlocal()')
  25. assert tz == dateutil.tz.tzlocal()
  26. def test_tzlocal_offset():
  27. # see gh-13583
  28. #
  29. # Get offset using normal datetime for test.
  30. ts = Timestamp("2011-01-01", tz=dateutil.tz.tzlocal())
  31. offset = dateutil.tz.tzlocal().utcoffset(datetime(2011, 1, 1))
  32. offset = offset.total_seconds() * 1000000000
  33. assert ts.value + offset == Timestamp("2011-01-01").value
  34. @pytest.fixture(params=[
  35. (pytz.timezone("US/Eastern"), lambda tz, x: tz.localize(x)),
  36. (dateutil.tz.gettz("US/Eastern"), lambda tz, x: x.replace(tzinfo=tz))
  37. ])
  38. def infer_setup(request):
  39. eastern, localize = request.param
  40. start_naive = datetime(2001, 1, 1)
  41. end_naive = datetime(2009, 1, 1)
  42. start = localize(eastern, start_naive)
  43. end = localize(eastern, end_naive)
  44. return eastern, localize, start, end, start_naive, end_naive
  45. def test_infer_tz_compat(infer_setup):
  46. eastern, _, start, end, start_naive, end_naive = infer_setup
  47. assert (timezones.infer_tzinfo(start, end) is
  48. conversion.localize_pydatetime(start_naive, eastern).tzinfo)
  49. assert (timezones.infer_tzinfo(start, None) is
  50. conversion.localize_pydatetime(start_naive, eastern).tzinfo)
  51. assert (timezones.infer_tzinfo(None, end) is
  52. conversion.localize_pydatetime(end_naive, eastern).tzinfo)
  53. def test_infer_tz_utc_localize(infer_setup):
  54. _, _, start, end, start_naive, end_naive = infer_setup
  55. utc = pytz.utc
  56. start = utc.localize(start_naive)
  57. end = utc.localize(end_naive)
  58. assert timezones.infer_tzinfo(start, end) is utc
  59. @pytest.mark.parametrize("ordered", [True, False])
  60. def test_infer_tz_mismatch(infer_setup, ordered):
  61. eastern, _, _, _, start_naive, end_naive = infer_setup
  62. msg = "Inputs must both have the same timezone"
  63. utc = pytz.utc
  64. start = utc.localize(start_naive)
  65. end = conversion.localize_pydatetime(end_naive, eastern)
  66. args = (start, end) if ordered else (end, start)
  67. with pytest.raises(AssertionError, match=msg):
  68. timezones.infer_tzinfo(*args)