Get POSIX/Unix time in seconds and nanoseconds in Python?

Bill picture Bill · Mar 7, 2010 · Viewed 82.8k times · Source

I've been trying to find a way to get the time since 1970-01-01 00:00:00 UTC in seconds and nanoseconds in python and I cannot find anything that will give me the proper precision.

I have tried using time module, but that precision is only to microseconds, so the code I tried was:

import time

print time.time()

which gave me a result like this:

1267918039.01

However, I need a result that looks like this:

1267918039.331291406

Does anyone know a possible way to express UNIX time in seconds and nanoseconds? I cannot find a way to set the proper precision or get a result in the correct format. Thank you for any help

Answer

MattH picture MattH · Mar 7, 2010

Your precision is just being lost due to string formatting:

>>> import time
>>> print "%.20f" % time.time()
1267919090.35663390159606933594