strftime replacement which supports Unicode format strings?

John Machin sjmachin at lexicon.net
Sun Aug 6 19:27:28 EDT 2006


Dennis Benzinger wrote:
> Is there a library with a strftime replacement which supports Unicode
> format strings?
>

Not that I know of.

I presume that you're not happy with workarounds like:

#>>> import datetime
#>>> now = datetime.datetime.now()
#>>> now.strftime('Year=%Y Month=%m Day=%d')
'Year=2006 Month=08 Day=07'
#>>> now.strftime(u'Year=%Y Month=%m Day=%d')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: strftime() argument 1 must be str, not unicode
#>>> u'Year=%s Month=%s Day=%s' %
tuple(now.strftime('%Y/%m/%d').split('/'))
u'Year=2006 Month=08 Day=07'
#>>>

You could generalise that by lashing up a function uniftime(obj,
unifmt) which would work on any obj with a strftime method: parse the
unifmt, build a strfmt with the components you want and some carefully
chosen separator (maybe a control character e.g. FS), call
obj.strftime(strfmt).split(FS) to get your components, then blend the
components into the  unicode constant parts of your unifmt -- easy :-)

Cheers,
John




More information about the Python-list mailing list