【问题标题】:How can I check if the current locale uses AM/PM or 24-hour time?如何检查当前区域设置是使用 AM/PM 还是 24 小时制?
【发布时间】:2017-01-26 02:15:01
【问题描述】:

我正在尝试更新一个字符串,该字符串应根据当前语言环境使用 AM/PM 还是 24 小时来不同地显示时间

如果可以确定语言环境,我可以根据语言环境使用strftime("%I:%M %p")strftime("%H:%M") 更新时间字符串

如何以编程方式确定当前区域设置是使用上午/下午还是 24 小时制?

或者有没有更好的方法来达到相同的目标(根据我的软件运行的区域而显示不同的时间)?

感谢您的帮助和亲切的问候, 托德

【问题讨论】:

  • 鉴于the strftime/strptime docsen_US 的“区域设置适当时间”以24 小时时钟呈现(%X21:30:00 区域设置下生成21:30:00),我怀疑Python语言环境信息指示当地标准是 12 小时制还是 24 小时制。美国几乎 100% 是 12 小时制,但语言环境时间表示为 24 小时。

标签: python python-3.x datetime locale


【解决方案1】:

使用带有%p 参数的time.strftime() 函数将为当前语言环境中的AM/PM 提供等效项,而对于不使用AM/PM 的语言环境,则返回一个空字符串。我通过以下方式使用它:

time_format_string = "%H:%M"  # Ex: Sweden
if time.strftime("%p"):  # Checking if the string contains something
    time_format_string = "%I:%M %p"  # Ex: US
time_of_day_string = time.strftime(time_format_string)
print(time_of_day_string)

我已在两种语言环境中尝试过此方法(一种有 AM/PM,另一种没有 AM/PM)

参考: https://docs.python.org/3/library/time.html#time.strftime

【讨论】:

    【解决方案2】:

    免责声明 1:仅在 GNU/Linux 上使用 glibc 进行了测试。在 Windows、macOS、具有非 GNU libc 的 Linux 和其他操作系统上,您的里程可能会有所不同。

    免责声明 2:我在 Alpine Linux 上使用 musl libc 进行了测试,但它不起作用。使用 Docker 时您可能会遇到这种情况,因为 Alpine 在制作极简容器镜像方面非常受欢迎。


    tl;博士

    使用这个:

    import locale
    
    locale.setlocale(locale.LC_TIME, locale.getdefaultlocale(("LC_TIME",)))
    

    问题

    @ShadowRangercomment 引起了我的注意:

    鉴于the strftime/strptime docsen_US 的“区域设置适当时间”以24 小时时钟呈现(%Xen_US 区域设置下产生21:30:00),我怀疑Python 区域设置信息是否表明当地标准是 12 或 24 小时制。美国几乎 100% 是 12 小时制,但语言环境时间表示为 24 小时。

    让我们试着找出这里发生了什么。

    locale.getdefaultlocale() 函数的文档给了我们一个提示:

    根据 POSIX,未调用 setlocale(LC_ALL, '') 的程序使用可移植的 'C' 语言环境运行。

    看起来CPython默认使用'C'语言环境,它的时间表示似乎是24小时:

    $ LC_TIME=C date
    Tue Oct 27 13:06:54 UTC 2020
    $ LC_TIME=en_US.UTF-8 date
    Tue Oct 27 01:06:54 PM UTC 2020
    $ TZ=UTC LC_TIME=de_DE.UTF-8 date
    Di 27. Okt 13:06:54 UTC 2020
    

    让我们确认一下:

    $ LC_TIME=en_US.UTF-8 python3
    >>> import locale
    >>> import time
    >>> locale.getlocale(locale.LC_TIME)
    (None, None)
    >>> time.strftime("%p")
    'PM'
    >>> time.strftime("%X")
    '13:03:34'
    >>> locale.setlocale(locale.LC_TIME, 'C')
    'C'
    >>> locale.getlocale(locale.LC_TIME)
    (None, None)
    

    这里我们可以看到两件事:

    1. 取消默认区域设置与将其设置为'C' 相同。
    2. 设置 LC_TIME 环境变量而不在 Python 中显式使用它不会产生任何效果。

    我想知道如果%X 不使用它,为什么会设置%p? ?️

    解决方案

    为了让程序尊重我们的区域设置偏好,我们需要调用locale.setlocale() 并提供LC_TIME

    $ LC_TIME=en_US.UTF-8 python3
    >>> import locale
    >>> import time
    >>> locale.setlocale(locale.LC_TIME, locale.getdefaultlocale(('LC_TIME',)))
    'en_US.UTF-8'
    >>> time.strftime("%p")
    'PM'
    >>> time.strftime("%X")
    '01:03:59 PM'
    
    $ LC_TIME=de_DE.UTF-8 python3
    >>> import locale
    >>> import time
    >>> locale.setlocale(locale.LC_TIME, locale.getdefaultlocale(('LC_TIME',)))
    'de_DE.UTF-8'
    >>> time.strftime("%p")
    ''
    >>> time.strftime("%X")
    '13:06:13'
    

    【讨论】:

      【解决方案3】:

      Tord,这种行为很奇怪,但这适用于 Windows 7。请注意,在设置区域设置之前,时间显示为 24 小时制。设置区域设置后,它会使用 12 小时 AM/PM 设置显示。这是我笔记本电脑上的正确设置。

      Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
      Type "copyright", "credits" or "license()" for more information.
      >>> import locale
      >>> import time
      >>> cur_locale = locale.getlocale() # current locale setting
      >>> cur_locale
      ('English_United States', '1252')
      >>> time.strftime("%X") # %X - Locale’s appropriate time representation
      '20:01:47'
      >>> locale.setlocale(locale.LC_TIME, cur_locale)
      'English_United States.1252'
      >>> time.strftime("%X") # %X - Locale’s appropriate time representation
      '8:02:11 PM'
      >>> 
      

      【讨论】:

      • 谢谢。好的,我在 Ubuntu 16.04 上得到了类似的行为:AM/PM 仅在使用 setlocale 后添加到输出字符串的末尾
      • 您的回答让我进一步挖掘并写下自己的答案。谢谢你:)
      猜你喜欢
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      相关资源
      最近更新 更多