2
0

test_tzinfo.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import copy
  2. import datetime
  3. import os
  4. import pickle
  5. import time
  6. import unittest
  7. import warnings
  8. from django.test.utils import IgnorePendingDeprecationWarningsMixin
  9. # Swallow the import-time warning to test the deprecated implementation.
  10. with warnings.catch_warnings():
  11. warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
  12. from django.utils.tzinfo import FixedOffset, LocalTimezone
  13. class TzinfoTests(IgnorePendingDeprecationWarningsMixin, unittest.TestCase):
  14. @classmethod
  15. def setUpClass(cls):
  16. cls.old_TZ = os.environ.get('TZ')
  17. os.environ['TZ'] = 'US/Eastern'
  18. try:
  19. # Check if a timezone has been set
  20. time.tzset()
  21. cls.tz_tests = True
  22. except AttributeError:
  23. # No timezone available. Don't run the tests that require a TZ
  24. cls.tz_tests = False
  25. @classmethod
  26. def tearDownClass(cls):
  27. if cls.old_TZ is None:
  28. del os.environ['TZ']
  29. else:
  30. os.environ['TZ'] = cls.old_TZ
  31. # Cleanup - force re-evaluation of TZ environment variable.
  32. if cls.tz_tests:
  33. time.tzset()
  34. def test_fixedoffset(self):
  35. self.assertEqual(repr(FixedOffset(0)), '+0000')
  36. self.assertEqual(repr(FixedOffset(60)), '+0100')
  37. self.assertEqual(repr(FixedOffset(-60)), '-0100')
  38. self.assertEqual(repr(FixedOffset(280)), '+0440')
  39. self.assertEqual(repr(FixedOffset(-280)), '-0440')
  40. self.assertEqual(repr(FixedOffset(-78.4)), '-0118')
  41. self.assertEqual(repr(FixedOffset(78.4)), '+0118')
  42. self.assertEqual(repr(FixedOffset(-5.5*60)), '-0530')
  43. self.assertEqual(repr(FixedOffset(5.5*60)), '+0530')
  44. self.assertEqual(repr(FixedOffset(-.5*60)), '-0030')
  45. self.assertEqual(repr(FixedOffset(.5*60)), '+0030')
  46. def test_16899(self):
  47. if not self.tz_tests:
  48. return
  49. ts = 1289106000
  50. # Midnight at the end of DST in US/Eastern: 2010-11-07T05:00:00Z
  51. dt = datetime.datetime.utcfromtimestamp(ts)
  52. # US/Eastern -- we force its representation to "EST"
  53. tz = LocalTimezone(dt + datetime.timedelta(days=1))
  54. self.assertEqual(
  55. repr(datetime.datetime.fromtimestamp(ts - 3600, tz)),
  56. 'datetime.datetime(2010, 11, 7, 0, 0, tzinfo=EST)')
  57. self.assertEqual(
  58. repr(datetime.datetime.fromtimestamp(ts, tz)),
  59. 'datetime.datetime(2010, 11, 7, 1, 0, tzinfo=EST)')
  60. self.assertEqual(
  61. repr(datetime.datetime.fromtimestamp(ts + 3600, tz)),
  62. 'datetime.datetime(2010, 11, 7, 1, 0, tzinfo=EST)')
  63. def test_copy(self):
  64. now = datetime.datetime.now()
  65. self.assertIsInstance(copy.copy(FixedOffset(90)), FixedOffset)
  66. self.assertIsInstance(copy.copy(LocalTimezone(now)), LocalTimezone)
  67. def test_deepcopy(self):
  68. now = datetime.datetime.now()
  69. self.assertIsInstance(copy.deepcopy(FixedOffset(90)), FixedOffset)
  70. self.assertIsInstance(copy.deepcopy(LocalTimezone(now)), LocalTimezone)
  71. def test_pickling_unpickling(self):
  72. now = datetime.datetime.now()
  73. self.assertIsInstance(pickle.loads(pickle.dumps(FixedOffset(90))), FixedOffset)
  74. self.assertIsInstance(pickle.loads(pickle.dumps(LocalTimezone(now))), LocalTimezone)