R. David Murray added the comment:

No, it is not "it's not broke because there's a workaround", it is not broken 
because it is *working as designed*.

The str of any object in python is intended to be a convenient representation 
of that object.  Note that this applies to *all* objects in Python, not just 
datetime.  (Thus, talking about deprecating __str__ doesn't make any sense.)

The most convenient representation of a datetime has been deemed to be 
yyyy-mm-dd hh:mm:ss if there microseconds is equal to zero, and yyy-mm-dd 
hh:mm:ss.MMMMMM if microseconds is not equal to zero, and in practice this has, 
in general, worked out very well.  It is too bad that Python can't read the 
programmer's mind and know whether or not zero microseconds are important in a 
given str call, but sadly it can't, so the above algorithm is the most 
convenient for the typical uses of str on a datetime.

Now, if you are writing an application and you are doing output that needs to 
be in a particular format, you should *format your output* to be the way you 
want it.  This applies to any data, not just datetimes.  It means using a 
format specification, to which the str of an object might or might not be an 
input.  In the case of datetime, you don't want to use its str as the input to 
the format, because that can loose information, as you have discovered.  
Instead you want to specify an strftime format string that causes the data to 
be displayed *exactly as you want it to be*:

    >>> counter1 = 23360
    >>> x = datetime.datetime(2013, 2, 5, 20, 45)
    >>> x
    datetime.datetime(2013, 2, 5, 20, 45)
    >>> str(x)
    '2013-02-05 20:45:00'
    >>> "counter1 is: {:6d} time is: {:%Y-%m-%d %H:%M:%S.%f}".format(counter1, 
x)
    'counter 1 is:  23360 time is: 2013-02-05 20:45:00.000000'

str is a generic function that is a convenience.  A format specification is, 
well, *specific* to a particular application.  Note how it is necessary to also 
use a format for the counter in order to obtain output records with consistent 
spacing.

Now, as far as documentation goes, __str__ references isoformat, and isoformat 
says:

    datetime.isoformat(sep='T')

        Return a string representing the date and time in ISO 8601 format,
        YYYY-MM-DDTHH:MM:SS.mmmmmm or, if microsecond is 0,
        YYYY-MM-DDTHH:MM:SS

It is hard to see how the documentation can get clearer than that.  

(As an aside, I *can* see the argument, as was made in the other issue, that 
the isoformat method should provide a way to control the micorseconds display.  
That would be a feature request, but given the history of this issue you should 
probably raise it on the python-ideas mailing list first in order to get buy-in 
before opening a new request for enhancement for it.  Not optimal, I know, but 
sometimes you have to deal with history when suggesting/requesting a change in 
a project.

Also note that datetime.now() does not have *any* format.  It is a datetime 
object.  Only when turning it in to a string by some method does formatting 
come in to play.

I want to thank you for your enthusiasm for making things better.  I hope that 
my more detailed explanation will help you understand the reasoning behind the 
design decisions that have been made here.

----------
resolution:  -> invalid
stage:  -> committed/rejected
status: open -> closed

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue17139>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to