简体   繁体   English

使用Python从用户那里获取时间数据以安排未来10分钟的任务

[英]Getting time data from user using Python to schedule task 10 mins into the future

Im trying to get Python to check if time given is at least 10 minutes into the future. 我试图让Python检查给定的时间是否至少是未来10分钟。 when entering data, I am always getting back the 'else' clause; 输入数据时,我总是会返回“ else”子句; The scheduled time must be at least 10 minutes from now
Here is the code im working with so far: 到目前为止,这是我正在使用的代码:

while len(schedTime) == 0:
        schedTime = raw_input('Scheduled Time (hh:mm): ')

        schedHr = schedTime.split(':')[0]
        schedMi = schedTime.split(':')[1]

        try:
            testTime = int(schedHr)
            testTime = int(schedMi)
        except:
            print 'The scheduled time must be in the format hh:mm)'
            schedTime = ''
            continue

        if int(self.hr) <= int(schedHr) and int(self.mi) + 10 <= int(schedMi):
            pass
        else:
            print 'The scheduled time must be at least 10 minutes from now'
            schedTime = ''

Second part of the script a little(lot) further down: 脚本的第二部分稍微向下一些:

 ### Get the current time
    now  = datetime.datetime.now()
    yrF = now.strftime('%Y')
    moF = now.strftime('%m')
    dyF = now.strftime('%d')

    then = now + datetime.timedelta(minutes=10)
    self.hr = then.strftime('%H')
    self.mi = then.strftime('%M')

Consider using the datetime library: http://docs.python.org/library/datetime.html . 考虑使用datetime库: http : //docs.python.org/library/datetime.html You can create a two timedelta objects, one for the current moment and one for the scheduled time. 您可以创建两个timedelta对象,一个用于当前时刻,一个用于计划时间。 Using a substraction, you can see if the scheduled time is less than 10 minutes away from now. 使用减法,您可以查看到现在的计划时间是否少于10分钟。

Eg 例如

t1 = datetime.timedelta(hours=self.hr, minutes=self.mi)
t2 = datetime.timedelta(hours=schedHr, minutes=schedMi)
t3 = t2 - t1
if t3.seconds < 600:
    print 'The scheduled time must be at least 10 minutes from now'
    schedTime = ''

There are several problems with this script, the most glaring is that you are not considering hour roll over. 这个脚本有几个问题,最明显的是您没有考虑小时数转换。 For example, if time is 5pm and someone puts in 6pm, clause: 例如,如果时间是下午5点,有人放了6点,则子句:

int(self.hr) <= int(schedHr) and int(self.mi) + 10 <= int(schedMi)

will be false since self.mi is 00 and schedMi is 00. 由于self.mi为00而schedMi为00,因此将为false。

you should use a timedelta object. 您应该使用一个timedelta对象。 For instance: 例如:

tdelta = datetime.timedelta(minutes=10)
#read in user_time from command line
current_time = datetime.datetime.now()
if user_time < current_time + tdelta:
    print "Something is wrong here buddy" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python日程安排创建作业任务,使其仅在白天从6AM到10PM的时间内每3分钟运行一次 - how to create a job task to run every 3 minutes during only at day time from 6AM to 10PM using python schedule 使用Python从xml创建计划任务 - Creating Schedule Task from xml using Python Python-每天使用Schedule从数据库中填充数据 - Python - populate data from database at the same time everyday using Schedule 如何使用Python安排一个不受系统时间变化影响的周期性任务 - How to schedule a periodic task that is immune to system time change using Python Python / Django:每X持续时间(秒/分钟/小时?)后实时调度任务 - Python/Django: Schedule task in realtime after every X duration (secs/mins/hours?) 使用计划执行任务直到某个时间? - Using schedule to do a task until a certain time? 使用Python从AWS Lambda安排ECS任务 - Schedule ECS Task from AWS Lambda in Python 在 python 中使用 time.sleep(secs) 来安排调用函数的周期性任务有多好/坏? - How good/bad is using time.sleep(secs) in python to schedule a periodic task of calling a function? 如何从未来的Python中检索任务? - How to retrieve a task from a future in python? python - 从x小时y分钟格式将时间转换为z分钟 - python - Convert time into z mins from x hours y mins format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM