title: Python, date validation
body: <p>I'm trying to think of a way to accomplish this in the best pythonic way possible.  Right now the only method I can think of is to brute force it.</p>
  
  <p>User inputs a date (via command line) in one of the following manners (ex.  ./mypy.py date='20110909.00 23' )</p>
  
  <pre><code>date='20110909'
  date='20110909.00 23'
  date='20110909.00 20110909.23'
  </code></pre>
  
  <p>All three examples should have the same result, it doesn't matter if it populates a list (which I can sort) such as </p>
  
  <pre><code>['20110909.00', '20110909.23]
  </code></pre>
  
  <p>or even two sorted separate variables, but in all cases it's YYYYMMDD.HH, and needs to make sure it is indeed a date and not text.</p>
  
  <p>Any ideas?</p>
  
  <p>Thank you.</p>
  
  <p>+++++ EDIT +++++
  After plugging away at this, I'm thinking I needed to do a lot of date checking/manipulating first.  Which all seems to be working just great.  Except at the very end I run the list through the date validation and it fails every time - even when it should be passing.</p>
  
  <p>(I launch it with)
      ./test.py date='20110909.00 23'</p>
  
  <p>(or any variation of date - i.e. date='20 22' or date='20110909' or date='20110909.00 23' etc.)</p>
  
  <pre><code>import sys, re, time, datetime
  
  now = datetime.datetime.now()
  tempdate=[]
  strfirstdate=None
  strtempdate=None
  
  temparg2 = sys.argv
  del temparg2[0]
  tempdate = temparg2[0].replace('date=','')
  date = tempdate.split(' ');
  
  tempdate=[]
  date.sort(key=len, reverse=True)
  result = None
  
  # If no date is passed then create list according to [YYMMDD.HH, YYMMDD.HH]
  if date[0] == 'None':
      tempdate.extend([now.strftime('%Y%m%d.00'), now.strftime('%Y%m%d.%H')])
  
  
  # If length of date list is 1 than see if it is YYMMDD only or HH only, and create list according to [YYMMDD.HH, YYMMDD.HH]
  elif len(date) == 1:
      if len(date[0]) == 8:
          tempdate.extend([ date[0] + '.00', date[0] + '.23'])
      elif len(date[0]) == 2:
          tempdate.extend([now.strftime('%Y%m%d') + '.' + date[0], now.strftime('%Y%m%d') + '.' + date[0]])
      else:
          tempdate.extend([date[0], date[0]])
  
  
  # iterate through list, see if value is YYMMDD only or HH only or YYYYMMDD.HH, and create list accoring to [YYYYMMDD.HH, YYYYMMDD.HH] - maximum of 2 values
  else:
      for _ in range(2):
          if len(date[_]) == 8:
              strfirstdate = date[0]
              tempdate.append([ date[_] + '.00'])
          elif len(date[_]) == 2:
              if _ == 0:  # both values passed could be hours only
                  tempdate.append(now.strftime('%Y%m%d') + '.' + date[_])
              else:  # we must be at the 2nd value passed.
                  if strfirstdate == None:
                      tempdate.append(now.strftime('%Y%m%d') + '.' + date[_])
                  else:
                      tempdate.append(strfirstdate + '.' + date [_])
          else:
              strfirstdate = date[0][:8]
              tempdate.append(date[_])
  
  tempdate.sort()
  
  
  for s in tempdate:
      try:
          result = datetime.datetime.strptime(s, '%Y%m%d.%H')
      except:
          pass
  
  if result is None:
      print 'Malformed date.'
  else:
      print 'Date is fine.'
  
  print tempdate
  </code></pre>
  
  <p>++++ Edit 2 ++++
  If I remove the bottom part (after tempdate.sort()) and replace it with this.</p>
  
  <pre><code>strfirstdate = re.compile(r'([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+\.[0-9][0-9])')
  for s in tempdate:
      if re.match(strfirstdate, s):
          result = "validated"
      else:
          print "#####################"
          print "#####################"
          print "##  error in date  ##"
          print "#####################"
          print "#####################"
          exit
  </code></pre>
  
  <p>It will validate appropriately.</p>
  
  <p>This entire method just doesn't seem to be very pythonic.</p>