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