Python - Ceil将日期时间提升到下一刻钟

时间:2012-10-25 14:56:26

标签: python math datetime ceil

让我们想象一下这个日期时间

>>> import datetime
>>> dt = datetime.datetime(2012, 10, 25, 17, 32, 16)

我想把它提交给下一个小时,以便

datetime.datetime(2012, 10, 25, 17, 45)

我想像

>>> quarter = datetime.timedelta(minutes=15)
>>> import math
>>> ceiled_dt = math.ceil(dt / quarter) * quarter

但当然这不起作用

6 个答案:

答案 0 :(得分:20)

这个需要微秒考虑!

import math

def ceil_dt(dt):
    # how many secs have passed this hour
    nsecs = dt.minute*60 + dt.second + dt.microsecond*1e-6  
    # number of seconds to next quarter hour mark
    # Non-analytic (brute force is fun) way:  
    #   delta = next(x for x in xrange(0,3601,900) if x>=nsecs) - nsecs
    # analytic way:
    delta = math.ceil(nsecs / 900) * 900 - nsecs
    #time + number of seconds to quarter hour mark.
    return dt + datetime.timedelta(seconds=delta)

t1 = datetime.datetime(2017, 3, 6, 7, 0)
assert ceil_dt(t1) == t1

t2 = datetime.datetime(2017, 3, 6, 7, 1)
assert ceil_dt(t2) == datetime.datetime(2017, 3, 6, 7, 15)

t3 = datetime.datetime(2017, 3, 6, 7, 15)
assert ceil_dt(t3) == t3

t4 = datetime.datetime(2017, 3, 6, 7, 16)
assert ceil_dt(t4) == datetime.datetime(2017, 3, 6, 7, 30)

t5 = datetime.datetime(2017, 3, 6, 7, 30)
assert ceil_dt(t5) == t5

t6 = datetime.datetime(2017, 3, 6, 7, 31)
assert ceil_dt(t6) == datetime.datetime(2017, 3, 6, 7, 45)

t7 = datetime.datetime(2017, 3, 6, 7, 45)
assert ceil_dt(t7) == t7

t8 = datetime.datetime(2017, 3, 6, 7, 46)
assert ceil_dt(t8) == datetime.datetime(2017, 3, 6, 8, 0)

delta的解释:

  • 900秒是15分钟(四分之一小时 sans闰秒,我认为日期时间不会处理...
  • nsecs / 900是已发生的四分之一小时块的数量。以ceil为单位计算四分之一小时的数量。
  • 将四分之一小时的块数乘以900,以计算自“四舍五入”后的小时开始已经发生的秒数。

答案 1 :(得分:8)

def ceil(dt):
    if dt.minute % 15 or dt.second:
        return dt + datetime.timedelta(minutes = 15 - dt.minute % 15,
                                       seconds = -(dt.second % 60))
    else:
        return dt

这会给你:

>>> ceil(datetime.datetime(2012,10,25, 17,45))
datetime.datetime(2012, 10, 25, 17, 45)
>>> ceil(datetime.datetime(2012,10,25, 17,45,1))
datetime.datetime(2012, 10, 25, 18, 0)
>>> ceil(datetime.datetime(2012,12,31,23,59,0))
datetime.datetime(2013,1,1,0,0)

答案 2 :(得分:7)

到目前为止

@Mark Dickinson suggested最好的公式:

def ceil_dt(dt, delta):
    return dt + (datetime.min - dt) % delta

在Python 3中,对于任意时间增量(不仅仅是15分钟):

#!/usr/bin/env python3
import math
from datetime import datetime, timedelta

def ceil_dt(dt, delta):
    return datetime.min + math.ceil((dt - datetime.min) / delta) * delta

print(ceil_dt(datetime(2012, 10, 25, 17, 32, 16), timedelta(minutes=15)))
# -> 2012-10-25 17:45:00

为避免中间花车,可以使用divmod()

def ceil_dt(dt, delta):
    q, r = divmod(dt - datetime.min, delta)
    return (datetime.min + (q + 1)*delta) if r else dt

示例:

>>> ceil_dt(datetime(2012, 10, 25, 17, 32, 16), timedelta(minutes=15))
datetime.datetime(2012, 10, 25, 17, 45)
>>> ceil_dt(datetime.min, datetime.resolution) 
datetime.datetime(1, 1, 1, 0, 0)
>>> ceil_dt(datetime.min, 2*datetime.resolution)
datetime.datetime(1, 1, 1, 0, 0)
>>> ceil_dt(datetime.max, datetime.resolution)
datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
>>> ceil_dt(datetime.max, 2*datetime.resolution)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in ceil_dt
OverflowError: date value out of range
>>> ceil_dt(datetime.min+datetime.resolution, datetime.resolution)
datetime.datetime(1, 1, 1, 0, 0, 0, 1)
>>> ceil_dt(datetime.min+datetime.resolution, 2*datetime.resolution)
datetime.datetime(1, 1, 1, 0, 0, 0, 2)
>>> ceil_dt(datetime.max-datetime.resolution, datetime.resolution)
datetime.datetime(9999, 12, 31, 23, 59, 59, 999998)
>>> ceil_dt(datetime.max-datetime.resolution, 2*datetime.resolution)
datetime.datetime(9999, 12, 31, 23, 59, 59, 999998)
>>> ceil_dt(datetime.max-2*datetime.resolution, datetime.resolution)
datetime.datetime(9999, 12, 31, 23, 59, 59, 999997)
>>> ceil_dt(datetime.max-2*datetime.resolution, 2*datetime.resolution)
datetime.datetime(9999, 12, 31, 23, 59, 59, 999998)
>>> ceil_dt(datetime.max-timedelta(1), datetime.resolution)
datetime.datetime(9999, 12, 30, 23, 59, 59, 999999)
>>> ceil_dt(datetime.max-timedelta(1), 2*datetime.resolution)
datetime.datetime(9999, 12, 31, 0, 0)
>>> ceil_dt(datetime.min, datetime.max-datetime.min)
datetime.datetime(1, 1, 1, 0, 0)
>>> ceil_dt(datetime.max, datetime.max-datetime.min)
datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)

