strftime replacement which supports Unicode format strings?

  • Thread starter Dennis Benzinger
  • Start date
D

Dennis Benzinger

Is there a library with a strftime replacement which supports Unicode
format strings?


Bye,
Dennis
 
J

John Machin

Dennis said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top