简体   繁体   中英

Python Check if a Minute has passed

Im running part of a script just once per minute and came up with this:

def minutePassed(oldminute):
    currentminute = time.gmtime()[4]

    if ((currentminute - oldminute) >= 1) or (oldminute == 59 and currentminute >= 0):
        return True
    else:
        return False

Problem here is that if the minute is 59 it runs everytime until its past that time - its not much of a bother performance wise for me. But I still dont like it happening!

I thought of something like this now:

def minutePassed(oldminute):
    currentminute = time.gmtime()[4]

    if ((currentminute - oldminute) >= 1) or (oldminute == 59 and currentminute >= 0 and ran == false):
        ran = True
        return True
    else:
        return False

Then on another part of the script I turn ran false again when the minute is != 59 and the variable isnt already false - but that seems crude?

On another note - is there any other way to check if a minute has passed? Maybe im making things complicated ...

Edit: Maybe I was not clear enough:

  1. Run only ONCE per minute.
  2. Execution time varies by many seconds but takes less then 30s.

Im looking at timedelta now.

Don't work with minutes like that; if the time is, for example, 00:00:59, your code will believe a minute has passed the very next second.

Instead, use something like time.time() , which returns seconds passed since the epoch:

def minute_passed(oldepoch):
    return time.time() - oldepoch >= 60

That can still be off by almost a second for the same reasons, but that's probably more acceptable.

You can use seconds since epoch to get the time in seconds so you don't have to worry about minutes wrapping around:

import time
oldtime = time.time()
# check
if time.time() - oldtime > 59:
    print "it's been a minute"

Use time.sleep :

from time import sleep

while True:
    # do something
    sleep(60)

I think you'll find it much easier to use the time() function in the time module, which returns the number of seconds elapsed since the 'epoch'.

import time

oldtime = time.time()

def minutePassed(oldtime):
    currenttime = time.time()
    if currenttime - oldtime > 60 and ran == False:
        ran = True
        return True
    else:
        return False

I had not much success with timedelta so I went with my former crude idea:

def minutePassed(oldminute):
    currentminute = time.gmtime()[4]

    if ((currentminute - oldminute) >= 1) or (oldminute == 59 and currentminute == 0):
        return True
    else:
        return False

Then in the script:

while True:
    dosomething()

    if minutePassed(time.gmtime(lastrun)[4]):
        domore
        lastrun = time.time()

This works perfectly fine for me now - only runs once a Minute - sleeping or anything is a bad choice for me here since the execution time on each loop is unreliable.

I had the exact same problem and I think I found a solution:

import time


oldtime = time.time()
print oldtime

while oldtime + 60 != time.time():

    if oldtime + 3 == time.time():
        print "60 secs passed"
        print time.time()
        break

You can delete the print statements, I had them there just to see if it works, hope it's what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM