def date_from_str(date_str):
 today = datetime.date.today()
 if (date_str in (u'now', u'today')):
  return today
 if (date_str == u'yesterday'):
  return (today - datetime.timedelta(days=1))
 match = re.match(u'(now|today)(?P<sign>[+-])(?P<time>\\d+)(?P<unit>day|week|month|year)(s)?', date_str)
 if (match is not None):
  sign = match.group(u'sign')
  time = int(match.group(u'time'))
  if (sign == u'-'):
   time = (- time)
  unit = match.group(u'unit')
  if (unit == u'month'):
   unit = u'day'
   time *= 30
  elif (unit == u'year'):
   unit = u'day'
   time *= 365
  unit += u's'
  delta = datetime.timedelta(**{unit: time})
  return (today + delta)
 return datetime.datetime.strptime(date_str, u'%Y%m%d').date()

def date_from_str ( date_str ) : today = datetime . date . today ( ) if ( date_str in ( u'now' , u'today' ) ) : return today if ( date_str == u'yesterday' ) : return ( today - datetime . timedelta ( days = 1 ) ) match = re . match ( u'(now|today)(?P<sign>[+-])(?P<time>\\d+)(?P<unit>day|week|month|year)(s)?' , date_str ) if ( match is not None ) : sign = match . group ( u'sign' ) time = int ( match . group ( u'time' ) ) if ( sign == u'-' ) : time = ( - time ) unit = match . group ( u'unit' ) if ( unit == u'month' ) : unit = u'day' time *= 30 elif ( unit == u'year' ) : unit = u'day' time *= 365 unit += u's' delta = datetime . timedelta ( ** { unit : time } ) return ( today + delta ) return datetime . datetime . strptime ( date_str , u'%Y%m%d' ) . date ( )
return a datetime object from a string in the format yyyymmdd or [+-][0-9](s)? .