Skip to content

Instantly share code, notes, and snippets.

@knzm
Created April 25, 2012 12:02
Show Gist options
  • Select an option

  • Save knzm/2489238 to your computer and use it in GitHub Desktop.

Select an option

Save knzm/2489238 to your computer and use it in GitHub Desktop.
$ locale
LANG=ja_JP.UTF-8
LANGUAGE=ja_JP:ja:en_GB:en
LC_CTYPE="ja_JP.UTF-8"
LC_NUMERIC="ja_JP.UTF-8"
LC_TIME="ja_JP.UTF-8"
LC_COLLATE="ja_JP.UTF-8"
LC_MONETARY="ja_JP.UTF-8"
LC_MESSAGES="ja_JP.UTF-8"
LC_PAPER="ja_JP.UTF-8"
LC_NAME="ja_JP.UTF-8"
LC_ADDRESS="ja_JP.UTF-8"
LC_TELEPHONE="ja_JP.UTF-8"
LC_MEASUREMENT="ja_JP.UTF-8"
LC_IDENTIFICATION="ja_JP.UTF-8"
LC_ALL=

locale.setlocale() ありの場合:

$ python
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
'ja_JP.UTF-8'
>>> import time
>>> time.strftime("%b")
' 4\xe6\x9c\x88'
>>> print time.strftime("%b")
 4月
>>> print time.strptime(u" 4月".encode('utf-8'), "%b")
time.struct_time(tm_year=1900, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=91, tm_isdst=-1)
>>> time.strptime("Apr", "%b")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'Apr' does not match format '%b'

locale.setlocale() なしの場合:

$ python
>>> import time
>>> time.strftime("%b")
'Apr'
>>> time.strptime("Apr", "%b")
time.struct_time(tm_year=1900, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=91, tm_isdst=-1)
>>> time.strptime(u" 4月".encode('utf-8'), "%b")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data ' 4\xe6\x9c\x88' does not match format '%b'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment