test_utils.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from datetime import timedelta, datetime
  4. from dateutil import tz
  5. from dateutil import utils
  6. from dateutil.tz import UTC
  7. from dateutil.utils import within_delta
  8. from freezegun import freeze_time
  9. NYC = tz.gettz("America/New_York")
  10. @freeze_time(datetime(2014, 12, 15, 1, 21, 33, 4003))
  11. def test_utils_today():
  12. assert utils.today() == datetime(2014, 12, 15, 0, 0, 0)
  13. @freeze_time(datetime(2014, 12, 15, 12), tz_offset=5)
  14. def test_utils_today_tz_info():
  15. assert utils.today(NYC) == datetime(2014, 12, 15, 0, 0, 0, tzinfo=NYC)
  16. @freeze_time(datetime(2014, 12, 15, 23), tz_offset=5)
  17. def test_utils_today_tz_info_different_day():
  18. assert utils.today(UTC) == datetime(2014, 12, 16, 0, 0, 0, tzinfo=UTC)
  19. def test_utils_default_tz_info_naive():
  20. dt = datetime(2014, 9, 14, 9, 30)
  21. assert utils.default_tzinfo(dt, NYC).tzinfo is NYC
  22. def test_utils_default_tz_info_aware():
  23. dt = datetime(2014, 9, 14, 9, 30, tzinfo=UTC)
  24. assert utils.default_tzinfo(dt, NYC).tzinfo is UTC
  25. def test_utils_within_delta():
  26. d1 = datetime(2016, 1, 1, 12, 14, 1, 9)
  27. d2 = d1.replace(microsecond=15)
  28. assert within_delta(d1, d2, timedelta(seconds=1))
  29. assert not within_delta(d1, d2, timedelta(microseconds=1))
  30. def test_utils_within_delta_with_negative_delta():
  31. d1 = datetime(2016, 1, 1)
  32. d2 = datetime(2015, 12, 31)
  33. assert within_delta(d2, d1, timedelta(days=-1))