Add some helper functions for converting to/from Python datetime objects similar to what is in Classic.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@70899 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2012-03-14 20:55:49 +00:00
parent f97b2e4e6b
commit c2dbb1f5e8
2 changed files with 33 additions and 0 deletions

View File

@@ -351,6 +351,30 @@ def run():
bool operator!=(const wxTimeSpan &ts) const;
"""))
#---------------------------------------------
# Convert to/from Python date objects
module.addPyFunction('pydate2wxdate', '(date)',
doc='Convert a Python date or datetime to a wx.DateTime object',
body="""\
import datetime
assert isinstance(date, (datetime.datetime, datetime.date))
return DateTime(date) # the built-in typemap will convert it for us
""")
module.addPyFunction('wxdate2pydate', '(date)',
doc='Convert a wx.DateTime object to a Python datetime.',
body="""\
import datetime
assert isinstance(date, DateTime)
if date.IsValid():
return datetime.datetime(date.year, date.month+1, date.day,
date.hour, date.minute, date.second, date.millisecond*1000)
else:
return None
""")
#-----------------------------------------------------------------
tools.doCommonTweaks(module)

View File

@@ -127,7 +127,16 @@ class datetime_Tests(wtc.WidgetTestCase):
d2 = wx.DateTime()
d2.ParseFormat(st, fmt)
self.assertEqual(d, d2)
def test_datetimeConvertHelpers(self):
d1 = wx.DateTime(1, wx.DateTime.Mar, 2012, 8, 15, 45)
pd = wx.wxdate2pydate(d1)
d2 = wx.pydate2wxdate(pd)
self.assertTrue(isinstance(pd, datetime.datetime))
self.assertEqual(d1, d2)
#---------------------------------------------------------------------------