Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # $Id: datediff,v 1.3 2013/09/14 14:37:31 elias Exp $
- from datetime import date
- from re import match
- from sys import argv
- opt_year_real = False
- opt_short_output = False
- def twodigityear(y):
- if y > 100 or opt_year_real:
- return y
- if y > 70:
- y += 1900
- else:
- y += 2000
- return y
- def s2date(s):
- mo = match(r'(\d{,2})\.(\d{,2})\.(\d+)$', s)
- if mo:
- year = twodigityear(int(mo.group(3)))
- return date(year, int(mo.group(2)), int(mo.group(1)))
- mo = match(r'(\d+)\-(\d{,2})\-(\d{,2})$', s)
- if mo:
- year = twodigityear(int(mo.group(1)))
- return date(year, int(mo.group(2)), int(mo.group(3)))
- raise RuntimeError, 'Unsupported date format'
- argc = len(argv)
- if argc == 2:
- d1 = date.today()
- d2 = s2date(argv[1])
- elif argc == 3:
- d1 = s2date(argv[1])
- d2 = s2date(argv[2])
- else:
- raise RuntimeError, 'Improper parameters'
- dd = d2 - d1
- days = dd.days
- if opt_short_output:
- print(days)
- else:
- direction = 'after'
- ds1 = d1.isoformat()
- ds2 = d2.isoformat()
- if days < 0:
- direction = 'before'
- days = abs(days)
- if days > 1:
- plural = 'days'
- else:
- plural = 'day'
- print('%s is %d %s %s %s' % (ds2, days, plural, direction, ds1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement