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
Solution:
(Based on pixelbeat's answer.)
# -*- coding: utf-8 -*-
import locale
def locale_time(t, show_seconds=False):
if show_seconds:
return t.strftime('%X')
replacement_fmts = [
(u'.%S', u''),
(u':%S', u''),
(u',%S', u''),
(u':%OS', ''),
(u'ཀསར་ཆ%S', u''),
(u' %S초', u''),
(u'%S秒', u''),
(u'%r', '%I:%M %p'),
(u'%t', '%H:%M'),
(u'%T', '%H:%M')
]
enc=locale.getpreferredencoding(do_setlocale=False)
t_fmt = locale.nl_langinfo(locale.T_FMT).decode(enc)
for fmt in replacement_fmts:
new_t_fmt = t_fmt.replace(*fmt)
if new_t_fmt != t_fmt:
return t.strftime(new_t_fmt.encode(enc))
return t.strftime(t_fmt.encode(enc)
Usage:
>>> locale.setlocale(locale.LC_ALL, 'en_IE.utf-8')
'en_IE.utf-8'
>>> print locale_time(t)
15:47
>>> locale.setlocale(locale.LC_ALL, 'zh_TW.utf-8')
'zh_TW.utf-8'
>>> print locale_time(t)
15時47分
This is bad solution. What happens with some new different locale?
Use following:
Now each translator for each languages would provide suitable format for each string, for
en_US
it would be '%I:M %p
', for zh_TW:%H時%M分
This is how usually problems of missing resource in standard localization tools are solved.
Consider ICU, and pyICU.
I would suggest special casing the returned T_FMT as there aren't that many to consider really:
The Babel library provides an easy and reliable solution for this problem. Here is a sample code using Python 2.