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 belopolsky
Recipients barry, belopolsky, p-ganssle, tim.peters, vstinner
Date 2018-01-09.19:54:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1515527694.42.0.467229070634.issue32522@psf.upfronthosting.co.za>
In-reply-to
Content
> 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, 12)
>>> datetime(*t.timetuple()[:5])
datetime.datetime(2018, 1, 9, 14, 47)
>>> datetime(*t.timetuple()[:4])
datetime.datetime(2018, 1, 9, 14, 0)
>>> datetime(*t.timetuple()[:3])
datetime.datetime(2018, 1, 9, 0, 0)

if you do this often, you can wrap this in a function


_PARTS = {'seconds': 6, 'minutes': 5, ...}
def truncate_to(t, timespec):
    return datetime(*t.timetuple()[:_PARTS[timespec])
History
Date User Action Args
2018-01-09 19:54:54belopolskysetrecipients: + belopolsky, tim.peters, barry, vstinner, p-ganssle
2018-01-09 19:54:54belopolskysetmessageid: <1515527694.42.0.467229070634.issue32522@psf.upfronthosting.co.za>
2018-01-09 19:54:54belopolskylinkissue32522 messages
2018-01-09 19:54:54belopolskycreate