classification
Title: Document that datetime.__format__ is datetime.strftime
Type: behavior Stage:
Components: Documentation Versions: Python 3.3, Python 3.2, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: belopolsky, brett.cannon, docs@python, eric.araujo, eric.smith, flox, r.david.murray
Priority: low Keywords: easy

Created on 2010-06-06 03:26 by brett.cannon, last changed 2012-01-16 16:39 by eric.araujo.

Messages (11)
msg107179 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2010-06-06 03:26
Documenting that would help get people using datetime objects with string.format more.
msg107184 - (view) Author: Alexander Belopolsky (belopolsky) (Python committer) Date: 2010-06-06 05:56
I wonder if this is subject to change.  I find it odd that

>>> "{:g}".format(1e3)
'1000'

but one has to use % in

>>> "{:%Y}".format(datetime.now())
'2010'

It is also different from PEP 3101 recommendation:
"""
    An example is the 'datetime' class,
    whose format specifiers might look something like the
    arguments to the strftime() function:

        "Today is: {0:a b d H:M:S Y}".format(datetime.now())
"""
The above, however, should probably be

"Today is: {0:a} {0:b} {0:d} {0:H}:{0:M}:{0:S} {0:Y}".format(datetime.now())
msg107211 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2010-06-06 19:15
I don't find it odd at all that you use datetime-specific formats instead of integral formats to get numbers out of a datetime object; they are totally different things. And one of the reasons for __format__ support is to have DSLs such as the one for datetime objects.

As for pulling out individual objects, that just looks ugly, so I wouldn't advocate pulling out individual values and then formatting them individually in the string itself.
msg107222 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-06-06 20:17
I think Alexander's example is best written as:
"Today is: {0:%a %b %d %H:%M:%S %Y}".format(datetime.now())

I agree with Brett that there's nothing unusual about having type-specific formatting languages, and for datetime strftime is the obvious choice. It's a little unfortunate that % is used, as it's mildly confusing with %-formatting, but not much can be done with that issue.
msg107223 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-06-06 20:19
This documentation should also be added for datetime.time and datetime.date, in addition to datetime.datetime.
msg107228 - (view) Author: Alexander Belopolsky (belopolsky) (Python committer) Date: 2010-06-06 21:06
The problem I have with strftime %-format codes is that according to BSD manual page they have already ran out of English alphabet and still "there is no conversion specification for the phase of the moon." :-)

On a serious note, there are no codes to format TZ offset hours and minutes separately which precludes specifying an important RFC 3339 format.

I think we should take this opportunity to define a comprehensive mini-language for datetime formatting rather than slavishly reuse strftime.

The new mini-language may end up to be a superset of that for strftime, but I would rather not commit to supporting %-codes indefinitely.
msg107233 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-06-06 21:29
Unfortunately I think it's too late to do anything about this. I, for one, have many lines of code in production that use the strftime format specifier for datetime.__format__.

You only option would be to invent a new language that was somehow distinguishable from the strftime specifiers, but I doubt you'd get much support.

I think it's better to extend strftime, and let __format__ benefit from it.
msg107236 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-06-06 21:42
I think this particular wheel has one very good reinvention as the Locale Data Markup Language specification. Example from the Python Babel package:

>>> format_datetime(dt, "yyyyy.MMMM.dd GGG hh:mm a", locale='en')
u'02007.April.01 AD 03:30 PM'

http://unicode.org/reports/tr35/#Date_Format_Patterns
http://babel.edgewall.org/wiki/Documentation/dates.html#pattern-syntax
msg117953 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-10-04 14:49
Alexander closed issue 7789 in favor of this one, which is fine, but I want to respond to Eric's rejection there of including info about datetime in the 'format mini language' section of the docs.  His point was that only the builtin types were documented there, but if the goal is to get people to actually use this facility, it would be helpful if some mention were made there of other stdlib types that support __format__.  Some set of 'see also' links, perhaps in a footnote?
msg117954 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-10-04 14:57
I'm okay with that. Grepping Lib shows that date/datetime/time and Decimal are the only types that implement __format__. Decimal largely implements the same language as float.
msg151388 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-01-16 16:39
IMO, now that two official releases of Python 3 have shipped with this behavior (3.1 and 3.2), it is too late to think about changing what datetime.__format__ does, and we should merely document that it is the same as strftime.
History
Date User Action Args
2012-01-16 16:39:36eric.araujosetmessages: + msg151388
2011-11-16 16:45:41floxsettype: behavior
versions: + Python 3.3, - Python 2.6, Python 3.1
2010-10-04 14:57:03eric.smithsetmessages: + msg117954
2010-10-04 14:49:46r.david.murraysetnosy: + r.david.murray
messages: + msg117953
2010-10-04 14:14:57belopolskylinkissue7789 superseder
2010-09-09 19:13:34floxsetnosy: + flox
2010-06-06 21:42:33eric.araujosetnosy: + eric.araujo
messages: + msg107236
2010-06-06 21:29:44eric.smithsetmessages: + msg107233
versions: + Python 2.6, Python 3.1, Python 2.7
2010-06-06 21:06:17belopolskysetmessages: + msg107228
2010-06-06 20:19:02eric.smithsetmessages: + msg107223
2010-06-06 20:18:00eric.smithsetnosy: + eric.smith
messages: + msg107222
2010-06-06 19:15:18brett.cannonsetmessages: + msg107211
2010-06-06 05:56:11belopolskysetnosy: + belopolsky
messages: + msg107184
2010-06-06 03:26:18brett.cannoncreate