about time

David Bolen db3l at fitlinxx.com
Wed Mar 26 11:48:30 EST 2003


Peter Hansen <peter at engcorp.com> writes:

> Byron Morgan wrote:
> > 
> > The time.py module seems to have no straightforward method to produce a time
> > display with seconds expressed in floating point precision, even though
> > clock() will produce microseconds. Am I missing something in the docs?
> 
> This is unclear to me.  time.time() returns a float, with the fractional
> part representing a fraction of a second, therefore certainly something
> that could be called "floating point precision" (whatever that means).  
> What about it don't you like?

I read it as the problem being the "produce a time display" portion
and not the underlying value itself.  So time.time() returns a float,
but none of time.ctime, time.asctime or time.strftime supply simple
ways to get the additional precision as part of a display string.

But to the original poster, there's nothing stopping you from
including the extra precision in any output by directly using the time
value you have, just by using the time value to compute the display.
If you need the information embedded as part of a larger string value,
your best bet is probably to dynamically build up a format string for
time.strftime which includes the necessary microseconds as a literal
part of the string, separately from the strftime call.

For example:

    >>> t = time.time()
    >>> t
    1048697222.525
    >>> time.ctime(t)
    'Wed Mar 26 11:47:02 2003'
    >>> format = '%m/%d/%Y %H:%M:%S' + '.%03d' % (t%1 * 1000)
    >>> format
    '%m/%d/%Y %H:%M:%S.524'
    >>> time.strftime(format,time.localtime(t))
    '03/26/2003 11:47:02.524'


--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/




More information about the Python-list mailing list