如何在Python中查看某个时刻是否已经过去

3

我在一个日历中使用以下代码,以告诉我离活动开始还有多长时间。有两部分,一部分显示离工作开始还有多长时间,另一部分显示我需要在什么时候起床上班。

work_alarm = timedelta(hours=2, minutes=30)
a_n = int((we_dt - tct).total_seconds() / 60) # to get minutes remaining from the current time to when work starts
alarm_time = we_dt - work_alarm
up_time = int((alarm_time - tct).total_seconds() / 60) # total minutes until I need to be up
print "Scheduled Work Time: " + str(we_dt.strftime('%H:%M'))
print "You have " + str(we[1]) + " in " + str(int((((we_dt - tct).total_seconds() / 60) / 60))) + " hours and " + str(divmod(a_n, 60)[1]) + " minutes."
print "You need to be up in " + str(int((((alarm_time - tct).total_seconds() / 60) / 60))) + " hours and " + str(divmod(up_time, 60)[1]) + " minutes."

一切都正常工作,直到“需要起床”的时间过去。当事件时间过去时,它将停止报告,因此这并不是真正的问题,但我想设置一种if语句来检查或告诉您是否需要起床的时间已经过去并停止显示它。因为目前情况是,一旦时间过去,它就开始倒数计时:例如,如果还有1分钟要起床,在零点后,它只会开始上涨:1、2、3分钟等等。该数字不会显示为负整数,因此我不知道如何对其进行比较,以便在时间过去后停止显示它。请注意,这个脚本每分钟使用一个apscheduler模块运行。任何帮助都将不胜感激。谢谢。


6
你可能想要去除那些过长的行,并让你的代码更易读一些。 - ThiefMaster
5
顺便说一句,如果你使用字符串格式化的方法,代码将更易读,例如:"You need to be up in {hrs:d} hours and {min:d} minutes".format(hrs=up_time/60, min=up_time%60)。 - askewchan
1
我同意。为了帮助自己和他人更好地理解你的代码,你应该将格式设置和数据准备与计算分开。 - Brian Peterson
如果你的 up_time 变量在接近唤醒时间时减少到0,然后在达到0后开始增加,那么你的代码中存在一个错误或逻辑错误。很难告诉你这个问题可能来自哪里,因为像 tctwe_dt 这样重要的变量在提供的代码中没有定义,也无法从它们的名称轻易猜出其内容。需要更多信息。 - Brian Peterson
由于代码中有几个函数导致了这一点,因此很难向您显示每个变量使用的初始定义区域。生成tct变量的部分约为20行,而这个特定部分则约为80行左右。we_dt变量是从其他在100多行后定义的变量创建的。第一个回答者理解了我的目标,并按照我需要的方式进行了修复。 - TekGiant
2个回答

2

在这里,您只需要一个if语句,如果该值为负数,则不显示任何内容。

您说“数字不会显示为负整数”,但您没有显示数字。您正在显示str(divmod(up_time, 60))[1]。因此,如果up_time为-3,则它将显示为57...但up_time本身仍然是负数。

work_alarm = timedelta(hours=2, minutes=30)
a_n = int((we_dt - tct).total_seconds() / 60) # to get minutes remaining from the current time to when work starts
alarm_time = we_dt - work_alarm
up_time = int((alarm_time - tct).total_seconds() / 60) # total minutes until I need to be up
if up_time >= 0:
    print "Scheduled Work Time: " + str(we_dt.strftime('%H:%M'))
    print "You have " + str(we[1]) + " in " + str(int((((we_dt - tct).total_seconds() / 60) / 60))) + " hours and " + str(divmod(a_n, 60)[1]) + " minutes."
    print "You need to be up in " + str(int((((alarm_time - tct).total_seconds() / 60) / 60))) + " hours and " + str(divmod(up_time, 60)[1]) + " minutes."

或者更好的方法是,告诉调度程序在工作时间过去时不要调用您的代码。 没有看到您的调度程序代码,很难说如何做到这一点 - 也许可以在这里使用 else 调整时间表直到明天早上,或者在第一次构建更复杂的时间表,等等......

0
你可以使用一个函数来返回两个日期时间之间的小时、分钟(和秒数):
def time_between(a, b):
    minutes, seconds = divmod((b-a).total_seconds(), 60)
    hours, minutes = divmod(minutes, 60)
    return hours, minutes, seconds

通过使用这个函数,大部分计算都可以从打印代码中分离出来。同时,使用更具描述性的变量名称将使您的代码更容易理解:
import datetime as DT

def time_between(a, b):
    minutes, seconds = divmod((b-a).total_seconds(), 60)
    hours, minutes = divmod(minutes, 60)
    return hours, minutes, seconds

# current time
tct = DT.datetime.now()
# job datetime (just for example)
we_dt = tct + DT.timedelta(hours=3, minutes=30)

work_alarm = DT.timedelta(hours=2, minutes=30)

# time till work
work_hours, work_minutes, work_seconds = time_between(tct, we_dt)

# time till get up
up_time = we_dt - work_alarm
up_hours, up_minutes, up_seconds = time_between(tct, up_time)
print("""\
Scheduled Work Time: {wt}
You have {we} in {h} + hours and {m} minutes.""".format(
          wt=we_dt.strftime('%H:%M'),
          we='something to do',
          h=work_hours,
          m=work_minutes))

if up_time > tct:
    print('You need to be up in {uh}  hours and {um} minutes.'.format(
        uh=up_hours,
        um=up_minutes))

你的回答非常清晰,而且是一个有趣的概念。这应该会使它变得更加容易。谢谢。 - TekGiant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接