如何在python中显示区域设置敏感时间格式而无需秒
问题描述
我可以使用 strftime('%X')
输出区域敏感的时间格式,但这总是包括秒.我如何在没有秒的情况下显示这种时间格式?
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秒
我能想到的唯一方法是尝试解析 locale.nl_langinfo(locale.T_FMT)
的输出并去除秒位,但这会带来自己的诡计.
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
<小时>
解决方案:
(基于pixelbeat的回答.)
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)
用法:
>>> 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分
解决方案
我会建议对返回的 T_FMT 进行特殊封装,因为实际上没有那么多需要考虑:
I would suggest special casing the returned T_FMT as there aren't that many to consider really:
$ for l in $(locale -a | grep utf8); do locale | cut -d= -f1 | LANG=$l xargs locale -kc | grep ^t_fmt=; done | sort -u
t_fmt="%H:%M:%S"
t_fmt="%H.%M.%S"
t_fmt="%H시 %M분 %S초"
t_fmt="ཆུ་ཚོད%Hཀསར་མ%Mཀསར་ཆ%S"
t_fmt="%H时%M分%S秒"
t_fmt="%H时%M分%S秒 %Z"
t_fmt="%H時%M分%S秒"
t_fmt="%I.%M.%S %p"
t_fmt="%I:%M:%S %Z"
t_fmt="%I:%M:%S %Z"
t_fmt="%I.%M.%S. %Z"
t_fmt="%I時%M分%S秒 %Z"
t_fmt="kl. %H.%M %z"
t_fmt="%k,%M,%S"
t_fmt="%k:%M:%S"
t_fmt="%l:%M:%S"
t_fmt="%OH:%OM:%OS"
t_fmt="%OI:%OM:%OS %p"
t_fmt="%p%I.%M.%S %Z"
t_fmt="%r"
t_fmt="%t"
t_fmt="%T"
t_fmt="%Z %I:%M:%S "
相关文章