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

问题描述 投票:2回答:1

我想比较两个日期时间变量来检测当前时间是否在周末时间。

我的代码在这里:

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:00
it[0] = 2018-09-10 21:00:00-05:51
it[1] = 2018-09-10 22:00:00-05:51
condition1 = False
condition2 = True

据我所知,condition1应该是True,而不是False。我如何更正此结果?

python datetime
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.