python - utcfromtimestamp 的相反函数?

标签 python datetime

utcfromtimestamp() 的相反函数是什么?

timestamp() 显然没有考虑时区,如下例所示:

import pandas as pd
import datetime
start = pd.datetime(2000, 1, 1, 0, 0, 0)
asFloat = start.timestamp()
startDifferent = datetime.datetime.utcfromtimestamp(asFloat)
startDifferent
Out[8]: datetime.datetime(1999, 12, 31, 23, 0)

最佳答案

utctimetuple --> calendar.timegm --> utcfromtimestamp 形成一个往返:

import calendar
import datetime as DT
start = DT.datetime(2000, 1, 1, 0, 0, 0)

utc_tuple = start.utctimetuple()
utc_timestamp = calendar.timegm(utc_tuple)
startDifferent = DT.datetime.utcfromtimestamp(utc_timestamp)
print(startDifferent)
# 2000-01-01 00:00:00

timestamp --> fromtimestamp 也是往返:

asFloat = start.timestamp()
startDifferent = DT.datetime.fromtimestamp(asFloat)
print(startDifferent)
# 2000-01-01 00:00:00

没有 utc 等同于 timestamp 直接从 datetime.datetime 到时间戳。最接近的等效项是 calendar.timegm(date.utctimetuple())


大致描述了方法之间的关系:

                o------------o
                |            |  DT.datetime.utcfromtimestamp (*)
                |            |<-----------------------------------o
                |            |                                    |
                |            |  DT.datetime.fromtimestamp         |
                |  datetime  |<-------------------------------o   |
                |            |                                |   |
                |            |    .timestamp                  |   |
                |            |----------------------------o   |   | 
                |            |                            |   |   |
                o------------o                            |   |   |
                   |   ^                                  |   |   |
        .timetuple |   |                                  |   |   |
 .utctimetuple (*) |   | DT.datetime(*tup[:6])            |   |   |
                   v   |                                  v   |   |
                o------------o                          o------------o
                |            |-- calendar.timegm (*) -->|            |
                |            |                          |            |
                |            |---------- time.mktime -->|            |
                |  timetuple |                          |  timestamp |
                |            |<-- time.localtime -------|            |
                |            |                          |            |
                |            |<-- time.gmtime (*)-------|            |
                o------------o                          o------------o

(*) 将其输入解释为 UTC 并返回应解释为 UTC 的输出。

关于python - utcfromtimestamp 的相反函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54935647/

相关文章:

python - 从现有文件中提取多个新的制表符分隔文件

python - 多于 2 个级别的 python 中的多处理

javascript - 时差并在javascript中转换为小时和分钟

mysql - MySQL 中重叠日期时间范围的总和

python - Anaconda 在 TravisCI for python 2.7 上构建故障处理程序的轮子失败

python - 我如何在 Python 中接收来自 IBs API 的数据?

Python Elasticsearch 超时

datetime - 在 R 中合并聚合数据

c# - 找出日期时间在未来多少天

c# - DateTime.ParseExact 不适用于 "yyyy-MM-dd HH:mm:ss"