test_tzinfo.py 3.4 KB

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