使用if语句

时间:2016-04-02 19:20:39

标签: python python-2.7

我的程序中的if语句有问题。我正在尝试检查current_time的值,看它是否小于stop_time

示例:当变量current_time18:36stop_time19:00时,我想跳转到打印出来的if语句是“中途” 。如果current_time18:36stop_time19:30,那么我还想跳转到打印出来的if语句是“中途”。但是当current_time18:36stop_time18:30时,我想跳转到打印it is finished的if语句。

这是我的代码:

profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', 'source.db'))
conn1 = database.connect(profilePath)
cur1 = conn1.cursor()
cur1.execute('SELECT start_date, stop_date FROM programs where channel=?', [channel])
stop_date = cur1.fetchone()

if stop_date is not None:
    stop_date = str(stop_date[1])
    current_time = datetime.datetime.now()
    stop_time = time.strptime(stop_date, '%Y%m%d%H%M%S')
    stop_time = datetime.datetime.fromtimestamp(time.mktime(stop_time))

    if current_time > start_time and current_time < stop_time or current_time < stop_time:
       print "program is half way"
else:
    print "program has finished"

以下是current_time的输出:

2016-04-01 18:37:37.664000
2016-04-01 18:37:37.665000
2016-04-01 18:37:37.667000
2016-04-01 18:37:37.668000
2016-04-01 18:37:37.670000
2016-04-01 18:37:37.672000
2016-04-01 18:37:37.674000

以下是stop_time的输出:

2016-04-02 19:00:00
2016-04-02 18:30:00
2016-04-02 18:30:00
2016-04-02 18:30:00
2016-04-02 19:00:00
2016-04-02 19:00:00
2016-04-02 18:30:00

current_time18:36stop_time19:00时,它会跳转到打印“程序中途”的if语句。当current_time18:36stop_time19:30时,它也会跳转到打印“程序中途”的if语句。当current_time18:36stop_time18:30时,我应该在时间过去并且程序已完成时获得program has finished,但是时间过后我得到program is half way。我的代码有问题,我无法弄清楚是什么。

我想在program is half way未超过current_time时打印stop_time,我想在program has finished&gt;时打印current_time stop_time

您能否向我展示一个示例,说明如何检查current_timestop_time以查看current_time&lt; stop_time打印program is half way并检查是否current_time&gt; stop_time打印program has finished

0 个答案:

没有答案
相关问题