#!/usr/bin/env python

"""datediff calculates number of days between 2 dates.

Usage: datediff date1 date2
date1 and date2 must be in format of "%Y%m%d....", hours etc are ignored.

Returns: (date1 - date2) in days
"""

import sys
import re
from datetime import datetime, timedelta

def on_error():
    print __doc__
    sys.exit(1)

try:
    if len(sys.argv) < 3:
        raise RuntimeError('argv')

    pat = r'^(\d{4})([^0-9]*)(\d{1,2})([^0-9]*)(\d{1,2})'

    date1m = re.search(pat, sys.argv[1])
    date2m = re.search(pat, sys.argv[2])

    if not date1m or not date2m:
        raise RuntimeError('match')

    date1 = datetime(*[int(i) for i in date1m.groups()[::2]])
    date2 = datetime(*[int(i) for i in date2m.groups()[::2]])

    dt = date1 - date2
    print dt.days
except Exception, e:
    print "Error detail: ", e
    on_error()
