-1

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 :^)

kmiklas
  • 13,085
  • 22
  • 67
  • 103

1 Answers1

1

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!

Sam
  • 1,406
  • 1
  • 8
  • 11
  • 2
    Unfortunately having a microsecond attribute doesn't tell you anything about the expected accuracy or resolution of that attribute. In fact it might depend on the OS you're running on. – Mark Ransom May 29 '20 at 18:58