datetime conversions

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)

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?

It's pretty much automatic so I think it will work.

2. In wx2py, is there an easier way to get the 'ymd' tuple than
calling all six GetXxxx() methods?

:slight_smile: you could try:
ymd = map(int, dt.FormatISODate().split('-') + dt.FormatISOTime().split(':'))

3. Is there an easier way of doing these conversions, period?

I think this shouldn't be necessary, wxpython objects should work with python datetime objects.

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?

yes,
print some_calendar.GetDate().__repr__
proves it :wink:

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?

Maybe Robin could copy CalendarCtrl from wx.calendar to lets say wx.gizmos and make it use python datetime objects. :slight_smile:

···

On Fri, 10 Sep 2004 06:58:17 -0700 (PDT), Donnal Walter <donnalcwalter@yahoo.com> wrote:

--
Peter Damoc
Hacker Wannabe

Pardon me for replying to my own posting, but...

Upon further reflection, I realized: (1) cal.CalendarCtrl does not
require the time component, and (2) the month integer is 1-12 for
Python datetime.date and 0-11 for wx.DateTime. The following
subclass does what I want. Of course any suggestions for further
simplifying the code will be much appreciated.

Donnal Walter
Arkansas Children's Hospital

···

=======
import wx
import wx.calendar as cal
import datetime

class MyCalendarCtrl(cal.CalendarCtrl):
    
    def PySetDate(self, date):
        """takes datetime.datetime or datetime.date object"""
        assert isinstance(date, (datetime.datetime,
                                 datetime.date))
        tt = date.timetuple()
        dmy = (tt[2], tt[1]-1, tt[0])
        self.SetDate(wx.DateTimeFromDMY(*dmy))

    def PyGetDate(self):
        """returns datetime.date object"""
        dt = self.GetDate()
        year = dt.GetYear()
        mo = dt.GetMonth() + 1
        day = dt.GetDay()
        ymd = (year, mo, day)
        return datetime.date(*ymd)

Yes, this works:

import wx
import wx.calendar as cal
import datetime

class MyCalendarCtrl(cal.CalendarCtrl):
    
    def PySetDate(self, date):
        """takes datetime.datetime or datetime.date object"""
        assert isinstance(date, (datetime.datetime, datetime.date))
        tt = date.timetuple()
        dmy = (tt[2], tt[1]-1, tt[0])
        self.SetDate(wx.DateTimeFromDMY(*dmy))

    def PyGetDate(self):
        """returns datetime.date object"""
        dt = self.GetDate()
        # thank you Peter Damoc
        ymd = map(int, dt.FormatISODate().split('-'))
        return datetime.date(*ymd)

Thanks,
Donnal Walter
Arkansas Children's Hospital

···

--- Peter Damoc <pdamoc@gmx.net> wrote:

:slight_smile: you could try:
ymd = map(int, dt.FormatISODate().split('-')
+ dt.FormatISOTime().split(':'))

Peter Damoc wrote:

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?

Maybe Robin could copy CalendarCtrl from wx.calendar to lets say wx.gizmos and make it use python datetime objects. :slight_smile:

It's all generic code, so it could easily be ported to pure-Python code with all wxDateTimes replaced with datetime.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Donnal Walter wrote:

···

--- Peter Damoc <pdamoc@gmx.net> wrote:

:slight_smile: you could try:
ymd = map(int, dt.FormatISODate().split('-') + dt.FormatISOTime().split(':'))

Yes, this works:

import wx
import wx.calendar as cal
import datetime

class MyCalendarCtrl(cal.CalendarCtrl):
        def PySetDate(self, date):
        """takes datetime.datetime or datetime.date object"""
        assert isinstance(date, (datetime.datetime, datetime.date))
        tt = date.timetuple()
        dmy = (tt[2], tt[1]-1, tt[0])
        self.SetDate(wx.DateTimeFromDMY(*dmy))

    def PyGetDate(self):
        """returns datetime.date object"""
        dt = self.GetDate()
        # thank you Peter Damoc
        ymd = map(int, dt.FormatISODate().split('-'))
        return datetime.date(*ymd)

I can easily add these methods to the existing CalendarCtrl class. Are there any other suggestions?

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

I can do everything I need to do with those two methods alone, but
to be consistent, I suppose the same two methods should be added to
CalendarEvent, and the Set and GetDateLimit methods should be added
as well. Here is how I would suggest coding all of these.

···

--- Robin Dunn <robin@alldunn.com> wrote:

I can easily add these methods to the existing CalendarCtrl
class. Are there any other suggestions?

=======
import wx
import wx.calendar as cal
import datetime

def py2wx(date):
    assert isinstance(date, (datetime.datetime, datetime.date))
    tt = date.timetuple()
    dmy = (tt[2], tt[1]-1, tt[0])
    return wx.DateTimeFromDMY(*dmy)

def wx2py(date):
    assert isinstance(date, wx.DateTime)
    ymd = map(int, date.FormatISODate().split('-'))
    return datetime.date(*ymd)

class MyCalendarCtrl(cal.CalendarCtrl):
    
    def PySetDate(self, date):
        """takes datetime.datetime or datetime.date object"""
        self.SetDate(py2wx(date))

    def PyGetDate(self):
        """returns datetime.date object"""
        return wx2py(self.GetDate())

    def PySetLowerDateLimit(self, date):
        """takes datetime.datetime or datetime.date object"""
        self.SetLowerDateLimit(py2wx(date))

    def PySetUpperDateLimit(self, date):
        """takes datetime.datetime or datetime.date object"""
        self.SetUpperDateLimit(py2wx(date))

    def PySetDateRange(self, lowerdate, upperdate):
        """takes datetime.datetime or datetime.date objects"""
        self.PySetLowerDateLimit(lowerdate)
        self.PySetUpperDateLimit(upperdate)

    def PyGetLowerDateLimit(self):
        """returns datetime.date object"""
        return wx2py(self.GetLowerDateLimit())

    def PyGetUpperDateLimit(self):
        """returns datetime.date object"""
        return wx2py(self.GetUpperDateLimit())

Incidentally, I found these methods by looking at the source code.
They do not appear to be documented in the chm (unless I am missing
somthing). Although I have tested the seven methods defined above,
I did not test PyGetDate and PySetDate with CalendarEvent, but I am
assuming that they should work the same there.

Thank you, Robin.

Donnal Walter
Arkansas Children's Hospital

I suggested the move to gizmos because I thought that a simple port will break peoples code :slight_smile: but come to think of it... a conditional in the constructor might take care of the "is it wx.DateTime or datetime.datetime" issue...

···

On Fri, 10 Sep 2004 10:24:17 -0700, Robin Dunn <robin@alldunn.com> wrote:

Peter Damoc wrote:

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?

  Maybe Robin could copy CalendarCtrl from wx.calendar to lets say wx.gizmos and make it use python datetime objects. :slight_smile:

It's all generic code, so it could easily be ported to pure-Python code with all wxDateTimes replaced with datetime.

--
Peter Damoc
Hacker Wannabe

btw does this thread refer to mxDateTime, or does python have another
DateTime?

···

On Saturday 11 September 2004 12:55 pm, you wrote:

On Fri, 10 Sep 2004 10:24:17 -0700, Robin Dunn <robin@alldunn.com> wrote:
> Peter Damoc wrote:
>>> 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?
>>
>> Maybe Robin could copy CalendarCtrl from wx.calendar to lets say
>> wx.gizmos and make it use python datetime objects. :slight_smile:
>
> It's all generic code, so it could easily be ported to pure-Python code
> with all wxDateTimes replaced with datetime.

I suggested the move to gizmos because I thought that a simple port will
break peoples code :slight_smile: but come to think of it... a conditional in the
constructor might take care of the "is it wx.DateTime or
datetime.datetime" issue...

--
regards
kg

http://www.onlineindianhotels.net - fastest hotel search website in the world
http://www.ootygolfclub.org

Easier than that, Get/SetDate still return/take wx.DateTime,
and PyGet/PySetDate return/take datetime.datetime from Python.

Regards,
Donnal Walter
Arkansas Children's Hospital

···

--- Peter Damoc <pdamoc@gmx.net> wrote:

Robin Dunn <robin@alldunn.com> wrote:
> Peter Damoc wrote:
> > Donnal Walter wrote:
> > 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?
> Maybe Robin could copy CalendarCtrl from wx.calendar to lets
> say wx.gizmos and make it use python datetime objects. :slight_smile:
>
> It's all generic code, so it could easily be ported to
> pure-Python code with all wxDateTimes replaced with datetime.

I suggested the move to gizmos because I thought that a simple
port will break peoples code :slight_smile: but come to think of it... a
conditional in the constructor might take care of the "is it
wx.DateTime or datetime.datetime" issue...

Kenneth Gonsalves wrote:

···

On Saturday 11 September 2004 12:55 pm, you wrote:

On Fri, 10 Sep 2004 10:24:17 -0700, Robin Dunn <robin@alldunn.com> wrote:
   

Peter Damoc wrote:
     

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?
         

Maybe Robin could copy CalendarCtrl from wx.calendar to lets say
wx.gizmos and make it use python datetime objects. :slight_smile:
       

It's all generic code, so it could easily be ported to pure-Python code
with all wxDateTimes replaced with datetime.
     

I suggested the move to gizmos because I thought that a simple port will
break peoples code :slight_smile: but come to think of it... a conditional in the
constructor might take care of the "is it wx.DateTime or
datetime.datetime" issue...
   
btw does this thread refer to mxDateTime, or does python have another DateTime?

As of Python 2.3 there is a datetime builtin module.

http://www.python.org/doc/2.3.4/lib/module-datetime.html
http://www.python.org/doc/2.3.4/lib/datetime-datetime.html

Regards,
Donnal Walter
Arkansas Children's Hospital

···

--- "Werner F. Bruhin" <werner.bruhin@free.fr> wrote:

Kenneth Gonsalves wrote:
> btw does this thread refer to mxDateTime, or does python have
> another DateTime?
>
As of Python 2.3 there is a datetime builtin module.