Python datetime.fromtimestamp vs datetime.utcfromtimestamp,哪个更安全?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30921399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
datetime.fromtimestamp vs datetime.utcfromtimestamp, which one is safer to use?
提问by cyberbemon
I'm collecting some data from sensors and I get the timestamp from it like this:
我正在从传感器收集一些数据,并从中获取时间戳,如下所示:
"time": {
"seconds": 40,
"year": 115,
"month": 5,
"hours": 7,
"time": 1434549820776,
"date": 17,
"minutes": 3,
"day": 3,
"timezoneOffset": 420
},
I have a python script that processes the data coming from the sensors (incoming data is json format), I take the value of time
and converts into readable time format.
我有一个 python 脚本来处理来自传感器的数据(传入数据是 json 格式),我取值time
并将其转换为可读的时间格式。
I used datetime.fromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S')
and that returned '2015-06-17 15:03:40'
我用过datetime.fromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S')
然后回来了'2015-06-17 15:03:40'
Where as the datetime.utcfromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S')
Returned: '2015-06-17 14:03:40'
作为datetime.utcfromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S')
返回的地方:'2015-06-17 14:03:40'
As you can there is an hour Difference, so my question is which one is better to use?
你可以有一个小时的差异,所以我的问题是哪个更好用?
采纳答案by Julien Spronck
Looking at your json, you can see that the time stamp corresponds to 2015-06-17 07:03:40 locally.
查看你的json,可以看到本地时间戳对应的是2015-06-17 07:03:40。
The timezoneOffset tells you that there are 7 hours difference between local time and UTC time => the UTC time corresponding to your json is 2015-06-17 14:03:40.
timezoneOffset 告诉您本地时间和 UTC 时间之间有 7 小时的时差 => 您的 json 对应的 UTC 时间是 2015-06-17 14:03:40。
Since this is what you get when using datetime.utcfromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S')
(=> '2015-06-17 14:03:40'), this means that your time stamp is written in UTC time and you should therefore use utcfromtimestamp
if you want to be exact.
由于这是您在使用datetime.utcfromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S')
(=> '2015-06-17 14:03:40') 时得到的结果,这意味着您的时间戳是用 UTC 时间写的,因此utcfromtimestamp
如果您想准确,您应该使用它。
回答by textshell
Basicly you want to use what works. Ideally your documentation will contain in what timezone the sensors report their time. Likely it's in the same timezone as the person setting up the sensors set their time in the first place, because it's unlikely sensors contain time zone awareness.
基本上,您想使用有效的方法。理想情况下,您的文档将包含传感器报告时间的时区。很可能它与设置传感器的人首先设置时间处于同一时区,因为传感器不太可能包含时区感知。
If you are free to decide what time is setup in the sensors i would generally recommend to use UTC because that will spare you all kinds of trouble with daylight saving time etc. Also it works well in international teams.
如果您可以自由决定在传感器中设置什么时间,我通常会建议使用 UTC,因为这样可以免除夏令时等各种麻烦。而且它在国际团队中运行良好。
Maybe even datetime.fromtimestamp(1434549820776/1000, timezone)
is the right answer if you can't be sure that the time zone setting of the PC the program runs on and the sensors match.
datetime.fromtimestamp(1434549820776/1000, timezone)
如果您不能确定运行程序的 PC 的时区设置和传感器是否匹配,甚至可能是正确的答案。
回答by Serge Ballesta
Both are correct, simply they do not give you same time. Both assume that timestamp is the number of millisecond from EPOCH (normally 1/01/1970 00:00 UTC) and :
两者都是正确的,只是它们不会给您相同的时间。两者都假设时间戳是从 EPOCH 开始的毫秒数(通常是 1/01/1970 00:00 UTC)并且:
fromtimestamp
give you the date and time in local timeutcfromtimestamp
gives you the date and time in UTC.
fromtimestamp
给你当地时间的日期和时间utcfromtimestamp
为您提供 UTC 日期和时间。
As I do not know where you live (UK ?) I cannot say more, in Spain, France, Belgium and Danmark, local time is UTC + 1 in winter and UTC + 2 in summer.
由于我不知道你住在哪里(英国?),我不能说更多,在西班牙、法国、比利时和丹麦,当地时间在冬天是 UTC + 1,在夏天是 UTC + 2。
Youmust know if you need UTC time or local time.
您必须知道您需要 UTC 时间还是本地时间。