为什么Python在语言环境模块中将德语和西班牙语(以及其他?)时间格式字符串存储为%T?

Sta*_*nch 8 python time locale string-formatting

我正在为将在多个语言环境中使用的程序编写测试.在用德语运行测试时,我收到了错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/local/lib/python2.7/_strptime.py", line 317, in _strptime
    (bad_directive, format))
ValueError: 'T' is a bad directive in format '%T'
Run Code Online (Sandbox Code Playgroud)

深入研究,我发现使用locale.nl_langinfo(locale.T_FMT)德语或西班牙语(以及可能的其他语言)生成格式字符串'%T'.这在time模块中无法识别.

对文档localepython.org没有有关返回提什么'%T'.'%T'我可以在任何地方找到的唯一引用是对单独的StackOverflow问题的响应.从那篇文章和背景来看,我假设'%T'是简写'%H:%M:%S'.

我的问题是,我如何处理locale将返回'%T'其格式字符串的语言环境,而不做类似的事情

if fmt_str == '%T':
    fmt_str = '%H:%M:%S'
Run Code Online (Sandbox Code Playgroud)

处理这些案件?

Sta*_*nch 1

这是一个完全不能令人满意的答案,但无论如何这就是答案:

之所以locale不能time.strptime很好地协同工作是因为locale格式不是为time.strptime. 它们是为time.strftime生成必要的日期/时间格式而编写的,而不是为了解析它们。

因为time.strptime是独立于平台编写的,所以它不接受locale给出的那么多指令;time.strftime需要能够转换向其抛出的任何内容,因此它接受任何平台定义的指令。

所以,不,没有更简单的方法可以让他们按照我想要的方式进行制作time和合作。locale