classification
Title: strftime strips '%' from unknown format codes on OS X
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: belopolsky, docs@python, eric.araujo, iritkatriel, lemburg, lukasz.langa, miss-islington, r.david.murray, ronaldoussoren, santoso.wijaya, sverrejoh
Priority: normal Keywords: patch

Created on 2010-09-09 09:59 by sverrejoh, last changed 2021-09-10 21:11 by iritkatriel. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28264 merged iritkatriel, 2021-09-09 21:09
PR 28277 merged miss-islington, 2021-09-10 16:27
PR 28278 merged miss-islington, 2021-09-10 16:27
Messages (19)
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.
msg401519 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-09-09 21:10
This issue was created 11 years ago today. Let's celebrate its anniversary by resolving it!
msg401593 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-10 16:27
New changeset e86bcfa58080f152f242c756f625f4015671f168 by Irit Katriel in branch 'main':
bpo-9811: [doc] strftime handling of unsupported format specifiers is platform dependent (GH-28264)
https://github.com/python/cpython/commit/e86bcfa58080f152f242c756f625f4015671f168
msg401610 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-10 19:53
New changeset 897e5aae748180acf8d546d14aeacaf02600fff9 by Miss Islington (bot) in branch '3.10':
bpo-9811: [doc] strftime handling of unsupported format specifiers is platform dependent (GH-28264) (GH-28277)
https://github.com/python/cpython/commit/897e5aae748180acf8d546d14aeacaf02600fff9
msg401611 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-10 19:53
New changeset baeaaecb8a97033bc2d07d51442cc8b1f89d410d by Miss Islington (bot) in branch '3.9':
bpo-9811: [doc] strftime handling of unsupported format specifiers is platform dependent (GH-28264) (GH-28278)
https://github.com/python/cpython/commit/baeaaecb8a97033bc2d07d51442cc8b1f89d410d
msg401613 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-10 20:27
Is there anything else you wanted to do here, Irit?
msg401615 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-09-10 21:11
No, I think we're good. Thanks.
History
Date User Action Args
2021-09-10 21:11:44iritkatrielsetmessages: + msg401615
2021-09-10 20:27:49lukasz.langasetmessages: + msg401613
2021-09-10 19:54:31lukasz.langasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-09-10 19:53:37lukasz.langasetmessages: + msg401611
2021-09-10 19:53:16lukasz.langasetmessages: + msg401610
2021-09-10 16:27:10miss-islingtonsetpull_requests: + pull_request26696
2021-09-10 16:27:06miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26695
2021-09-10 16:27:04lukasz.langasetnosy: + lukasz.langa
messages: + msg401593
2021-09-09 21:14:19iritkatrielsetversions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.1, Python 2.7, Python 3.2
2021-09-09 21:10:23iritkatrielsetmessages: + msg401519
2021-09-09 21:09:38iritkatrielsetkeywords: + patch
nosy: + iritkatriel

pull_requests: + pull_request26684
stage: patch review
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