我是python的新手,我需要将air_time
变量转换为本地机器时间,或者将current_time
变量转换为GMT,然后在此脚本中将它们彼此相减,但不知道如何去做。
from datetime import datetime
air_time_GMT = '2020-08-05 13:30:00'
air_time = datetime.strptime(air_time_GMT, '%Y-%m-%d %H:%M:%S')
current_time = datetime.now()
time_remaining = air_time - current_time
print(time_remaining)
答案 0 :(得分:1)
以下是注释中的一些解释方法:
from datetime import datetime, timezone
air_time_GMT = '2020-08-05 13:30:00'
# Python will assume your input is local time if you don't specify a time zone:
air_time = datetime.strptime(air_time_GMT, '%Y-%m-%d %H:%M:%S')
# ...so let's do this:
air_time = air_time.replace(tzinfo=timezone.utc) # using UTC since it's GMT
# again, if you don't supply a time zone, you will get a datetime object that
# refers to local time but has no time zone information:
current_time = datetime.now()
# if you want to compare this to a datetime object that HAS time zone information,
# you need to set it here as well. You can set local time zone via
current_time = current_time.astimezone()
print(current_time)
print(air_time-current_time)
>>> 2020-08-05 14:11:45.209587+02:00 # note that my machine is on UTC+2 / CEST
>>> 1:18:14.790413
我认为您应该在这里注意两件事。
答案 1 :(得分:0)
您要查找的命令是df %>%
group_by(ID) %>%
summarise(across(starts_with("S"), ~ sum(.x <= C)))
。
如果您使用此时间代替datetime.utcnow()
,则脚本将使用当前的GMT时间而不是您的当前时区/机器时间。
请注意:正如您在MrFuppes answer中所看到的,您需要格外小心。时区意识。但是,如果将所有时间对象保持为天真且采用UTC,则使用datetime.utcnow()的简单方法应该可以。