[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

[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

[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

[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,

[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])

[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

[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

[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

[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

[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

[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

[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) >>>

[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`

[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 . -- ___

[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

[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

[issue32522] Add precision argument to datetime.now

2018-01-09 Thread Paul Ganssle
Change by Paul Ganssle : -- type: -> enhancement ___ Python tracker ___ ___

[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:

[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

[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,

[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,