我在Python中有一个日期对象,需要为遗留系统生成C语言区域设置下的时间戳,使用%a(星期几)和%b(月份)代码。但是我不想更改应用程序的语言环境,因为其他部分需要尊重用户当前的语言环境。是否有一种方法可以使用特定语言环境调用strftime()?
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')
使用全局锁创建线程安全的上下文管理器,并通过使用LOCALE_LOCK允许您运行具有本地化依赖性代码的多个线程。它还处理yield语句中的异常,以确保始终恢复原始区域设置。
不,没有办法以特定的语言环境调用 strftime()
。
假设您的应用程序不是多线程的,保存并恢复现有的语言环境,并在调用 strftime
时将语言环境设置为 'C'
。
#! /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'))
如果您更喜欢使用 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'))
locale.setlocale()
会影响整个程序。try-finally
块或 with
块设置并恢复区域设置,以便不干扰程序的其余部分。 - Robᵩ看一下pytz包
你可以像这样使用
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将在指定的时区进行渲染
如果要使用本地语言环境中的月份名称,请使用calendar,例如:
import calendar
print calendar.month_name[dateUTC.month] #will print in the locale
深入检查日历以获取更多信息
calendar.month_name
是一个数组,表示当前语言环境下的每个月份。 - Dima Tisnek
saved = locale.setlocale(...
上面那个是笔误吗? - vonPetrushevsetlocale
抛出异常,它是否仍然会yield
?这不会影响with
结构吗?(例如提前引发StopIteration
等)。 (顺便说一句 - 很酷的答案,其中有很多内容。我认为“区域设置是每个进程”的问题不容易解决。) - Tomasz Gandor