如何四舍五入的date时间对象python的分钟

我有一个使用strptime()生成的date时间对象。

>>> tm datetime.datetime(2010, 6, 10, 3, 56, 23) 

我需要做的是一分钟到最近的第十分钟。 到目前为止,我一直在做的是采取分钟的价值,并使用round()。

 min = round(tm.minute, -1) 

但是,与上面的例子一样,当分钟值大于56时,它会给出一个无效的时间,即:3:60

什么是更好的方法来做到这一点? date时间是否支持这个?

这将得到tm中存储的datetime对象的“floor”,并在tm之前舍入到10分钟的标记。

 tm = tm - datetime.timedelta(minutes=tm.minute % 10, seconds=tm.second, microseconds=tm.microsecond) 

如果你想经典的四舍五入到最近的10分钟,做到这一点:

 discard = datetime.timedelta(minutes=tm.minute % 10, seconds=tm.second, microseconds=tm.microsecond) tm -= discard if discard >= datetime.timedelta(minutes=5): tm += datetime.timedelta(minutes=10) 

或这个:

 tm += datetime.timedelta(minutes=5) tm -= datetime.timedelta(minutes=tm.minute % 10, seconds=tm.second, microseconds=tm.microsecond) 

一般function可以在任何时间以秒为单位舍入date时间:

 def roundTime(dt=None, roundTo=60): """Round a datetime object to any time laps in seconds dt : datetime.datetime object, default now. roundTo : Closest number of seconds to round to, default 1 minute. Author: Thierry Husson 2012 - Use it as you want but don't blame me. """ if dt == None : dt = datetime.datetime.now() seconds = (dt.replace(tzinfo=None) - dt.min).seconds rounding = (seconds+roundTo/2) // roundTo * roundTo return dt + datetime.timedelta(0,rounding-seconds,-dt.microsecond) 

1小时取整和30分钟取整的样本:

 print roundTime(datetime.datetime(2012,12,31,23,44,59,1234),roundTo=60*60) 2013-01-01 00:00:00 print roundTime(datetime.datetime(2012,12,31,23,44,59,1234),roundTo=30*60) 2012-12-31 23:30:00 

从最好的答案我修改为只使用date时间对象的修改版本,这避免了必须做到秒转换,使调用代码更具可读性:

 def roundTime(dt=None, dateDelta=datetime.timedelta(minutes=1)): """Round a datetime object to a multiple of a timedelta dt : datetime.datetime object, default now. dateDelta : timedelta object, we round to a multiple of this, default 1 minute. Author: Thierry Husson 2012 - Use it as you want but don't blame me. Stijn Nevens 2014 - Changed to use only datetime objects as variables """ roundTo = dateDelta.total_seconds() if dt == None : dt = datetime.datetime.now() seconds = (dt - dt.min).seconds # // is a floor division, not a comment on following line: rounding = (seconds+roundTo/2) // roundTo * roundTo return dt + datetime.timedelta(0,rounding-seconds,-dt.microsecond) 

1小时取整和15分钟取样:

 print roundTime(datetime.datetime(2012,12,31,23,44,59),datetime.timedelta(hour=1)) 2013-01-01 00:00:00 print roundTime(datetime.datetime(2012,12,31,23,44,49),datetime.timedelta(minutes=15)) 2012-12-31 23:30:00 

我使用了Stijn Nevens代码,效果很好(谢谢Stijn),并且有一个小插件可以共享。 四舍五入到最近。

 def round_time(dt=None, date_delta=timedelta(minutes=1), to='average'): """ Round a datetime object to a multiple of a timedelta dt : datetime.datetime object, default now. dateDelta : timedelta object, we round to a multiple of this, default 1 minute. from: http://stackoverflow.com/questions/3463930/how-to-round-the-minute-of-a-datetime-object-python """ round_to = date_delta.total_seconds() if dt is None: dt = datetime.now() seconds = (dt - dt.min).seconds if to == 'up': # // is a floor division, not a comment on following line (like in javascript): rounding = (seconds + round_to) // round_to * round_to elif to == 'down': rounding = seconds // round_to * round_to else: rounding = (seconds + round_to / 2) // round_to * round_to return dt + timedelta(0, rounding - seconds, -dt.microsecond) 

如果你不想使用条件,你可以使用modulo运算符:

 minutes = int(round(tm.minute, -1)) % 60 

UPDATE

你想要这样的东西吗?

 def timeround10(dt): a, b = divmod(round(dt.minute, -1), 60) return '%i:%02i' % ((dt.hour + a) % 24, b) timeround10(datetime.datetime(2010, 1, 1, 0, 56, 0)) # 0:56 # -> 1:00 timeround10(datetime.datetime(2010, 1, 1, 23, 56, 0)) # 23:56 # -> 0:00 

..如果你想结果作为string。 为获得date时间结果,最好使用timedelta – 查看其他响应;)

 def get_rounded_datetime(self, dt, freq, nearest_type='inf'): if freq.lower() == '1h': round_to = 3600 elif freq.lower() == '3h': round_to = 3 * 3600 elif freq.lower() == '6h': round_to = 6 * 3600 else: raise NotImplementedError("Freq %s is not handled yet" % freq) # // is a floor division, not a comment on following line: seconds_from_midnight = dt.hour * 3600 + dt.minute * 60 + dt.second if nearest_type == 'inf': rounded_sec = int(seconds_from_midnight / round_to) * round_to elif nearest_type == 'sup': rounded_sec = (int(seconds_from_midnight / round_to) + 1) * round_to else: raise IllegalArgumentException("nearest_type should be 'inf' or 'sup'") dt_midnight = datetime.datetime(dt.year, dt.month, dt.day) return dt_midnight + datetime.timedelta(0, rounded_sec) 

根据Stijn Nevens和Django进行修改,将当前时间舍入为最接近的15分钟。

 from datetime import date, timedelta, datetime, time def roundTime(dt=None, dateDelta=timedelta(minutes=1)): roundTo = dateDelta.total_seconds() if dt == None : dt = datetime.now() seconds = (dt - dt.min).seconds # // is a floor division, not a comment on following line: rounding = (seconds+roundTo/2) // roundTo * roundTo return dt + timedelta(0,rounding-seconds,-dt.microsecond) dt = roundTime(datetime.now(),timedelta(minutes=15)).strftime('%H:%M:%S') dt = 11:45:00 

如果你需要完整的date和时间,只需删除.strftime('%H:%M:%S')

当exception被捕获时,速度不是最好的,但是这会起作用。

 def _minute10(dt=datetime.utcnow()): try: return dt.replace(minute=round(dt.minute, -1)) except ValueError: return dt.replace(minute=0) + timedelta(hours=1) 

计时

 %timeit _minute10(datetime(2016, 12, 31, 23, 55)) 100000 loops, best of 3: 5.12 µs per loop %timeit _minute10(datetime(2016, 12, 31, 23, 31)) 100000 loops, best of 3: 2.21 µs per loop