[Tutor] Dealing with Microseconds
Kent Johnson
kent37 at tds.net
Thu Sep 11 03:37:48 CEST 2008
On Wed, Sep 10, 2008 at 8:13 PM, Wayne Watson
<sierra_mtnview at sbcglobal.net> wrote:
> I'd like to allow a user to enter a drift rate in seconds per day into a
> program along with the date and time, a base, to begin calculation of
> subsequent recorded date and times. datetime seems to only allow hours,
> minutes and seconds in its formats. For example, 2.74 might be entered.
> Using 2.74 for a time that's 24 hours from the base would cause me to record
> the increase as 2 seconds. How can I account for fractional increases? I see
> that the tuple allows for microseconds, but I'm not sure arithmetic or
> formatting is allowed on it.
I don't really understand your problem statement, maybe a better
example would help?
Anyway both datetime and timedelta support microseconds so I think the
basic mechanism is there in the data structures. What is missing is
support in strptime() / strftime(). You can easily tack on
milliseconds to a formatted date. For example,
In [1]: from datetime import datetime, timedelta
In [4]: n
Out[4]: datetime.datetime(2008, 9, 10, 21, 31, 39, 937000)
In [8]: n.strftime('%Y%m%d_%H%M%S')
Out[8]: '20080910_213139'
In [11]: '%s.%3d' % (n.strftime('%Y%m%d_%H%M%S'), n.microsecond/1000)
Out[11]: '20080910_213139.937'
In [13]: n += timedelta(seconds=2, milliseconds=740)
In [14]: '%s.%3d' % (n.strftime('%Y%m%d_%H%M%S'), n.microsecond/1000)
Out[14]: '20080910_213142.677'
Kent
More information about the Tutor
mailing list