testDates.py 1.4 KB

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