In the abstraction layer of my applications I prefer to use the new
(for Python 2.3) datetime module, but in the presentation layer I
am wrapping the ws.calendar.CalendarCtrl class in a "presenter",
which means that I must convert between datetime.datetime objects
and wx.DateTime objects. The code below seems to accurately perform
the round-trip conversion both ways, but:
1. Is there any reason to believe that these conversions might not
work for some combination of dates and times?
2. In wx2py, is there an easier way to get the 'ymd' tuple than
calling all six GetXxxx() methods?
3. Is there an easier way of doing these conversions, period?
4. Am I correct that the wx.calendar.CalendarCtrl.GetDate() returns
a wx.DateTime object, and that SetDate() requires a wx.DateTime
object as its input argument?
5. Would anyone else find it helpful if the wxPython wrapper of the
wxWidgets (C++) CalendarCtrl could produce and/or handle Python
datetime.datetime objects? Is this feasible?
Donnal Walter
Arkansas Children's Hospital
···
=======
import wx
import datetime
def py2wx(dt):
tt = dt.timetuple()
dmy = (tt[2], tt[1], tt[0], tt[3], tt[4], tt[5])
return wx.DateTimeFromDMY(*dmy)
def wx2py(dt):
year = dt.GetYear()
mo = dt.GetMonth()
day = dt.GetDay()
hh = dt.GetHour()
mm = dt.GetMinute()
ss = dt.GetSecond()
ymd = (year, mo, day, hh, mm, ss)
return datetime.datetime(*ymd)
if __name__ == '__main__':
pydt_in = datetime.datetime(2004, 9, 10, 7, 27, 05)
wxdt = py2wx(pydt_in)
pydt_out = wx2py(wxdt)
print pydt_in.isoformat()
print pydt_out.isoformat()
assert(pydt_in == pydt_out)
wxdt_in = wx.DateTimeFromDMY(10, 9, 2004, 7, 35, 12)
pydt = wx2py(wxdt_in)
wxdt_out = py2wx(pydt)
assert(wxdt_in == wxdt_out)