3

有没有人设法在 OS X 上使用语言环境来导出默认的本地日期和时间格式?(我需要在各种语言环境中解析简单的日期和时间)。

目前,我正在求助于 osascript ...</p>

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()

它返回类似:

'%d/%m/%Y', '%H:%M:%S'
4

1 回答 1

1

为什么不使用这个python-dateutil呢?

它的parse功能可以处理大多数格式,而无需配置格式:

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

或者,您可以尝试通过以下命令翻译可用的ICU 格式字符串:defaults read

$ defaults read .GlobalPreferences AppleICUDateFormatStrings
{
    1 = "yyyy/M/d";
}
$ defaults read .GlobalPreferences AppleICUTimeFormatStrings
{
    1 = "kk:mm ";
    2 = "kk:mm:ss ";
    3 = "kk:mm:ss  z";
    4 = "kk:mm:ss  v";
}

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

于 2012-11-05T12:16:52.093 回答