[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Paul Ganssle added the comment: > Still, this feature is not appealing enough to try to squeeze into 3.7 > release schedule. I think this should go through a round of discussion > either on datetime-sig or python-ideas. Agreed. I will put together some summary in the next week or so and send

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: > (And, honestly, `dateutil` would provide a version-independent backport > anyway). Why not start with that? Remember: python standard library is where code goes to die. By implementing this feature in dateutil you will not be tied to relatively slo

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: I am about +0 on adding a keyword argument to datetime.now. Note that as I wrote in issue 19475 (msg202242), "precision" may be a misleading name because python makes no guarantee about the precision of the computer clock. Still, this feature is not ap

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Paul Ganssle added the comment: > This can be accomplished rather efficiently by truncating a time tuple: This will not preserve tzinfo, and (though this is not a concern unless nanosecond precision is added), I don't believe it preserves microseconds either. That said, it's also not very re

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: > replacing all elements of a datetime below a certain level is a very common > idiom This can be accomplished rather efficiently by truncating a time tuple: >>> t = datetime.now() >>> datetime(*t.timetuple()[:6]) datetime.datetime(2018, 1, 9, 14, 47, 1

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: > I think that a "truncate to rrule" function is *way* beyond the scope of the > standard library I agree and I did not propose that. What I said was that in the process of implementing truncate to rrule in dateutil you may encounter some common patte

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Paul Ganssle added the comment: > Looking at the dateutil, I don't see a truncate to rrule function. Maybe a > good starting point would be to implement that in dateutil and if some > simpler pattern emerges that can be proposed for stdlib, we can discuss it > then. I think that a "truncate

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: The problem that I have with the round/truncate proposal is that it is not general enough. Days, hours, minutes etc. are just arbitrary intervals that became popular for obscure historical and astronomical reasons. In practice, you are as likely to en

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Barry A. Warsaw
Barry A. Warsaw added the comment: On Jan 9, 2018, at 08:33, Paul Ganssle wrote: > @Barry I don't think it's a good idea to duplicate the `replace` > functionality in `datetime` like that. I think the main problem isn't the > `.replace`, it's the fact that you have to specify exactly which c

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Paul Ganssle added the comment: > In my experience, when dealing with temporal data truncation (rounding > towards -infinity) is more useful than any other form of rounding. See also > issue 19475. Ah, I agree - if you see that's how my __round__ implementation works. I guess that's another

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: In my experience, when dealing with temporal data truncation (rounding towards -infinity) is more useful than any other form of rounding. See also issue 19475. -- ___ Python tracker

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Paul Ganssle added the comment: One thing to note, the "example implementation" of __round__ above is an actual working prototype*: >>> round(Datetime.now(), 'second') Datetime(2018, 1, 9, 11, 59, 35) >>> round(Datetime.now(), 'day') Datetime(2018, 1, 9, 0, 0) >>> round(Datetime.now(), 'minu

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Paul Ganssle added the comment: I think if we're going to use `timedelta` then `__mod__` is the more appropriate option here, since it would be hard to interpret what `round(dt, timedelta(hours=2, microseconds=31))` would do. Either __mod__ or __round__ with `timedelta` is a bit of a stretch

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: Maybe __round__ can be generalized to take a timedelta instead of ndigits? For some related prior art, take a look at . -- ___ Python tracker

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Paul Ganssle added the comment: @Barry I don't think it's a good idea to duplicate the `replace` functionality in `datetime` like that. I think the main problem isn't the `.replace`, it's the fact that you have to specify exactly which components you want to set to zero - to get "the beginnin

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Barry A. Warsaw
Barry A. Warsaw added the comment: The .replace(microseconds=0) hack annoys me too, but I'd be happier with a simpler solution: make datetime.now() accept a microseconds parameter, so datetime.now(microseconds=0) would be equivalent to datetime.now().replace(microseconds=0) -- _

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Barry A. Warsaw
Change by Barry A. Warsaw : -- nosy: +barry ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.pytho

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Change by Paul Ganssle : -- type: -> enhancement ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Paul Ganssle added the comment: @Victor: With regards to getting a "date as datetime", that is another way to do it that I have also done in the past (and in fact it's how the new dateutil.utils.today() function is implemented: https://github.com/dateutil/dateutil/blob/master/dateutil/utils.p

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Paul Ganssle added the comment: An alternate possibility here might be to implement either `__round__` or a `round` function in `datetime` (which would basically automatically add this precision functionality to *all* the constructors, not just now). An example implementation: from datetime

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread STINNER Victor
STINNER Victor added the comment: > dt = datetime.now(precision='day') Why not creating a date and then convert it to a datetime object? > dt = datetime.now().replace(microseconds=0) Yeah, that one annoys me as well, but I learnt the .replace(microseconds=0) hack, or how to format without mi

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
New submission from Paul Ganssle : One thing I think that is fairly common is the desire to get the current datetime only up to a current precision, so you see a lot of things in, say, `dateutil` like this: dt = datetime.now().replace(hours=0, minutes=0, seconds=0, microseconds=0) Or: