21

让我们想象一下datetime

>>> 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

但是,当然,这是行不通的

4

6 回答 6

23

这需要微秒!

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 分钟(一刻钟没有闰秒,我认为 datetime 无法处理......
  • nsecs / 900是已经发生的一刻钟块的数量。取ceil这个四舍五入的四分之一小时块的数量。
  • 将四分之一小时的块数乘以 900 以计算出“舍入”后从一小时开始以来已经发生了多少秒。
于 2012-10-25T15:07:46.197 回答
12

@Mark Dickinson 提出了迄今为止最好的公式:

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)
于 2015-09-18T17:05:16.063 回答
9
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)
于 2012-10-25T15:05:24.300 回答
4

您只需要在将分钟、秒设置为零后计算正确的分钟并将它们添加到 datetime 对象中

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
于 2012-10-25T15:06:09.113 回答
1

@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,我们可以使用我们的 floor 函数向下舍入到最接近的五分钟增量:

>>> 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>)
于 2020-08-18T19:45:48.617 回答
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() 技巧,否则它在从时间戳转换期间使用本地时区

于 2019-11-25T11:33:14.347 回答