python中的datetime.now在本地和服务器上运行时不同 [英] datetime.now in python different when running locally and on server
问题描述
我编写的代码使用预定义的时间,例如: 16:00 并将其与当前时间进行比较,并计算如下所示的差异:
now = datetime.datetime.now()
starttime = datetime.datetime.combine(datetime.date.today(),datetime.time(int(小时),int(分钟)))
dif = now - starttime
在本地运行这个ofc会在我的系统中使用这个时间,我想所有的东西都是正确的。但是,当我将它发布到服务器上并在那里运行时,时间是一个小时。那么我该如何解决这个问题,所以它总是使用我所在的时区?
我住在瑞典
谢谢大家,代码示例将深受赞赏。
EDIT1
其余代码如下所示:
if dif< datetime.timedelta(秒= 0):
hmm = 3
elif dif < datetime.timedelta(秒= 45 * 60):
t = dif.total_seconds()/ 60
time,trash = str(t).split(。)
time = time + '
elif dif < datetime.timedelta(秒= 48 * 60):
time =45
elif dif< datetime.timedelta(秒= 58 * 60):
time =HT
elif dif < datetime.timedelta(秒= 103 * 60):
t =(dif.total_seconds() - 840)/ 60
时间,trash = str(t).split(。)
time = time +'
elif dif < datetime.timedelta(seconds = 108 * 60):
time =90'
else:
time =FT
并使用您提供的导入我现在得到这个错误:
AttributeError:类型对象'datetime.datetime'没有属性'timedelta'
我试图做像这样但它没有帮助:
from datetime import datetime,time,timedelta
那么我该如何解决这个问题,所以它总是使用我的时区
在tz数据库中查找您的时区,例如使用 tzlocal
模块。在您的本地机器上运行 :
$ b
#!/ usr / bin / env python
import tzlocal#$ pip install tzlocal
print(tzlocal.get_localzone()。zone)
如果 tzlocal
能够获得时区id,那么您应该看到如下所示: Europe / Paris
。
在服务器上:
<!c $ c>#!/ usr / bin / env python $ b $ from datetime import datetime,time
import pytz#$ pip install pytz
tz = pytz.timezone ('Europe / Paris')#< - 将当地时区放在这里
now = datetime.now(tz)#当地时区的当前时间
naive_starttime = datetime.combine(now,time( int(hour),int(minute)))
starttime = tz.localize(naive_starttime,is_dst = None)#让它知道
dif = now - starttime
I am using Heroku to run some python code.
The code that i have written uses a predefined time like example: 16:00 and compares that with the current time and the calculates the difference like this:
now = datetime.datetime.now()
starttime = datetime.datetime.combine(datetime.date.today(), datetime.time(int(hour), int(minute)))
dif = now - starttime
Running this locally ofc uses the time in my system i guess and everything is correct. However when i post it on the server and run it there the time is one hour back. So how can i fix this so it always uses the timezone that i am in?
I live in Sweden
Thank you all, Code examples would be deeply appreciated.
EDIT1
Rest of the code looks like this:
if dif < datetime.timedelta(seconds=0):
hmm = 3
elif dif < datetime.timedelta(seconds=45*60):
t = dif.total_seconds() / 60
time, trash = str(t).split(".")
time = time+"'"
elif dif < datetime.timedelta(seconds=48*60):
time = "45'"
elif dif < datetime.timedelta(seconds=58*60):
time = "HT"
elif dif < datetime.timedelta(seconds=103*60):
t = (dif.total_seconds() - 840) / 60
time, trash = str(t).split(".")
time = time+"'"
elif dif < datetime.timedelta(seconds=108*60):
time = "90'"
else:
time = "FT"
and using the imports that you provided i get this error now:
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
i tried to do like this but it did not help:
from datetime import datetime, time, timedelta
So how can i fix this so it always uses the timezone that i am in?
Find your timezone in the tz database e.g., using tzlocal
module. Run on your local machine:
#!/usr/bin/env python
import tzlocal # $ pip install tzlocal
print(tzlocal.get_localzone().zone)
If tzlocal
has been capable to get the timezone id then you should see something like: Europe/Paris
. Pass this string to the server.
On the server:
#!/usr/bin/env python
from datetime import datetime, time
import pytz # $ pip install pytz
tz = pytz.timezone('Europe/Paris') # <- put your local timezone here
now = datetime.now(tz) # the current time in your local timezone
naive_starttime = datetime.combine(now, time(int(hour), int(minute)))
starttime = tz.localize(naive_starttime, is_dst=None) # make it aware
dif = now - starttime
这篇关于python中的datetime.now在本地和服务器上运行时不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!