[Python-ideas] strptime without second argument as an inverse to __str__

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Mon Aug 4 22:56:56 CEST 2014


On 04.08.2014 22:14, Skip Montanaro wrote:
> On Mon, Aug 4, 2014 at 2:23 PM, Alexander Belopolsky
> <alexander.belopolsky at gmail.com> wrote:
>>
>> On Mon, Aug 4, 2014 at 3:00 PM, Skip Montanaro <skip at pobox.com> wrote:
>>>
>>>> Why is this question relevant for Ram's proposal?
>>>
>>> It would seem to have some impact on how hard it is to create a
>>> general inverse. Will one format work for all platforms ("one and
>>> done"), or will the inverse implementation potentially have to be
>>> updated as new platforms come into (or go out of) existence?
>>
>>
>> I think str(datetime) format is an implementation detail to the same extent
>> as str(int) or str(float) is.  In the past, these variations did not prevent
>> providing (sometimes imperfect) inverse.
>
> I took a look at whatever version of CPython I have laying about (some
> variant of 2.7). str(datetime) seems to be well-defined as calling
> isoformat with " " as the separator. The only caveat is that if the
> microsecond field is zero, it's omitted.
>
> If that behavior holds true in 3.x, only two cases require consideration:
>

it does hold true in 3.x, but the documented behavior is slightly more 
complex (I assume also in 2.x):

datetime.__str__()
     For a datetime instance d, str(d) is equivalent to d.isoformat(' ').

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

     If utcoffset() does not return None, a 6-character string is 
appended, giving the UTC offset in (signed) hours and minutes: 
YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM or, if microsecond is 0 
YYYY-MM-DDTHH:MM:SS+HH:MM

     The optional argument sep (default 'T') is a one-character 
separator, placed between the date and time portions of the result.

> %Y-%m-%d %H:%M:%S
> %Y-%m-%d %H:%M:%S.%f
>

=> plus timezone versions of the above.

Wolfgang



More information about the Python-ideas mailing list