[Python-ideas] Making datetime __str__ and isoformat more consistent
Andrew Barnert
abarnert at yahoo.com
Sat Nov 2 01:28:09 CET 2013
On Nov 1, 2013, at 14:29, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 11/01/2013 01:37 PM, MRAB wrote:
>> On 01/11/2013 19:49, Ethan Furman wrote:
>>> On 11/01/2013 11:15 AM, Skip Montanaro wrote:
>>>>
>>>> http://bugs.python.org/issue19475
>>>>
>>>> Do others agree with me that consistency in this situation is better than
>>>> the current behavior?
>>>
>>> I haven't seen the arguments in favor of this awkward behavior, so I may change my mind, but at the moment I would
>>> certainly argue for consistency: either emit microseconds in __str__ or ignore remaining microseconds or ignore a
>>> trailing %f (or all three ;) .
>> Suppose there were, say, 900000 microseconds, i.e. 0.9 seconds?
>>
>> If the microseconds aren't shown by __str__, should it truncate or
>> round?
>
> My first (and preferred) option was to emit the microseconds (even if 0).
>
> At the heart of this issue is: If datetime.datetime is not going to always emit the microseconds, then there should be an easy roundtrip method to get the value back; currently there is not.
>
> Maybe the best fix is to make the csv module smarter about how it outputs datetime's (so it would always emit microseconds).
Why should the round trip involve strftime? What about just adding a fromisoformat classmethod constructor that's meant specifically for round tripping isoformat? That means if isoformat sometimes emits microseconds and sometimes doesn't, fromisoformat can take both strings with microseconds and those without.
(In fact, there's no reason it couldn't be even more flexible and handle all of the valid variations of ISO format, not just the ones isoformat generates...)
More information about the Python-ideas
mailing list