python - 使用“时间”库在 python 中到 gmt 和从 gmt 返回到秒的秒数
问题描述
对于以下代码,我希望 seconds_input 和 seconds_output 相同。但我得到一个小时的差异:
import time
seconds_input = 1571875186
date_struct1 = time.gmtime(seconds_input)
tm_str = time.strftime("%Y-%m-%d %H:%M:%S", date_struct1)
date_struct2 = time.strptime(tm_str, '%Y-%m-%d %H:%M:%S')
seconds_output = time.mktime(date_struct2)
print(seconds_input - seconds_output)
print(date_struct1, date_struct2)
我认为这可能是由于tm_isdst=-1
在 中date_struct2
,但我不确定如何将其设置为0
.
我已经尝试过date_struct2.tm_isdst = 0
,但出现以下错误:
AttributeError: readonly attribute
更新:
如果 seconds_input 对应于未来的日期,那么 seconds_input 和 seconds_output 将是相同的。
例如,如果 seconds_input = 1673084786。
解决方案
就我而言(GMT+9),我有 9 小时的差异。
所以我想你得到了你自己的 TZ 和 GMT 之间的区别——就像它应该的那样。
要正确转换,您必须添加(或减去)3600 秒,即 1 小时。
推荐阅读
- c++ - 如何在模板函数中有 C++ 字符串文字?
- javascript - 使用map jquery获取数组中的选项值
- c# - 尝试/捕获(OperationCanceledException)与 ContinueWith()
- c# - 如何使 ModelState.IsValid 中的 ActionResult 打开两个 url
- h.264 - 从似乎在 Windows 10 上具有不正确数据的 IMFSample 读取 YUY2 数据
- php - Plesk Api 多重查询 502 错误
- r - 数据帧 R 中值组合的计数
- jhipster - 尝试升级到 JHipster 版本 5.2.1,在匿名统计提示时停止
- c# - 在 XML 序列化期间删除属性的命名空间 xml
- sql - 如何在给定行程列表的情况下查询属于行程的所有用户?