[英]Getting astimezone error in linux machine
I am using linux aws machine and there is a timezone difference when I do datetime.datetime.now.我正在使用 linux aws 机器,并且在执行 datetime.datetime.now 时存在时区差异。 So I have tried this method to overcome the timezone error
所以我尝试了这种方法来克服时区错误
format = "%Y-%m-%d %H:%M:%S %Z%z"
current_date = datetime.datetime.now()
now_asia = current_date.astimezone(timezone('Asia/Kolkata'))
print(now_asia.strftime(format))
when I do my window machine I didnt get any error.当我做我的 window 机器时,我没有收到任何错误。 The same lines when I use in my linux machine I am getting "ValueError: astimezone() cannot be applied to a naive datetime"
当我在我的 linux 机器中使用相同的行时,我收到“ValueError: astimezone() cannot be applied to a naive datetime”
To debug this I tried the methods which is mentioned in this link pytz and astimezone() cannot be applied to a naive datetime为了调试这个我尝试了这个链接中提到的方法pytz and astimezone() cannot be applied to a naive datetime
when I tried the first answer I am not getting any error but the timezone is not converted.当我尝试第一个答案时,我没有收到任何错误,但时区未转换。 when I tried the second answer I am getting an error 'AttributeError: 'module' object has no attribute 'utcnow'
当我尝试第二个答案时,我收到一个错误'AttributeError:'module' object has no attribute 'utcnow'
I tried this我试过这个
>>>loc_date = local_tz.localize(current_date)
>>> loc_date
datetime.datetime(2020, 4, 6, 7, 23, 36, 702645, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD>)
>>> loc_date.strftime(format)
'2020-04-06 07:23:36 IST+05:30'
I am getting this,so according to indian time if we add 5:30 it will be correct.我得到了这个,所以根据印度时间,如果我们加上 5:30,那将是正确的。 How should i do it.
我该怎么做。
Please check that you are indeed running the Python 3.7 interpreter in the cloud.请检查您是否确实在云中运行 Python 3.7 解释器。 Citing the documentation for the
astimezone()
function :引用
astimezone()
function 的文档:
Changed in version 3.6: The astimezone() method can now be called on naive instances that are presumed to represent system local time .
在 3.6 版更改:现在可以在假定代表系统本地时间的幼稚实例上调用astimezone() 方法。
Indeed, I just tested the script using Python 3.5.9 and pytz
2019.3 and I get事实上,我刚刚使用 Python 3.5.9 和
pytz
测试了脚本,我得到了
File "timez.py", line 6, in <module>
now_asia = current_date.astimezone(timezone('Asia/Kolkata'))
ValueError: astimezone() cannot be applied to a naive datetime
But when using Python 3.7.6 on an Amazon Linux 2 AMI instance, the code runs correctly.但是在 Amazon Linux 2 AMI 实例上使用 Python 3.7.6 时,代码运行正常。
Nevertheless, I would suggest to use timezone-aware datetimes from the start.不过,我建议从一开始就使用时区感知日期时间。
In the code you're referencing , you're getting that there's no utcnow
attribute because that code imports from datetime import datetime
, while you're doing import datetime
.在您引用的代码中,您会发现没有
utcnow
属性,因为该代码from datetime import datetime
,而您正在执行import datetime
。 To make it work, you would use为了使它工作,你会使用
now_utc = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
But note that the Python documentation now suggests you to use the tz
parameter in datetime.now
:但请注意, Python 文档现在建议您在
datetime.now
中使用tz
参数:
import datetime
import pytz
now_utc = datetime.datetime.now(tz=pytz.utc)
local_tz = pytz.timezone('Asia/Kolkata')
now_asia = now_utc.astimezone(local_tz)
format = "%Y-%m-%d %H:%M:%S %Z%z"
print(now_asia.strftime(format))
which prints 2020-04-22 09:25:21 IST+0530
in my case.在我的情况下打印
2020-04-22 09:25:21 IST+0530
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.