比较声明中的时间

时间:2016-04-12 15:29:30

标签: python python-2.7

我的代码需要一些帮助。我想在11:30PM12:00AMprogram_stop_time之间的时间进行比较,以显示时间11:45PM,看看我是否可以进入声明。

当我尝试这个时:

if program_stop_time > epg_time_1 and program_stop_time < epg_time_2:
    print "Now you are working on resize on the button"

它不会让我参与声明。

以下是完整代码:

half_hour = str("11:30AM")
one_hour = str("12:00AM")
one_hour_half = str("12:30AM")
epg_time_1 = time.strptime(half_hour, '%I:%M%p')
epg_time_2 = time.strptime(one_hour, '%I:%M%p')
epg_time_3 = time.strptime(one_hour_half, '%I:%M%p')
stop_time = str("11:45PM")
program_stop_time = time.strptime(stop_time, '%I:%M%p')
program_stop_clock_list.append(program_stop_time)

if program_stop_time > epg_time_1 and program_stop_time < epg_time_2:
    print "Now you are working on resize on the button"
else:
    print "Now you are working on to remove the button"

这是epg_time_1 time.struct_time:

time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=23, tm_min=30, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)

这是epg_time_2 time.struct_time:

time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)

这里是program_stop_time time.struct_time:

time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=23, tm_min=45, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)

我希望在epg_time_1显示时间11:30PMepg_time_2显示时间12:00AMprogram_stop_time进行比较时显示时间11:45PM,如果epg_time_1显示12:00AM时间program_stop_time之后的时间11:45PM,那么我想进入else语句。

你能告诉我如何使用我的代码做到这一点吗?

2 个答案:

答案 0 :(得分:0)

问题在于您尝试使用的日期是在“日”边界内发生的。所以你想要11:30PM < 12:00AM < 12:30AM,但是在你指定它们的方式中,转换为timedatetime对象时,它们都在同一天,所以12:00AM < 12:30AM < 11:30PM。您可以通过转换为datetime个对象并将一天添加到AM时间来解决此问题。

import time
from datetime import datetime
from datetime import timedelta

half_hour = str("11:30PM")
one_hour = str("12:00AM")
one_hour_half = str("12:30AM")
epg_time_1 = datetime.strptime(half_hour, '%I:%M%p')
epg_time_2 = datetime.strptime(one_hour, '%I:%M%p')
epg_time_3 = datetime.strptime(one_hour_half, '%I:%M%p')
stop_time = str("11:45PM")
program_stop_time = datetime.strptime(stop_time, '%I:%M%p')
# program_stop_clock_list.append(program_stop_time)

epg_time_2 += timedelta(days=1)
epg_time_3 += timedelta(days=1)

if program_stop_time > epg_time_1 and program_stop_time < epg_time_2:
    print("Now you are working on resize on the button")
else:
    print("Now you are working on to remove the button")

答案 1 :(得分:0)

首先,您可能希望在代码中将11:30AM更改为11:30PM

其次你应该使用datetime个对象,因为上午12:00是一天的开始,晚上11:30是一天的结束,你似乎遇到了问题。

import datetime

half_hour = str("1/1/1997 11:30PM")
one_hour = str("2/1/1997 12:00AM")
one_hour_half = str("12:30AM")
epg_time_1 = datetime.datetime.strptime(half_hour, '%d/%m/%Y %I:%M%p')
epg_time_2 = datetime.datetime.strptime(one_hour, '%d/%m/%Y %I:%M%p')
epg_time_3 = datetime.datetime.strptime(one_hour_half, '%d/%m/%Y %I:%M%p')
stop_time = str("1/1/1997 11:45PM")
program_stop_time = datetime.datetime.strptime(stop_time, '%I:%M%p')

if program_stop_time > epg_time_1 and program_stop_time < epg_time_2:
    print "program_stop_time is between epg_time_1 and epg_time_2"
else:
    print "program_stop_time is NOT between epg_time_1 and epg_time_2"

无论如何,解决方案取决于您拥有的数据。如果您可以获得日期而不仅仅是时间作为您的程序的输入,这个解决方案将为您提供很好的帮助。