osx上的Python:获取本地日期时间模式

2024-04-26 04:32:57 发布

您现在位置:Python中文网/ 问答频道 /正文

有人设法在OSX上使用locale来派生默认的本地日期和时间格式吗?(我需要分析各种语言环境中的简单日期和时间)。在

目前,我正在求助于osascript

from subprocess import Popen, PIPE

cmd = "osascript -e " + """'
    tell (current date)
        set {its year, its month, its day} to {2001, 2, 3}
        set {its hours, its minutes, its seconds} to {4, 5, 6}
        return its short date string & tab & its time string
    end tell'"""

str_date, err_num = Popen(cmd, shell=True, \
    stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()

str_date, _, str_time = str_date.rpartition('   ')
str_date_pattern = str_date.replace('2001', '%Y').replace('02', '%m').replace('03','%d')
str_time_pattern = str_time.replace('04', '%H').replace('05', '%M').replace('06', '%S').rstrip()

它返回如下内容:

^{pr2}$

Tags: tocmddatestringtime时间replaceits
1条回答
网友
1楼 · 发布于 2024-04-26 04:32:57

为什么不用^{} package代替呢?在

它的^{} function可以处理大多数格式而不必配置格式:

>>> from dateutil.parser import parse
>>> parse('03/02/2001 04:05:06')
datetime.datetime(2001, 3, 2, 4, 5, 6)

或者,您可以尝试通过defaults read命令翻译可用的ICU format strings

^{pr2}$

这些是通过Language & Text配置面板配置的区域设置。在

相关问题 更多 >