This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author p-ganssle
Recipients barry, belopolsky, p-ganssle, tim.peters, vstinner
Date 2018-01-09.20:16:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1515528962.57.0.467229070634.issue32522@psf.upfronthosting.co.za>
In-reply-to
Content
> 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 readable code without a wrapper - it's not obvious that you're trying to truncate, or what level you're truncating to, just reading it. I think it's worth considering "truncate" to be a first-class operation of datetimes, since it comes up very frequently - people truncating off unnecessary microseconds from `now`, people truncating the result of `datetime.now()` to get today, people getting the beginning of a given month, etc.

Of course, then the question is just "where does this wrapper live". It can live in user code, which is probably not ideal since a bunch of people are implementing their own versions of this common operation and carrying around `utils` submodules or whatever just for this, or it can live in third party libraries like `dateutil` or `boltons`, or it can live in the standard library - where it will likely get the most optimized treatment. (And, honestly, `dateutil` would provide a version-independent backport anyway).

That said, if the answer to the above is "not in the standard library", I think it makes sense to add a precision argument to `now`, since that is probably the most common use case for this sort of truncation function - and it also makes a lot of sense to allow users to specify the precision with which they get the current time.
History
Date User Action Args
2018-01-09 20:16:02p-gansslesetrecipients: + p-ganssle, tim.peters, barry, belopolsky, vstinner
2018-01-09 20:16:02p-gansslesetmessageid: <1515528962.57.0.467229070634.issue32522@psf.upfronthosting.co.za>
2018-01-09 20:16:02p-gansslelinkissue32522 messages
2018-01-09 20:16:02p-gansslecreate