默认情况下,该
datetime对象
naive位于Python中,因此您需要使它们都成为天真
datetime对象或感知对象。可以使用以下方法完成:
import datetimeimport pytzutc=pytz.UTCchallenge.datetime_start = utc.localize(challenge.datetime_start) challenge.datetime_end = utc.localize(challenge.datetime_end) # now both the datetime objects are aware, and you can compare them
注意:这将引发一个
ValueErrorif
tzinfo值。如果您不确定,请使用
start_time = challenge.datetime_start.replace(tzinfo=utc)end_time = challenge.datetime_end.replace(tzinfo=utc)
顺便说一句,您可以在带有时区信息的datetime.datetime对象中格式化UNIX时间戳,如下所示
d = datetime.datetime.utcfromtimestamp(int(unix_timestamp))d_with_tz = datetime.datetime( year=d.year, month=d.month, day=d.day, hour=d.hour, minute=d.minute, second=d.second, tzinfo=pytz.UTC)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)