How to display locale sensitive time format without seconds in python

Posted by Tim Kersten on Stack Overflow See other posts from Stack Overflow or by Tim Kersten
Published on 2010-03-24T12:50:06Z Indexed on 2010/03/24 12:53 UTC
Read the original article Hit count: 351

Filed under:
|
|
|

I can output a locale sensitive time format using strftime('%X'), but this always includes seconds. How might I display this time format without seconds?

>>> import locale
>>> import datetime
>>> locale.setlocale(locale.LC_ALL, 'en_IE.utf-8')
'en_IE.utf-8'
>>> print datetime.datetime.now().strftime('%X')
12:22:43
>>> locale.setlocale(locale.LC_ALL, 'zh_TW.utf-8')
'zh_TW.utf-8'
>>> print datetime.datetime.now().strftime('%X')
12?22?58?

The only way I can think of doing this is attempting to parse the output of locale.nl_langinfo(locale.T_FMT) and strip out the seconds bit, but that brings it's own trickery.

>>> print locale.nl_langinfo(locale.T_FMT)
%H?%M?%S?
>>> locale.setlocale(locale.LC_ALL, 'en_IE.utf-8')
'en_IE.utf-8'
>>> print locale.nl_langinfo(locale.T_FMT)
%T

© Stack Overflow or respective owner

Related posts about python

Related posts about locale