# datetime_compare_seconds

 

import time

import datetime

 

 

def comp_dates(d1, d2):

    # Date format: %Y-%m-%d %H:%M:%S

    return time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))-\

           time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S"))

 

dateStr = '2007-12-19 21:28:06'

dateStr1 = '2007-12-20 21:28:07'

now = datetime.datetime.now()

diff = comp_dates(dateStr, dateStr1)

diff1 = comp_dates(dateStr, str(now).split('.')[0])

 

one_day = 60*60*24

 

if diff > one_day:

    print "You waited too long!"

else:

    print "You made it in time!"

 

print

 

def comp_dates1(d1, d2):

    if isinstance(d1, str):

        dt1 = datetime.datetime(*time.strptime(d1, "%Y-%m-%d %H:%M:%S")[:6])

    else: dt1 = d1

    if isinstance(d2, str):

        dt2 = datetime.datetime(*time.strptime(d2, "%Y-%m-%d %H:%M:%S")[:6])

    else: dt2 = d2

    delta = dt2-dt1

    return delta.days*86400+delta.seconds

 

dateStr = '2007-12-19 21:28:06'

dateStr1 = '2007-12-20 21:28:07'

 

one_day = datetime.timedelta(days=1)

print one_day.days*86400

one_day = 60*60*24

 

diff2 = comp_dates1(dateStr, dateStr1)

print diff2   

 

dt1 = datetime.datetime(*time.strptime(dateStr, "%Y-%m-%d %H:%M:%S")[:6])

print dt1

dt2 = datetime.datetime(*time.strptime(dateStr1, "%Y-%m-%d %H:%M:%S")[:6])

print dt2

 

diff3 = comp_dates1(dt1, dt2)

print diff3

 

dt3 = datetime.datetime.now()

 

print dt3

print comp_dates1(dt1, datetime.datetime.now())

 

 

'''

>>> You waited too long!

 

86400

86401

2007-12-19 21:28:06

2007-12-20 21:28:07

86401

2007-12-28 17:23:53.765000

762947

>>>

'''