Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51924

Re: outputting time in microseconds or milliseconds

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: outputting time in microseconds or milliseconds
Date 2013-08-04 10:33 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-7D52A5.10334804082013@news.panix.com> (permalink)
References <b5df0633-c6fe-4489-adda-5b9f270a904d@googlegroups.com> <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com>

Show all headers | View raw


In article <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com>,
 matt.doolittle33@gmail.com wrote:

> self.logfile.write('%s\t'%(str(time())))
> [...]
> 1375588774.89
> [...]
> Why is it only giving me the centisecond precision?  the docs say i should 
> get microsecond precision

When citing documentation, it's a good idea to provide a link to the 
docs page, and/or a direct quote of what you read.

I'm looking at http://docs.python.org/2/library/time.html#time.time, 
which says, "not all systems provide time with a better precision than 1 
second".  So, I don't know where you got the impression that you're 
guaranteed microsecond precision.

Earlier in the thread, you did mention that you're on Ubuntu, and there 
you do indeed get pretty good precision.  I'm not 100% sure if it's good 
to the microsecond (it appears to be), but it's certainly better than 
centisecond.

Anyway, your problem appears to be that str(float) gives you two digits 
after the decimal (at least for values in the range we're talking about 
here), but repr() will give you more:

>>> t = time.time()
>>> str(t)
'1375626035.26'
>>> repr(t)
'1375626035.260934'

I don't know anywhere that those behaviors are guaranteed, however.  If 
you want to make sure you print a float with 6 digits after the decimal, 
you should use %f, not %s:

>>> '%.6f' % t
'1375626035.260934'

Of course, if the underlying system call that time.time() invokes 
returns less precision than microseconds, some of those 6 digits may 
always be zero.  Or worse, garbage.

Taking a step back, you're probably better off using datetimes.  You'll 
get all this conversion nonsense for free:

>>> print datetime.datetime.utcnow()
2013-08-04 14:33:09.255096

When I write an operating system, I'm going to have it keep time in 
units of YiTp (Yobi Plank times).

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-02 03:54 -0700
  Re: outputting time in microseconds or milliseconds Dave Angel <davea@davea.name> - 2013-08-02 12:08 +0000
  Re: outputting time in microseconds or milliseconds Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-08-02 13:59 +0200
    Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-02 06:17 -0700
      Re: outputting time in microseconds or milliseconds Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-08-05 08:15 +0200
  Re: outputting time in microseconds or milliseconds Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-02 12:35 +0000
    Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-02 08:09 -0700
  Re: outputting time in microseconds or milliseconds Skip Montanaro <skip@pobox.com> - 2013-08-02 07:37 -0500
    Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-02 07:18 -0700
      Re: outputting time in microseconds or milliseconds Skip Montanaro <skip@pobox.com> - 2013-08-02 10:15 -0500
  Re: outputting time in microseconds or milliseconds Chris Angelico <rosuav@gmail.com> - 2013-08-02 13:50 +0100
  Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-04 04:30 -0700
    Re: outputting time in microseconds or milliseconds Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2013-08-04 13:57 +0200
    Re: outputting time in microseconds or milliseconds Dave Angel <davea@davea.name> - 2013-08-04 13:38 +0000
    Re: outputting time in microseconds or milliseconds Roy Smith <roy@panix.com> - 2013-08-04 10:33 -0400
      Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-07 19:51 -0700
        Re: outputting time in microseconds or milliseconds Skip Montanaro <skip@pobox.com> - 2013-08-08 07:03 -0500
          Re: outputting time in microseconds or milliseconds Roy Smith <roy@panix.com> - 2013-08-08 09:30 -0400

csiph-web