python - Microseconds lost in datetime -


i'm trying microseconds last 3 digits 0, suppose can milliseconds. os issue? i'm on win7x64 python 2.7.10. doing wrong? how else microseconds?

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

this not duplicate of

using %f strftime() in python microseconds

as approach gives me same zeroes @ end.

i believe that's known limitation of 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, example, here , here.

in python 2.7 source code, function modules/datetimemodule.c/datetime_now() calls modules/timemodule.c/floattime(), has following comment:

/* there 3 ways time:   (1) gettimeofday() -- resolution in microseconds   (2) ftime() -- resolution in milliseconds   (3) time() -- resolution in seconds   in cases return value float in seconds.   since on systems (e.g. sco odt 3.0) gettimeofday() may   fail, fall on ftime() or time().   note: clock resolution not imply clock accuracy! */ 

so windows platform using ftime() current time and, per the msdn page _ftime() (which no doubt ends in), no microseconds available. therefore python 2.7 gives milliseconds , leaves microseconds zero:

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

python 3.5 appears have full microsecond resolution may want consider switching (as if large number of other improvements weren't enough).


Comments