Python 如何 strftime 不同语言环境中的日期对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18593661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:10:58  来源:igfitidea点击:

How do I strftime a date object in a different locale?

pythonlocalestrftimesetlocale

提问by MagerValp

I have a date object in python and I need to generate a time stamp in the C locale for a legacy system, using the %a (weekday) and %b (month) codes. However I do not wish to change the application's locale, since other parts need to respect the user's current locale. Is there a way to call strftime() with a certain locale?

我在 python 中有一个日期对象,我需要使用 %a(工作日)和 %b(月)代码在 C 语言环境中为遗留系统生成时间戳。但是我不希望更改应用程序的语言环境,因为其他部分需要尊重用户的当前语言环境。有没有办法用特定的语言环境调用 strftime() ?

采纳答案by Daniel

The example given by Rob is great, but isn't threadsafe. Here's a version that works with threads:

Rob 给出的例子很好,但不是线程安全的。这是一个适用于线程的版本:

import locale
import threading

from datetime import datetime
from contextlib import contextmanager


LOCALE_LOCK = threading.Lock()

@contextmanager
def setlocale(name):
    with LOCALE_LOCK:
        saved = locale.setlocale(locale.LC_ALL)
        try:
            yield locale.setlocale(locale.LC_ALL, name)
        finally:
            locale.setlocale(locale.LC_ALL, saved)

# Let's set a non-US locale
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')

# Example to write a formatted English date
with setlocale('C'):
    print(datetime.now().strftime('%a, %b')) # e.g. => "Thu, Jun"

# Example to read a formatted English date
with setlocale('C'):
    mydate = datetime.strptime('Thu, Jun', '%a, %b')

It creates a threadsafe context manager using a global lock and allows you to have multiple threads running locale-dependent code by using the LOCALE_LOCK. It also handles exceptions from the yield statement to ensure the original locale is always restored.

它使用全局锁创建线程安全上下文管理器,并允许您使用 LOCALE_LOCK 让多个线程运行与语言环境相关的代码。它还处理来自 yield 语句的异常,以确保始终恢复原始语言环境。

回答by Philippe T.

take a look to the pytz package

看一下pytz 包

you can use like this

你可以这样使用

import pytz
UTC = pytz.timezone('UTC') # utc
fr = pytz.timezone('Europe/Paris') #your local
from datetime import datetime
date = datetime.now(fr)
dateUTC = date.astimezone(UTC)

strftime will render in the timezone specified

strftime 将在指定的时区呈现

for have month name in the locale use calendarfor example :

对于在语言环境中使用日历的月份名称,例如:

import calendar
print calendar.month_name[dateUTC.month] #will print in the locale

inspect more deeply calendar for having more information

检查更深入的日历以获取更多信息

回答by Rob?

No, there is no way to call strftime()with a specific locale.

不,无法strftime()使用特定语言环境进行调用。

Assuming that your app is not multi-threaded, save and restore the existing locale, and set your locale to 'C'when you invoke strftime.

假设您的应用程序不是多线程的,请保存并恢复现有的语言环境,并'C'在调用strftime.

#! /usr/bin/python3
import time
import locale


def get_c_locale_abbrev():
  lc = locale.setlocale(locale.LC_TIME)
  try:
    locale.setlocale(locale.LC_TIME, "C")
    return time.strftime("%a-%b")
  finally:
    locale.setlocale(locale.LC_TIME, lc)

# Let's suppose that we're french
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')

# Should print french, english, then french
print(time.strftime('%a-%b'))
print(get_c_locale_abbrev())
print(time.strftime('%a-%b'))


If you prefer with:to try:-finally:, you could whip up a context manager:

如果你喜欢with:try:- finally:,你可以掀起一个上下文管理器:

#! /usr/bin/python3
import time
import locale
import contextlib

@contextlib.contextmanager
def setlocale(*args, **kw):
  saved = locale.setlocale(locale.LC_ALL)
  yield locale.setlocale(*args, **kw)
  locale.setlocale(locale.LC_ALL, saved)

def get_c_locale_abbrev():
  with setlocale(locale.LC_TIME, "C"):
    return time.strftime("%a-%b")

# Let's suppose that we're french
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')

# Should print french, english, then french
print(time.strftime('%a-%b'))
print(get_c_locale_abbrev())
print(time.strftime('%a-%b'))