oddities in the datetime module

Max M maxm at mxm.dk
Fri Jan 14 17:03:16 EST 2005


Serge Orlov wrote:
> Max M wrote:

> Yes, you did. datetime.timetuple is those who want *time module* format, you should use datetime.data, datetime.time, datetime.year 
> and so on...
> 
> As they say, if the only tool you have is timetuple, everything looks like tuple <wink>
> Try this:
> 
>>>>dt = datetime(2005, 1, 1, 12, 0, 0)
>>>>dt.date()
> 
> datetime.date(2005, 1, 1)

This doesn't solve it. I don't think you understand my issue.

First of, it should be possible to easily convert between the datetime 
objects.

And eg. the date object doesn't have a datetime() method. Which it could 
easily have. Neither does time. They could have. But I don't think that 
is the way to solve it.



It is a problem if you make a subclass of datetime. Often you will ned 
to make datetime arithmetics with the new class.

Like: datetime_subclass_1 + datetime_subclass_2

The result of that is a plain datetime

In that case you will rather want your own subclass returned. But 
subclasses of datetime returns datetime objects. Not the subclass.

So to do an add of your own objects you must convert the output to your 
own class "manually"

class my_datetime(datetime):

     def __add__(self, other):
         result = datetime.__add__(self, other)
         return my_datetime(result.timetuple()[:6])

datetime(), time() etc. methods will not help with this.

-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list