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

Mads Kiilerich mads at kiilerich.com
Fri Mar 6 11:35:45 EST 2015


On 03/06/2015 05:29 PM, Thomas De Schampheleire wrote:
> [..]
>>>>>> 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?

Hmm ...

I think I will suggest just keeping the format string hardcoded so it is 
"obvious" what is going on. I will tweak your patch as
         return date.strftime("%Y-%m-%d %H:%M:%S").decode('utf8')

DATETIME_FORMAT and friends have apparently no purpose and should just 
be removed.

/Mads


More information about the kallithea-general mailing list