[PATCH] date representation: use ISO8601 rather than a specific locale

Thomas De Schampheleire patrickdepinguin at gmail.com
Fri Mar 6 11:29:16 EST 2015


[..]
>>>>>
>>>>> diff --git a/kallithea/lib/helpers.py b/kallithea/lib/helpers.py
>>>>> --- a/kallithea/lib/helpers.py
>>>>> +++ b/kallithea/lib/helpers.py
>>>>> @@ -469,7 +469,7 @@
>>>>>
>>>>>    def fmt_date(date):
>>>>>        if date:
>>>>> -        _fmt = u"%a, %d %b %Y %H:%M:%S".encode('utf8')
>>>
>>>
>>> Heh - making a const unicode string with all ascii chars and then
>>> encoding
>>> it to utf8 ... that seems a bit convoluted ;-)
>>>
>>> It does however raise the question of how this function was supposed to
>>> handle locales where the name of the day or month had unicode chars.
>>>
>>> With your changes it will always only be ascii chars and it would perhaps
>>> be
>>> more clear to use unicode(...) instead of .decode.
>>>
>>>>> +        _fmt = u"%Y-%m-%d %H:%M:%S".encode('utf8')
>>>>>            return date.strftime(_fmt).decode('utf8')
>>
>> I'm not very familiar with unicode and didn't really understand that
>> part of the original code, to be honest.
>>
>> Do you mean this:
>>
>> _fmt = "%Y-%m-%d %H:%M:%S"
>> return unicode(date.strftime(_fmt))
>>
>> and thus finally simply:
>>
>> return unicode(date.strftime(DATETIME_FORMAT))
>
>
> Yes, i did.
>
> That will however apparently also put an "invisible" constraint on
> DATETIME_FORMAT; it must be unicode-able without specifying encoding.
>
> I think the big unknown for me is whether datetime.datatime strftime ever
> can be localized and return unicode strings or if it always will be in
> english and thus be pure ascii that trivially converts to unicode. Perhaps
> also whether it will return a unicode string if it is given a unicode format
> string.

The %c format key will return a localized date representation, so it
can be unicode.

Manual says:
'strftime() returns a locale depedent byte string; the result may be
converted to unicode by doing
strftime(<myformat>).decode(locale.getlocale()[1]).'
https://docs.python.org/2/library/time.html#time.strftime

However, on my PC, locale.getlocale()[1] returns None and decode
doesn't accept that.

I guess
return unicode(date.strftime(DATETIME_FORMAT))
should still be fine? Or should it be changed to something else?


More information about the kallithea-general mailing list