如何在python中显示区域设置敏感的时间格式 [英] How to display locale sensitive time format without seconds in python

查看:231
本文介绍了如何在python中显示区域设置敏感的时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 strftime('%X')输出一个语言环境敏感的时间格式,但是这总是包含秒。我怎样才能显示这个时间格式没有秒?

 >>>导入区域设置
>>> 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'
>>>打印datetime.datetime.now()。strftime('%X')
12时22分58秒

我能想到的唯一方法就是试图解析 locale.nl_langinfo(locale.T_FMT)的输出并去掉秒位,但是这是自己的诡计。

 >>> 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的答案。)

 <$ c 
$ locale_time(t,show_seconds = False):
如果show_seconds:
返回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'
>>>打印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,因为没有那么多的考虑:
$ b $ pre $ $ $ $ $ l $ (locale -a | grep utf8);做locale | cut -d = -f1 | LANG = $ l xargs locale -kc | grep ^ t_fmt =;完成| 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分%秒秒
t_fmt = %H时%M分%S秒%Z
t_fmt =%H时%M分%秒秒
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


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分

解决方案

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 "

这篇关于如何在python中显示区域设置敏感的时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