outputting time in microseconds or milliseconds

Skip Montanaro skip at pobox.com
Thu Aug 8 08:03:31 EDT 2013


> i did:
>
>    from time import strftime, time
>    from datetime import datetime
>
>    now = datetime.now()
>
>    self.logfile.write('%s\t'%(strftime("%Y-%m-%d",)))
>    self.logfile.write('%s\t'%(now.strftime("%H:%M:%S.%f",)))

Note that you don't need the time module here.  Datetime objects have
what you need all by themselves:

from datetime import datetime
now = datetime.now()
self.logfile.write('%s\t%s\n' % (now.strftime("%Y-%m-%d"),
                                 now.strftime("%H:%M:%S.%f")))

The time module was historically the way Python did time, but it
wasn't at all object-oriented and provided no direct support for date
arithmetic.  When Tim Peters wrote datetime, the world became a better
place.  Cozy up to the datetime documentation and put the time module
aside.

Skip

Skip



More information about the Python-list mailing list