Expand/Shrink
Argumenty příkazové řádky:
python date_conversion2.2.py 20.12.2005 20.3.2005 1.3.2008 18.7.77 36.5.2004 1.14.1999 30.2.2006 29.2.2006 29.2.2008
  1   import sys
  2   import re
  3   import calendar
  4   
  5   for date in sys.argv[1:]:
  6       m = re.match( "^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$", date)
  7       if m:
  8           d, m, y = [int( g) for g in m.groups()]
  9           if m < 1 or m > 12:
 10               print "Wrong month '%d' in '%s'" % (m, date)
 11           else:
 12               weekday, maxday = calendar.monthrange( y, m)
 13               if d < 1 or d > maxday:
 14                   print "Wrong day '%d' in '%s'" % (d, date)
 15               else:
 16                   print "%d-%02d-%02d" % (y, m, d)
 17       else:
 18           print "Date in wrong format:", date
stdout:
2005-12-20
2005-03-20
2008-03-01
Date in wrong format: 18.7.77
Wrong day '36' in '36.5.2004'
Wrong month '14' in '1.14.1999'
Wrong day '30' in '30.2.2006'
Wrong day '29' in '29.2.2006'
2008-02-29
Doba běhu: 28.1 ms