testDates.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from __future__ import print_function
  2. from datetime import datetime
  3. import unittest
  4. import pywintypes
  5. import win32com.client
  6. import win32com.test.util
  7. import win32com.server.util
  8. from win32timezone import TimeZoneInfo
  9. # A COM object so we can pass dates to and from the COM boundary.
  10. class Tester:
  11. _public_methods_ = [ 'TestDate' ]
  12. def TestDate(self, d):
  13. assert isinstance(d, datetime)
  14. return d
  15. def test_ob():
  16. return win32com.client.Dispatch(win32com.server.util.wrap(Tester()))
  17. class TestCase(win32com.test.util.TestCase):
  18. def check(self, d, expected = None):
  19. if not issubclass(pywintypes.TimeType, datetime):
  20. self.skipTest("this is testing pywintypes and datetime")
  21. got = test_ob().TestDate(d)
  22. self.assertEqual(got, expected or d)
  23. def testUTC(self):
  24. self.check(datetime(year=2000, month=12, day=25, microsecond=500000, tzinfo=TimeZoneInfo.utc()))
  25. def testLocal(self):
  26. self.check(datetime(year=2000, month=12, day=25, microsecond=500000, tzinfo=TimeZoneInfo.local()))
  27. def testMSTruncated(self):
  28. # milliseconds are kept but microseconds are lost after rounding.
  29. self.check(datetime(year=2000, month=12, day=25, microsecond=500500, tzinfo=TimeZoneInfo.utc()),
  30. datetime(year=2000, month=12, day=25, microsecond=500000, tzinfo=TimeZoneInfo.utc()))
  31. if __name__=='__main__':
  32. unittest.main()