4

I'm trying to get microseconds but the last three digits always are 0, so I suppose I only can get milliseconds. Is this an OS issue? I'm on Win7x64 with Python 2.7.10. Am I doing something wrong? How else would I get microseconds?

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2016, 8, 9, 4, 33, 28, 504000)

This is not a duplicate of

Using %f with strftime() in Python to get microseconds

as this approach gives me the same zeroes at the end.

doc
  • 127
  • 1
  • 11
  • 1
    It might be an OS issue; here on LMDE Betsy (Linux) 64-bit and Python 2.7.9 I can get microseconds: `datetime.datetime(2016, 8, 9, 12, 46, 14, 209624)` – Fred Barclay Aug 09 '16 at 02:48
  • Here's the output from `OS X` using Python 2.7.10: `datetime.datetime(2016, 8, 8, 19, 49, 23, 952833)`. In Windoze I get `datetime.datetime(2016, 8, 8, 19, 49, 23, 952000)`... so I'd say Fred nailed it. – l'L'l Aug 09 '16 at 02:49
  • 1
    Relevant: http://stackoverflow.com/questions/1938048/high-precision-clock-in-python, http://stackoverflow.com/questions/16507924/bad-microsecond-resolution-in-python – Amadan Aug 09 '16 at 02:56

1 Answers1

4

I believe that's a known limitation of the Python 2.7 implementation under Windows:

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2016, 8, 9, 10, 50, 59, 657000)
>>> datetime.datetime.now()
datetime.datetime(2016, 8, 9, 10, 51, 1, 561000)
>>> datetime.datetime.now()
datetime.datetime(2016, 8, 9, 10, 51, 2, 314000)
>>> datetime.datetime.now()
datetime.datetime(2016, 8, 9, 10, 51, 2, 906000)
>>> datetime.datetime.now()
datetime.datetime(2016, 8, 9, 10, 51, 9, 277000)

See, for example, here and here.

In the Python 2.7 source code, the function Modules/datetimemodule.c/datetime_now() eventually calls Modules/timemodule.c/floattime(), which has the following comment:

/* There are three ways to get the time:
  (1) gettimeofday() -- resolution in microseconds
  (2) ftime() -- resolution in milliseconds
  (3) time() -- resolution in seconds
  In all cases the return value is a float in seconds.
  Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  fail, so we fall back on ftime() or time().
  Note: clock resolution does not imply clock accuracy! */

So the Windows platform is obviously using ftime() to get the current time and, as per the MSDN page for _ftime() (which it no doubt ends up in), no microseconds are available. Therefore Python 2.7 just gives you the milliseconds and leaves the microseconds as zero:

return (double)t.time + (double)t.millitm * (double)0.001;

Python 3.5 appears to have a full microsecond resolution so you may want to consider switching to that (as if the large number of other improvements weren't enough).

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • It's not a limitation of Windows in general, but it may be a limitation of the API Python 2.7 is using. In Windows 10 at least, using Python 3.5.1, `datetime.datetime.now()` is filling in the low three digits of the microseconds for me, e.g. `>>> datetime.datetime.now()`, `datetime.datetime(2016, 8, 8, 23, 7, 13, 646615)` – ShadowRanger Aug 09 '16 at 03:07
  • @Shadow, yes, it's not so much a limitation of Windows as the usage of the Windows API by Python. Editing to assign blame correctly :-) – paxdiablo Aug 09 '16 at 03:15
  • @doc, that sounds like an excellent title to a new SO question. Nudge nudge wink wink. – paxdiablo Aug 09 '16 at 22:53
  • I've read some stuff about Python 3 being slower in some cases and it seemed to match with what I have coded. Don't see any reason for a new question. But thanks :) – doc Aug 09 '16 at 23:43