python - Check if a timestamp string is within a time range -



python - Check if a timestamp string is within a time range -

i need check if timestamp string time range:

tt = '26-12-2012 18:32:51' t1 = datetime.timedelta(0, 28800) #08:00 hrs t2 = datetime.timedelta(0, 68400) #19:00 hrs

to compare need convert timestamp timedelta?, how can that, compare like:

if tt >= t1 , tt <= t2:

thanks..

first, build datetime object datetime.strptime:

>>> t = datetime.datetime.strptime('26-12-2012 18:32:51','%d-%m-%y %h:%m:%s') >>> t datetime.datetime(2012, 12, 26, 18, 32, 51)

now, build sec datetime object represents date portion:

>>> t2 = t.replace(hour=0,minute=0,second=0)

from can datetime.timedelta suitable comparing other timedeltas:

>>> t - t2 datetime.timedelta(0, 66771) >>> dt = t - t2 >>> dt1 = datetime.timedelta(0, 28800) #08:00 hrs >>> dt2 = datetime.timedelta(0, 68400) #08:00 hrs >>> dt > dt1 true >>> dt2 > dt > dt1 true

python

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -