I'm trying to extend Python's datetime.datetime
class with a couple of extra methods. So, for example I'm doing:
import datetime
class DateTime(datetime.datetime):
def millisecond(self):
return self.microsecond/1000
but then if I do
>>> d = DateTime(2010, 07, 11, microsecond=3000)
>>> print d.millisecond()
3
>>> delta = datetime.timedelta(hours=4)
>>> newd = d + delta
>>> print newd.millisecond()
AttributeError: 'datetime.datetime' object has no attribute 'millisecond'
This is obviously because doing d + delta
calls the datetime.datetime.__add__()
method which returns a datetime.datetime
object.
Is there any way I can make this datetime.datetime
object convert to a DateTime
object? Or would I have to reimplement all the operators in my DateTime
subclass to return the correct type?