Has anyone managed to use locale on OS X to derive a default local date and time format ? (I need to parse simple dates and times in a variety of locales).
For the moment, I am resorting to 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()
which returns something like:
'%d/%m/%Y', '%H:%M:%S'
Why not use the
python-dateutilpackage instead?It’s
parsefunction can handle most formats without having to configure the format:Alternatively, you could try and translate the ICU format strings available via the
defaults readcommand:These are the locale settings configured via the
Language & Textconfiguration panel.