答案 3 :(得分:2)

您只需计算正确的分钟数,并在将分钟数,秒数设置为零后将其添加到日期时间对象

import datetime

def quarter_datetime(dt):
    minute = (dt.minute//15+1)*15
    return dt.replace(minute=0, second=0)+datetime.timedelta(minutes=minute)

for minute in [12, 22, 35, 52]:
    print quarter_datetime(datetime.datetime(2012, 10, 25, 17, minute, 16))

适用于所有情况:

2012-10-25 17:15:00
2012-10-25 17:30:00
2012-10-25 17:45:00
2012-10-25 18:00:00

答案 4 :(得分:0)

这是我的代码可在任何时期使用:

def floorDT(dt, secperiod):
    tstmp = dt.timestamp()
    return datetime.datetime.fromtimestamp(
        math.floor(tstmp/secperiod)*secperiod).astimezone().astimezone(datetime.timezone.utc)


def ceilDT(dt, secperiod):
    tstmp = dt.timestamp()
    return datetime.datetime.fromtimestamp(
        math.ceil(tstmp/secperiod)*secperiod).astimezone().astimezone(datetime.timezone.utc)

注意:我们必须使用astimezone()。astimezone()技巧,否则在从时间戳转换时会使用本地时区

答案 5 :(得分:0)

formula proposed here by @Mark Dickinson的运行效果很好,但是我需要一个能够同时处理时区和夏令时(DST)的解决方案。

我使用pytz到达:

import pytz
from datetime import datetime, timedelta

def datetime_ceiling(dt, delta):
    # Preserve original timezone info
    original_tz = dt.tzinfo
    if original_tz:
        # If the original was timezone aware, translate to UTC.
        # This is necessary because datetime math does not take
        # DST into account, so first we normalize the datetime...
        dt = dt.astimezone(pytz.UTC)
        # ... and then make it timezone naive
        dt = dt.replace(tzinfo=None)
    # We only do math on a timezone naive object, which allows
    # us to pass naive objects directly to the function
    dt = dt + ((datetime.min - dt) % delta)
    if original_tz:
        # If the original was tz aware, we make the result aware...
        dt = pytz.UTC.localize(dt)
        # ... then translate it from UTC back its original tz.
        # This translation applies appropriate DST status.
        dt = dt.astimezone(original_tz)
    return dt

可以通过更改一行代码来实现几乎相同的floor函数:

def datetime_floor(dt, delta):
    ...
    dt = dt - ((datetime.min - dt) % delta)
    ...

以下日期时间是从DST转换回标准时间(STD)的三分钟:

datetime.datetime(2020, 11, 1, 1, 57, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)

假设上面的值为dt,我们可以使用下限函数四舍五入到最接近的五分钟增量:

>>> datetime_floor(dt, timedelta(minutes=5))
datetime.datetime(2020, 11, 1, 1, 55, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)

时区和与DST的关系被保留。 (天花板功能也是如此。)

在该日期DST将在凌晨2点结束,此时时间将“回滚”到STD凌晨1点。如果我们使用上限功能从夏令时凌晨1:57开始舍入,则应该在夏令时凌晨2点结束,而应该在夏令时凌晨1:00结束,这就是结果:

>>> datetime_ceiling(dt, timedelta(minutes=5))
datetime.datetime(2020, 11, 1, 1, 0, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)