Sunday 15 April 2012

Find if 24 hrs have passed between datetimes - Python -



Find if 24 hrs have passed between datetimes - Python -

i have next method:

# last_updated datetime() object, representing lastly time programme ran def time_diff(last_updated): day_period = last_updated.replace(day=last_updated.day+1, hour=1, minute=0, second=0, microsecond=0) delta_time = day_period - last_updated hours = delta_time.seconds // 3600 # create sure period of 24hrs have passed before shuffling if hours >= 24: print "hello" else: print "do nothing"

i want find out if 24 hrs have passed since last_updated, how can in python?

if last_updated naive datetime object representing time in utc:

from datetime import datetime, timedelta if (datetime.utcnow() - last_updated) > timedelta(1): # more 24 hours passed

if last_updated local time (naive (timezone-unaware) datetime object):

import time day = 86400 = time.time() = time.mktime(last_updated.timetuple()) if (now - then) > day: # more 24 hours passed

if last_updated ambiguous time e.g., time during end-of-dst transition (once year in many timezones) there fifty-fifty chance mktime() returns wrong result (e.g., off hour).

time.mktime() may fail if c time library doesn't utilize historical timezone database on given platform and utc offset local timezone different @ last_updated time compared now. may apply more 3rd of timezones in lastly year. linux, os x, recent versions of windows have tz database (i don't know whether old windows versions work such past dates).

beware: might tempting write datetime.now() - last_updated (similar utc case) guaranteed fail on platforms if utc offset different @ last_updated time (it possible in many timezones). mktime()-based solution can utilize tz database @ to the lowest degree on platforms , hence can handle changes in utc offset whatever reason there.

for portability, install tz database. provided pytz module in python. tzlocal can homecoming pytz timezone corresponding local timezone:

from datetime import datetime, timedelta tzlocal import get_localzone # $ pip install tzlocal tz = get_localzone() # local timezone = tz.normalize(tz.localize(last_updated)) # create timezone-aware = datetime.now(tz) # timezone-aware current time in local timezone if (now - then) > timedelta(1): # more 24 hours passed

it works if utc offset different in past. can't (as time.mktime()) prepare ambiguous times (tz.localize() picks is_dst=false time default). tz.normalize() called adjust non-existing times e.g., correspond start-of-dst transition (it should not impact result).

the above code assumes last_updated naive datetime object (no associated timezone info). if last_updated aware datetime object easy convert utc:

from datetime import datetime, timedelta then_in_utc = last_updated.replace(tzinfo=none) - last_updated.utcoffset() if (datetime.utcnow() - then_in_utc) > timedelta(1): # more 24 hours passed

general note: should understand why people recommend work utc time , utilize local time display.

python datetime timezone

No comments:

Post a Comment