[issue19475] Add microsecond flag to datetime isoformat()
STINNER Victor added the comment: 'seconds' - %H:%M:%S 'us' - %H:%M:%S.%f 'us' is not consistent with the datetime module: it should be 'microseconds. datetime.datetime.now().second 50 datetime.timedelta(seconds=1) datetime.timedelta(0, 1) datetime.datetime.now().microsecond 123710 datetime.timedelta(microseconds=1) datetime.timedelta(0, 0, 1) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Changes by Berker Peksag berker.pek...@gmail.com: -- stage: resolved - needs patch superseder: datetime: add ability to parse RFC 3339 dates and times - versions: +Python 3.6 -Python 3.5 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Changes by Jerry Elmore jerry.r.elm...@gmail.com: -- nosy: +Jerry Elmore ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Alexander Belopolsky added the comment: Here is some prior art: GNU date utility has an --iso-8601[=timespec] option defined as ‘-I[timespec]’ ‘--iso-8601[=timespec]’ Display the date using the ISO 8601 format, ‘%Y-%m-%d’. The argument timespec specifies the number of additional terms of the time to include. It can be one of the following: ‘auto’ Print just the date. This is the default if timespec is omitted. ‘hours’ Append the hour of the day to the date. ‘minutes’ Append the hours and minutes. ‘seconds’ Append the hours, minutes and seconds. ‘ns’ Append the hours, minutes, seconds and nanoseconds. If showing any time terms, then include the time zone using the format ‘%z’. https://www.gnu.org/software/coreutils/manual/html_node/Options-for-date.html -- versions: +Python 3.5 -Python 3.4 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Alexander Belopolsky added the comment: Based on GNU date prior art, we can introduce timespec='auto' keyword argument with the following values: 'auto' - (default) same as current behavior 'hours' - %H 'minutes' - %H:%M 'seconds' - %H:%M:%S 'us' - %H:%M:%S.%f -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Changes by Chris Rebert pyb...@rebertia.com: -- nosy: +cvrebert ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Alexander Belopolsky added the comment: MAL: Have you thought about the rounding/truncation issues associated with not showing microseconds ? I believe it has to be the truncation. Rounding is better left to the user code where it can be done either using timedelta arithmetics or at the time source. I would expect that in the majority of cases where lower resolution printing is desired the times will be already at lower resolution at the source. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Marc-Andre Lemburg added the comment: On 06.11.2013 16:51, Alexander Belopolsky wrote: MAL: Have you thought about the rounding/truncation issues associated with not showing microseconds ? Sure, otherwise I wouldn't have mentioned it :-) mxDateTime always uses 2 digit fractions when displaying date/time values. This has turned out to be a good compromise between accuracy and usability. In early version, I used truncation, but that caused (too many) roundtrip problems, so I started using careful rounding in later versions: /* Fix a second value for display as string. Seconds are rounded to the nearest microsecond in order to avoid cases where e.g. 3.42 gets displayed as 03.41 or 3.425 is diplayed as 03.42. Special care is taken for second values which would cause rounding to 60.00 -- these values are truncated to 59.99 to avoid the value of 60.00 due to rounding to show up even when the indicated time does not point to a leap second. The same is applied for rounding towards 61.00 (leap seconds). The second value returned by this function should be formatted using '%05.2f' (which rounds to 2 decimal places). */ This approach has worked out well, though YMMV. I believe it has to be the truncation. Rounding is better left to the user code where it can be done either using timedelta arithmetics or at the time source. I would expect that in the majority of cases where lower resolution printing is desired the times will be already at lower resolution at the source. In practice you often don't know the resolution of the timing source. Nowadays, the reverse of what you said is usually true: the source resolution is higher than the precision you use to print it. MS SQL Server datetime is the exception to that rule, with a resolution of 333ms and weird input rounding: http://msdn.microsoft.com/en-us/library/ms187819.aspx For full seconds, truncation will add an error of +/- 1 second, whereas rounding only adds +/- 0.5 seconds. This is what convinced me to use rounding instead of truncation. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Alexander Belopolsky added the comment: I am afraid that the rounding issues may kill this proposal. Can we start with something simple? For example, we can start with show=None keyword argument and allow a single value 'microseconds' (or 'us'). This will solve the issue at hand with a reasonable syntax: t.isoformat(show='us'). If other resolutions will be required, we can later add more values and may even allow t.isoformat(show=2) to show 2 decimal digits. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Skip Montanaro added the comment: I am afraid that the rounding issues may kill this proposal. Can we start with something simple? For example, we can start with show=None keyword argument and allow a single value 'microseconds' (or 'us'). This will solve the issue at hand with a reasonable syntax: t.isoformat(show='us'). If other resolutions will be required, we can later add more values and may even allow t.isoformat(show=2) to show 2 decimal digits. I don't think the meaning of this proposed show keyword argument should be overloaded as you suggest. If you show microseconds, just show all of them. Furthermore... If we go far enough back, my original problem was really that the inclusion of microseconds in csv module output was inconsistent, making it impossible for me to later parse those values in another script using a fixed strptime format. Since the csv module uses str() to convert input values for output, nothing you do to isoformat() will have any effect on my original problem. In my own code (where I first noticed the problem) I acquiesced, and changed this d[time] = now to this: d[time] = now.strftime(%Y-%m-%dT%H:%M:%S.%f) where now is a datetime object. I thus guarantee that I can parse these timestamps later using the same format. I realize the inclusion of T means my fields changed in other ways, but that was intentional, and not germane to this discussion. So, fiddle all you want with isoformat(), but do it right. I vote that if you want to add a show parameter it should simply include all fields down to that level, omitting any lower down. If people want to round or truncate things you can give them that option, returning a suitably adjusted, new datetime object. I don't think rounding, truncation, or other numeric operations should be an element of conversion to string form. This does not happen today: import datetime x = datetime.datetime.now() x datetime.datetime(2013, 11, 6, 12, 19, 5, 759020) x.strftime(%Y-%m-%d %H:%M:%S) '2013-11-06 12:19:05' (%S doesn't produce 06) Skip -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Changes by STINNER Victor victor.stin...@gmail.com: -- nosy: +haypo ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Changes by Alexander Belopolsky alexander.belopol...@gmail.com: -- nosy: +belopolsky ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Alexander Belopolsky added the comment: +1 on adding an option to isoformat(). We already have an optional sep argument, so the symmetry with __str__ is not complete. To make this option more useful, rather than implementing always_emit_microseconds=False flag, I would add a keyword argument 'precision' that would take ('hour'|'minute'|'second'|millisecond'|'microsecond') value. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Andrei Dorian Duma added the comment: I would like to implement this feature. I already wrote the Python part. Is there anything else to decide? -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
STINNER Victor added the comment: 2013/11/5 Alexander Belopolsky rep...@bugs.python.org: +1 on adding an option to isoformat(). We already have an optional sep argument, so the symmetry with __str__ is not complete. To make this option more useful, rather than implementing always_emit_microseconds=False flag, I would add a keyword argument 'precision' that would take ('hour'|'minute'|'second'|millisecond'|'microsecond') value. Hour precision is not part of the ISO 8601 standard. resolution is maybe a better name for the new parameter than precision: http://www.python.org/dev/peps/pep-0418/#glossary The new parameter should be added to datetime.datetime.isoformat() but also datetime.time.isoformat(). -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Alexander Belopolsky added the comment: +1 on all Victor's points. I like 'resolution' because this is the term that datetime module uses already: from datetime import * datetime.resolution datetime.timedelta(0, 0, 1) There is a slight chance of confusion stemming from the fact that datetime.resolution is timedelta, but proposed parameter is a string. I believe ISO 8601 uses the word accuracy to describe this kind of format variations. I am leaning towards resolution, but would like to hear from others. Here are the candidates: 1. resolution 2. accuracy 3. precision (Note that accuracy is the shortest but resolution is the most correct.) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Marc-Andre Lemburg added the comment: On 05.11.2013 21:31, STINNER Victor wrote: 2013/11/5 Alexander Belopolsky rep...@bugs.python.org: +1 on adding an option to isoformat(). We already have an optional sep argument, so the symmetry with __str__ is not complete. To make this option more useful, rather than implementing always_emit_microseconds=False flag, I would add a keyword argument 'precision' that would take ('hour'|'minute'|'second'|millisecond'|'microsecond') value. Hour precision is not part of the ISO 8601 standard. resolution is maybe a better name for the new parameter than precision: http://www.python.org/dev/peps/pep-0418/#glossary The new parameter should be added to datetime.datetime.isoformat() but also datetime.time.isoformat(). Since this ticket is about being able to remove the seconds fraction part, I think it's better to use a name that is not already overloaded with other meanings, e.g. show_us=False or show_microseconds=False. BTW: Have you thought about the rounding/truncation issues associated with not showing microseconds ? A safe bet is truncation, but this can lead to inaccuracies of up to a second. Rounding is difficult, since it can lead to a 60 second value showing up for e.g. 11:00:59.95 seconds, or the need to return 12:00:00 for 11:59:59.95. -- nosy: +lemburg ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Changes by Andrei Dorian Duma andrei.duma.dor...@gmail.com: -- nosy: +andrei.duma ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19475] Add microsecond flag to datetime isoformat()
Terry J. Reedy added the comment: As I understand Guido's message, he reopened this to consider adding a new parameter. Given an existing csv file like that given, either Tim's solution or try: parse with microseconds except ValueError: parse without should work. -- nosy: +terry.reedy title: Inconsistency between datetime's str()/isoformat() and its strptime() method - Add microsecond flag to datetime isoformat() type: behavior - enhancement versions: -Python 2.7, Python 3.3 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19475 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com