Python datetime: how to save all `us (microsecond)`?

In [25]: datetime.fromtimestamp(15179052980380002/10000000)
Out[25]: datetime.datetime(2018, 2, 6, 17, 21, 38, 38000)

As you can see here, it is converted to 38000usinstead of 38002us( us= microsecond)

How can I save the “2” right digit?

+4
source share
2 answers

You can try a simple way to store microseconds separately, then replacein datetimeobject

>>> timestamp = 15179052980000000
>>> m_seconds = 38002
>>> dt = datetime.fromtimestamp(timestamp/10000000)
>>> dt
>>> datetime.datetime(2018, 2, 6, 16, 21, 38)

Then finally replace dtwith the desired microseconds

>>> dt.replace(micorsecond=m_seconds)
>>> dt
>>> datetime.datetime(2018, 2, 6, 16, 21, 38, 38002)
0
source

You will need nanosecond precision to encode this value - note that you do not have .038002, you have 0.0380002 (extra digit).

It is 38000us (rounded) or 38000200ns.

+4
source

Source: https://habr.com/ru/post/1693233/


All Articles