test_utcoffset.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2021, Brandon Nielsen
  3. # All rights reserved.
  4. #
  5. # This software may be modified and distributed under the terms
  6. # of the BSD license. See the LICENSE file for details.
  7. import datetime
  8. import pickle
  9. import unittest
  10. from aniso8601.utcoffset import UTCOffset
  11. class TestUTCOffset(unittest.TestCase):
  12. def test_pickle(self):
  13. # Make sure timezone objects are pickleable
  14. testutcoffset = UTCOffset(name="UTC", minutes=0)
  15. utcoffsetpickle = pickle.dumps(testutcoffset)
  16. resultutcoffset = pickle.loads(utcoffsetpickle)
  17. self.assertEqual(resultutcoffset._name, testutcoffset._name)
  18. self.assertEqual(resultutcoffset._utcdelta, testutcoffset._utcdelta)
  19. def test_repr(self):
  20. self.assertEqual(str(UTCOffset(minutes=0)), "+0:00:00 UTC")
  21. self.assertEqual(str(UTCOffset(minutes=60)), "+1:00:00 UTC")
  22. self.assertEqual(str(UTCOffset(minutes=-60)), "-1:00:00 UTC")
  23. self.assertEqual(str(UTCOffset(minutes=12)), "+0:12:00 UTC")
  24. self.assertEqual(str(UTCOffset(minutes=-12)), "-0:12:00 UTC")
  25. self.assertEqual(str(UTCOffset(minutes=83)), "+1:23:00 UTC")
  26. self.assertEqual(str(UTCOffset(minutes=-83)), "-1:23:00 UTC")
  27. self.assertEqual(str(UTCOffset(minutes=1440)), "+1 day, 0:00:00 UTC")
  28. self.assertEqual(str(UTCOffset(minutes=-1440)), "-1 day, 0:00:00 UTC")
  29. self.assertEqual(str(UTCOffset(minutes=2967)), "+2 days, 1:27:00 UTC")
  30. self.assertEqual(str(UTCOffset(minutes=-2967)), "-2 days, 1:27:00 UTC")
  31. def test_dst(self):
  32. tzinfoobject = UTCOffset(minutes=240)
  33. # This would raise ISOFormatError or a TypeError if dst info is invalid
  34. result = datetime.datetime.now(tzinfoobject)
  35. # Hacky way to make sure the tzinfo is what we'd expect
  36. self.assertEqual(result.tzinfo.utcoffset(None), datetime.timedelta(hours=4))