What is the most accurate/granular Python3 timestamp?

236 Views Asked by At

What is the most accurate timestamp in Python3?

Associated question: would it make sense to write a little timestamp program in C and invoke it from Python, or perhaps use C++'s std::chrono::high_resolution_clock? Thx, Keith :^)

1

There are 1 best solutions below

1
On

python's datetime package has both datetime and time classes, which are accurate to microseconds. A small demonstration:

>>> import datetime
>>> d = datetime.datetime.now()
>>> elements = ['year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond']
>>> for element in elements:
>>>     print(f'{element}: {getattr(d,element)}')
year: 2020
month: 5
day: 29
hour: 11
minute: 48
second: 48
microsecond: 979449

Hope this helps, Happy Coding!