def strftime(date, date_format):
 def strip_zeros(x):
  return (x.lstrip(u'0') or u'0')
 c89_directives = u'aAbBcdfHIjmMpSUwWxXyYzZ%'
 format_options = u'%[-]?.'
 candidates = re.findall(format_options, date_format)
 template = re.sub(format_options, u'%s', date_format)
 (lang_code, enc) = locale.getlocale(locale.LC_TIME)
 formatted_candidates = []
 for candidate in candidates:
  if (candidate[(-1)] in c89_directives):
   if (len(candidate) == 3):
    candidate = u'%{}'.format(candidate[(-1)])
    conversion = strip_zeros
   else:
    conversion = None
   if isinstance(date, SafeDatetime):
    formatted = date.strftime(candidate, safe=False)
   else:
    formatted = date.strftime(candidate)
   if ((not six.PY3) and (enc is not None)):
    formatted = formatted.decode(enc)
   if conversion:
    formatted = conversion(formatted)
  else:
   formatted = candidate
  formatted_candidates.append(formatted)
 return (template % tuple(formatted_candidates))

def strftime ( date , date_format ) : def strip_zeros ( x ) : return ( x . lstrip ( u'0' ) or u'0' ) c89_directives = u'aAbBcdfHIjmMpSUwWxXyYzZ%' format_options = u'%[-]?.' candidates = re . findall ( format_options , date_format ) template = re . sub ( format_options , u'%s' , date_format ) ( lang_code , enc ) = locale . getlocale ( locale . LC_TIME ) formatted_candidates = [ ] for candidate in candidates : if ( candidate [ ( - 1 ) ] in c89_directives ) : if ( len ( candidate ) == 3 ) : candidate = u'%{}' . format ( candidate [ ( - 1 ) ] ) conversion = strip_zeros else : conversion = None if isinstance ( date , SafeDatetime ) : formatted = date . strftime ( candidate , safe = False ) else : formatted = date . strftime ( candidate ) if ( ( not six . PY3 ) and ( enc is not None ) ) : formatted = formatted . decode ( enc ) if conversion : formatted = conversion ( formatted ) else : formatted = candidate formatted_candidates . append ( formatted ) return ( template % tuple ( formatted_candidates ) )
a version of strftime that returns unicode strings and tries to handle dates before 1900 .