datetime – How can I create a Python timestamp with millisecond granularity?
datetime – How can I create a Python timestamp with millisecond granularity?
import time
time.time() * 1000
where 1000 is milliseconds per second. If all you want is hundredths of a second since the epoch, multiply by 100.
In Python, datetime.now()
might produce a value with more precision than time.time()
:
from datetime import datetime, timezone, timedelta
now = datetime.now(timezone.utc)
epoch = datetime(1970, 1, 1, tzinfo=timezone.utc) # use POSIX epoch
posix_timestamp_millis = (now - epoch) // timedelta(milliseconds=1) # or `/ 1e3` for float
In theory, time.gmtime(0)
(the epoch used by time.time()
) may be different from 1970
.