Message309732
> 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]) |
|
Date |
User |
Action |
Args |
2018-01-09 19:54:54 | belopolsky | set | recipients:
+ belopolsky, tim.peters, barry, vstinner, p-ganssle |
2018-01-09 19:54:54 | belopolsky | set | messageid: <1515527694.42.0.467229070634.issue32522@psf.upfronthosting.co.za> |
2018-01-09 19:54:54 | belopolsky | link | issue32522 messages |
2018-01-09 19:54:54 | belopolsky | create | |
|