[英]Python convert timestamp to unix
I know these questions have been asked before but I'm struggling to convert a timestamp string to a unix time and figuring out whether the datetime objects are naive or aware我知道以前有人问过这些问题,但我正在努力将时间戳字符串转换为 unix 时间并弄清楚 datetime 对象是天真还是有意识
For example, to convert the time "2021-05-19 12:51:47" to unix:例如,要将时间“2021-05-19 12:51:47”转换为 unix:
>>> from datetime import datetime as dt
>>> dt_obj = dt.strptime("2021-05-19 12:51:47", "%Y-%m-%d %H:%M:%S")
>>> dt_obj
datetime.datetime(2021, 5, 19, 12, 51, 47)
is dt_obj
naive or aware and how would you determine this? dt_obj
是幼稚的还是有意识的,您将如何确定这一点? The methods on dt_obj such as timetz
, tzinfo
, and tzname
don't seem to indicate anything - does that mean that dt_obj is naive? dt_obj 上的方法,例如timetz
、 tzinfo
和tzname
似乎没有任何指示——这是否意味着 dt_obj 是幼稚的?
Then to get unix:然后得到unix:
>>> dt_obj.timestamp()
1621421507.0
However when I check 1621421507.0
on say https://www.unixtimestamp.com then it tells me that gmt for the above is Wed May 19 2021 10:51:47 GMT+0000, ie 2 hours behind the original timestamp?但是,当我在https://www.unixtimestamp.com上检查1621421507.0
时,它告诉我上面的 gmt 是 2021 年 5 月 19 日星期三 10:51:47 GMT+0 之后?
since Python's datetime treats naive datetime as local time by default, you need to set the time zone ( tzinfo
attribute):由于 Python 的 datetime 默认将 naive datetime 视为本地时间,因此您需要设置时区( tzinfo
属性):
from datetime import datetime, timezone
# assuming "2021-05-19 12:51:47" represents UTC:
dt_obj = datetime.fromisoformat("2021-05-19 12:51:47").replace(tzinfo=timezone.utc)
Or, as @Wolf suggested, instead of setting the tzinfo attribute explicitly, you can also modify the input string by adding "+00:00"
which is parsed to UTC;或者,正如@Wolf 所建议的那样,您也可以通过添加解析为UTC 的"+00:00"
来修改输入字符串,而不是显式设置tzinfo 属性;
dt_obj = datetime.fromisoformat("2021-05-19 12:51:47" + "+00:00")
In any case, the result无论如何,结果
dt_obj.timestamp()
# 1621428707.0
now converts as expected on https://www.unixtimestamp.com/ :现在在https://www.unixtimestamp.com/上按预期转换:
As long as you don't specify the timezone when calling strptime
, you will produce naive datetime
objects.只要您在调用strptime
时不指定时区,就会产生天真的datetime
对象。 You may pass time zone information via %z
format specifier and +00:00
added to the textual date-time representation to get a timezone aware datetime
object:您可以通过%z
格式说明符传递时区信息,并将+00:00
添加到文本日期时间表示中,以获得时区感知datetime
时间 object:
from datetime import datetime
dt_str = "2021-05-19 12:51:47"
print(dt_str)
dt_obj = datetime.strptime(dt_str+"+00:00", "%Y-%m-%d %H:%M:%S%z")
print(dt_obj)
print(dt_obj.timestamp())
The of above script is this:上面的脚本是这样的:
2021-05-19 12:51:47
2021-05-19 12:51:47+00:00
1621428707.0
Naive datetime instances are assumed to represent local time and this method relies on the platform C mktime() function to perform the conversion.假定原始日期时间实例表示本地时间,并且此方法依赖于平台 C mktime() function 来执行转换。
So using this does automatically apply yours machine current timezone, following recipe is given to calculate timestamp from naive datetime without influence of timezone:因此,使用它确实会自动应用您的机器当前时区,给出以下配方以从天真的日期时间计算时间戳,而不受时区的影响:
timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.