Why datetime module is so complicated?

Diez B. Roggisch deets at web.de
Sun Nov 14 17:53:42 CET 2010


Zeynel <azeynel1 at gmail.com> writes:

> It's about a week now I've been trying to convert a datetime object to
> seconds since epoch; the object is set to current time by class Rep()
> in Google App Engine:
>
> class Rep(db.Model):
>     ...
>     mCOUNT = db.IntegerProperty()
>     mDATE0 = db.DateTimeProperty(auto_now_add=True)
>     mWEIGHT = db.FloatProperty()
>
> I want to divide mDATE0 by the integer mCOUNT. I asked the same
> question here previously and also at stackoverflow. So far, I still
> cannot make it work. I would greatly appreciate if someone who is an
> expert in datetime operation in Python could help me understand this
> issue. Thank you.

>>> from datetime import *
>>> d = datetime.now()
>>> dir(d)
['__add__', '__class__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__str__', '__sub__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
>>> d.time()
datetime.time(17, 50, 54, 778159)
>>> d.ctime()
'Sun Nov 14 17:50:54 2010'
>>> d.time
<built-in method time of datetime.datetime object at 0x1003210f8>
>>> d.timetuple()
(2010, 11, 14, 17, 50, 54, 6, 318, -1)
>>> import time
>>> dir(time)
['__doc__', '__file__', '__name__', 'accept2dyear', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'gmtime', 'localtime', 'mktime', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname', 'tzset']
>>> help(time.mktime)

>>> time.mktime(d.timetuple())
1289753454.0
>>> 

Not that hard.

Diez



More information about the Python-list mailing list