classification
Title: strftime strips '%' from unknown format codes on OS X
Type: behavior Stage:
Components: Documentation Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: belopolsky, docs@python, eric.araujo, lemburg, r.david.murray, ronaldoussoren, santoso.wijaya, sverrejoh
Priority: normal Keywords:

Created on 2010-09-09 09:59 by sverrejoh, last changed 2011-05-18 08:22 by santoso.wijaya.

Messages (13)
msg115933 - (view) Author: Sverre Johansen (sverrejoh) Date: 2010-09-09 09:59
There seems to be a platform difference in the way stftime handles 
unknown format codes.

In OSX Python removes the percentage sign from the returned string when the format code is unknown. In Linux it leaves it.

Look at the following example:

This is Python 3.1.2 in Ubuntu:

    Python 3.1.2 (r312:79147, Apr 15 2010, 15:35:48) 
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import datetime
    >>> datetime.datetime.now().strftime("%q")
    '%q'

And this is Python 3.1.2 on Max OSX:

    Python 3.1.2 (r312:79147, Sep  9 2010, 11:11:24) 
    [GCC 4.2.1 (Apple Inc. build 5664)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import datetime
    >>> datetime.datetime.now().strftime("%q")
    'q'

I've gotten the same result in 2.5, 2.6 and 3.1.

FYI, this broke some code that parsed the same string first with strftime, and then used string interpolation (The mac version
deleted all percentage signs before '('):

    >>> datetime.datetime.now().strftime("%Y - %(foobar)s") % {"foobar": "egg"}

This does not work in OSX.
msg115937 - (view) Author: Sverre Johansen (sverrejoh) Date: 2010-09-09 10:35
This is because of differences in GNU and BSD C stdlib; I get the same results using the BSD and GNU versions of `date`.

    $ date +"%q"

What does Python typically do in cases like this?
msg115948 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-09-09 14:23
Python's strftime is a thin wrapper around the system strftime.  This means, for example, that a slightly different set of % codes is supported on windows vs linux.  So, from Python's point of view this is at *most* a doc bug.

That said, I think there have been some rumblings about writing our own strftime, but I don't know if anyone is serious about it.
msg115952 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-09-09 15:47
> I think there have been some rumblings about writing our own strftime

Yes, see issue #3173.

Another related issue is #9650 which discusses how to properly document strftime codes.
msg116132 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-11 23:26
I think supporting different specifiers is one thing, dropping the % character is another.  This is how I understand the bug report.
msg116141 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-09-12 00:54
But if it is a bug, it is a bug in OS/X, not in Python.  Python could potentially fix it for python programs by providing our own strftime, but until someone does that the best we can do is a doc mention of this platform quirk.
msg116143 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-12 01:04
Ah, I understand your first reply now.  Thanks.  Agreed on all your points.
msg136152 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2011-05-17 12:32
I've filed #9451707 about this in Apple's bug tracker.

BTW. I don't think this is a platform bug, neither the manpage nor the unix specification at <http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html> specify what happens when you use an undocumented format specifier.
msg136161 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-05-17 14:06
Right, that's why I said "if it is a bug" :)

FreeBSD has the same behavior, so I'd actually prefer that Apple not "fix" this.  It would be nice if FreeBSD did and Apple adopted it, though, since only dropping the % is the least intuitive approach to handling unknown codes I can think of.  Is Apple tracking FreeBSD development at all these days, or are they a true fork now?
msg136163 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2011-05-17 14:52
I'm not sure if Apple's tracking FreeBSD, but they at the very least heavily borrow code (see <http://opensource.apple.com/source/Libc/Libc-594.9.4/> for libc)
msg136183 - (view) Author: Santoso Wijaya (santoso.wijaya) * Date: 2011-05-17 21:23
`strftime` does not, indeed, seem to define what behaviour it is supposed to do when given non-supported format characters. Under Windows, in fact, it will crash the runtime (see: issue #10762).
msg136208 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2011-05-18 07:53
Santoso Wijaya wrote:
> 
> Santoso Wijaya <santoso.wijaya@gmail.com> added the comment:
> 
> `strftime` does not, indeed, seem to define what behaviour it is supposed to do when given non-supported format characters. Under Windows, in fact, it will crash the runtime (see: issue #10762).

According to POSIX, it is defined to be undefined:

"""
       If a conversion specification does not correspond to any of  the  above,  the
       behavior is undefined.
"""

Of course, crashing the runtime is not a good way to interpret this ;-)
msg136211 - (view) Author: Santoso Wijaya (santoso.wijaya) * Date: 2011-05-18 08:22
Yeah, I meant undefined. I looked that up when writing a patch for issue #10762.

Correction: It *used to* crash on Windows, before the patch was applied.
History
Date User Action Args
2011-05-18 08:22:16santoso.wijayasetmessages: + msg136211
2011-05-18 07:53:21lemburgsetnosy: + lemburg
messages: + msg136208
2011-05-17 21:23:47santoso.wijayasetnosy: + santoso.wijaya
messages: + msg136183
2011-05-17 14:52:47ronaldoussorensetmessages: + msg136163
2011-05-17 14:06:27r.david.murraysetmessages: + msg136161
2011-05-17 12:32:16ronaldoussorensetnosy: + ronaldoussoren
messages: + msg136152
2010-09-12 01:04:08eric.araujosetmessages: + msg116143
2010-09-12 00:54:57r.david.murraysetmessages: + msg116141
2010-09-11 23:26:29eric.araujosetnosy: + eric.araujo
messages: + msg116132
2010-09-09 15:47:47belopolskysetmessages: + msg115952
2010-09-09 14:23:32r.david.murraysetversions: + Python 2.7, Python 3.2
nosy: + docs@python, r.david.murray, belopolsky

messages: + msg115948

assignee: docs@python
components: + Documentation, - Library (Lib)
2010-09-09 10:35:21sverrejohsetmessages: + msg115937
2010-09-09 09:59:03sverrejohcreate