tests.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import sys
  2. import os
  3. from datetime import datetime
  4. import unittest
  5. import pytz
  6. import tzlocal.unix
  7. class TzLocalTests(unittest.TestCase):
  8. def setUp(self):
  9. if 'TZ' in os.environ:
  10. del os.environ['TZ']
  11. def test_env(self):
  12. tz_harare = tzlocal.unix._tz_from_env(':Africa/Harare')
  13. self.assertEqual(tz_harare.zone, 'Africa/Harare')
  14. # Some Unices allow this as well, so we must allow it:
  15. tz_harare = tzlocal.unix._tz_from_env('Africa/Harare')
  16. self.assertEqual(tz_harare.zone, 'Africa/Harare')
  17. local_path = os.path.split(__file__)[0]
  18. tz_local = tzlocal.unix._tz_from_env(':' + os.path.join(local_path, 'test_data', 'Harare'))
  19. self.assertEqual(tz_local.zone, 'local')
  20. # Make sure the local timezone is the same as the Harare one above.
  21. # We test this with a past date, so that we don't run into future changes
  22. # of the Harare timezone.
  23. dt = datetime(2012, 1, 1, 5)
  24. self.assertEqual(tz_harare.localize(dt), tz_local.localize(dt))
  25. # Non-zoneinfo timezones are not supported in the TZ environment.
  26. self.assertRaises(pytz.UnknownTimeZoneError, tzlocal.unix._tz_from_env, 'GMT+03:00')
  27. def test_timezone(self):
  28. # Most versions of Ubuntu
  29. local_path = os.path.split(__file__)[0]
  30. tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'timezone'))
  31. self.assertEqual(tz.zone, 'Africa/Harare')
  32. def test_zone_setting(self):
  33. # A ZONE setting in /etc/sysconfig/clock, f ex CentOS
  34. local_path = os.path.split(__file__)[0]
  35. tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'zone_setting'))
  36. self.assertEqual(tz.zone, 'Africa/Harare')
  37. def test_timezone_setting(self):
  38. # A ZONE setting in /etc/conf.d/clock, f ex Gentoo
  39. local_path = os.path.split(__file__)[0]
  40. tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'timezone_setting'))
  41. self.assertEqual(tz.zone, 'Africa/Harare')
  42. def test_symlink_localtime(self):
  43. # A ZONE setting in the target path of a symbolic linked localtime, f ex systemd distributions
  44. local_path = os.path.split(__file__)[0]
  45. tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'symlink_localtime'))
  46. self.assertEqual(tz.zone, 'Africa/Harare')
  47. def test_only_localtime(self):
  48. local_path = os.path.split(__file__)[0]
  49. tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'localtime'))
  50. self.assertEqual(tz.zone, 'local')
  51. dt = datetime(2012, 1, 1, 5)
  52. self.assertEqual(pytz.timezone('Africa/Harare').localize(dt), tz.localize(dt))
  53. if sys.platform == 'win32':
  54. import tzlocal.win32
  55. class TzWin32Tests(unittest.TestCase):
  56. def test_win32(self):
  57. tzlocal.win32.get_localzone()
  58. if __name__ == '__main__':
  59. unittest.main()