为了账号安全,请及时绑定邮箱和手机立即绑定

如何正确比较两个日期时间变量?

如何正确比较两个日期时间变量?

GCT1015 2021-06-09 13:13:57
我想比较两个日期时间变量来检测当前时间是否在周末时间。我的代码在这里:def is_weekend_off(weekend_table, trading_timezone):    now_trading_timezone = datetime.now(tz=trading_timezone)    is_weekend = False    for it in weekend_table:        condition1 = it[0] <= now_trading_timezone        condition2 = now_trading_timezone < it[1]        condition3 = condition1 and condition2        print('now = ' + str(now_trading_timezone))        print('it[0] = ' + str(it[0]))        print('it[1] = ' + str(it[1]))        print('condition1 = ' + str(condition1))        print('condition2 = ' + str(condition2))        time.sleep(60)        if condition3:            tmp = True        else:            tmp = False        is_weekend = is_weekend or tmp    return is_weekend结果在这里:now = 2018-09-10 21:50:59.001475-05:00it[0] = 2018-09-10 21:00:00-05:51it[1] = 2018-09-10 22:00:00-05:51condition1 = Falsecondition2 = True据我所知,condition1 应该是 True,而不是 False。我该如何纠正这个结果?
查看完整描述

1 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

it[0]有一个时区-05:51,所以当now_trading_timezone,他们的时区-05:00,进行比较it[0]now_trading_timezone变成有效的2018-09-10 21:50:59.00147551分,等于20:59:59.001475-05:51,小于it[0]2018-09-10 21:00:00-05:51,从而False为条件it[0] <= now_trading_timezone

it[0]使用now_trading_timezone's更正's 的时区,您可以使用以下datetime.replace方法:

it[0] = it[0].replace(tzinfo=now_trading_timezone.tzinfo)


查看完整回答
反对 回复 2021-06-15
  • 1 回答
  • 0 关注
  • 104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号