Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Все-таки обход для дат при неверной локали #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions pytils/dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import datetime
import re

from pytils import numeral
from pytils.utils import check_positive
Expand Down Expand Up @@ -220,14 +221,32 @@ def ru_strftime(format=u"%d.%m.%Y", date=None, inflected=False,
format = format.replace(u'%b', MONTH_NAMES[date.month-1][0])
format = format.replace(u'%B', MONTH_NAMES[date.month-1][month_idx])

# Python 2: strftime's argument must be str
# Python 3: strftime's argument str, not a bitestring
if six.PY2:
# strftime must be str, so encode it to utf8:
s_format = format.encode("utf-8")
s_res = date.strftime(s_format)
# and back to unicode
u_res = s_res.decode("utf-8")
else:
u_res = date.strftime(format)
def run_strftime_on_py_2_or_3(f):
# Python 2: strftime's argument must be str
# Python 3: strftime's argument str, not a bitestring
if six.PY2:
# strftime must be str, so encode it to utf8:
s_format = f.encode("utf-8")
s_res = date.strftime(s_format)
# and back to unicode
return s_res.decode("utf-8")
else:
return date.strftime(f)

need_locale_workaround = False
try:
u_res = run_strftime_on_py_2_or_3(format)
if u_res == u'' and format:
need_locale_workaround = True
except UnicodeError:
need_locale_workaround = True

# workaround for https://github.com/last-partizan/pytils/issues/32
if need_locale_workaround:
u_res = re.sub(
u'\%[c-zC-Z]{1}',
lambda m: run_strftime_on_py_2_or_3(m.group(0)),
format
)

return u_res