如何在不失去微秒精度的情况下在Python中转换为时间戳和从时间戳转换?

时间:2016-09-14 14:04:52

标签: python django datetime timestamp precision

到底是怎么回事?我认为它必须与浮动不精确有关,但我该如何解决呢?

> from datetime import datetime
> from django.utils.dateparse import parse_datetime
> for i in range(10):
...    dt = parse_datetime('2016-09-13T14:32:10.40455{}'.format(i))
...    ts = dt.timestamp()
...    dt2 = datetime.fromtimestamp(ts)
...    print("{} == {}: {}".format(dt, dt2, dt == dt2))

2016-09-13 14:32:10.404550 == 2016-09-13 14:32:10.404550: True
2016-09-13 14:32:10.404551 == 2016-09-13 14:32:10.404551: True
2016-09-13 14:32:10.404552 == 2016-09-13 14:32:10.404551: False
2016-09-13 14:32:10.404553 == 2016-09-13 14:32:10.404552: False
2016-09-13 14:32:10.404554 == 2016-09-13 14:32:10.404553: False
2016-09-13 14:32:10.404555 == 2016-09-13 14:32:10.404555: True
2016-09-13 14:32:10.404556 == 2016-09-13 14:32:10.404556: True
2016-09-13 14:32:10.404557 == 2016-09-13 14:32:10.404556: False
2016-09-13 14:32:10.404558 == 2016-09-13 14:32:10.404557: False
2016-09-13 14:32:10.404559 == 2016-09-13 14:32:10.404558: False

PS:我使用的是Python 3,Django 1.9.6

0 个答案:

没有答案