如何在添加timedelta时不使用datetime开始第二天?

时间:2015-05-26 16:29:31

标签: python datetime timedelta

鉴于当前时间为23:30:00,我加上两小时(7200秒)。我怎样才能得到同一天的时间?所以我想要结果25:30:00

目前我只能得到第二天的时间:

>>> from datetime import datetime, timedelta
>>> current_time  = "23:30:00"
>>> duration = 3600
>>> (datetime.strptime(current_time, "%H:%M:%S") + timedelta(seconds=duration)).strftime("%H:%M:%S")
'00:30:00'

3 个答案:

答案 0 :(得分:2)

在我看来,你想要的只是添加两个timedeltas以获得另一个timedelta ...对吗?

from datetime import timedelta as td

t0 = td(hours=23, minutes=30)
t1 = t0 + td(seconds=7200)
print(t1)  # prints "1 day, 1:30:00"
print("Hours: {}".format(t1.days*24 + int(t1.seconds/3600)))  # prints "Hours: 25"

答案 1 :(得分:2)

如果您只想增加小时,分钟和创建字符串:

def weird_time(current_time,duration):
    start = datetime.strptime(current_time, "%H:%M:%S")
    st_hr, st_min, st_sec = start.hour, start.minute, start.second
    mn, secs = divmod(duration, 60)
    hour, mn = divmod(mn, 60)
    mn, secs = st_min+mn, st_sec+secs
    if secs > 59:
        m, secs = divmod(secs,60)
        mn += m
    if mn > 59:
        h, mn = divmod(mn,60)
        hour += h
    return "{:02}:{:02}:{:02}".format(st_hr+hour, mn, secs)

输出:

In [19]: weird_time("23:30:00",7200)
Out[19]: '25:30:00'

In [20]: weird_time("23:30:00",3600)
Out[20]: '24:30:00'

In [21]: weird_time("23:30:59",7203)
Out[21]: '25:31:02'

In [22]: weird_time("23:30:59",3601)
Out[22]: '24:31:00'

我们也可以使用timedelta来计算总秒数,并从中进行计算,而不是自己进行所有计算:

from datetime import datetime,timedelta


def weird_time(current_time,duration):
    start = datetime.strptime(current_time, "%H:%M:%S")
    st_hr, st_min, st_sec = start.hour, start.minute, start.second
    comb = timedelta(minutes=st_min,seconds=st_sec) + timedelta(seconds=duration)
    mn, sec = divmod(comb.total_seconds(), 60)
    hour, mn = divmod(mn, 60)
    return "{:02}:{:02}:{:02}".format(int(st_hr+hour), int(mn), int(sec))

哪个输出相同:

In [29]: weird_time("23:30:00",7200)
Out[29]: '25:30:00'

In [30]: weird_time("23:30:00",3600)
Out[30]: '24:30:00'

In [31]: weird_time("23:30:59",7203)
Out[31]: '25:31:02'

In [32]: weird_time("23:30:59",3601)
Out[32]: '24:31:00'

In [33]:  weird_time("05:00:00",3600)
Out[33]: '06:00:00'

只需要增加小时数,我们需要捕捉的部分是指秒,分钟或两者的总和大于59的时间。

答案 2 :(得分:2)

  

我想生成一个有效的GTFS数据集。谷歌定义进入第二天的旅行需要这样的时间:http://developers.google.com/transit/gtfs/reference#stop_times_fields

要获得正确的时间,您必须考虑夏令时的变化:

#!/usr/bin/env python
from datetime import datetime, time, timedelta
from tzlocal import get_localzone # $ pip install tzlocal

local_timezone = get_localzone()
current_time = datetime.now(local_timezone)
noon_naive = datetime.combine(current_time, time(12,0))
noon = local_timezone.localize(noon_naive, is_dst=None)

departure_time = (current_time - noon + timedelta(hours=12))
duration = timedelta(hours=2)
arrival_time = departure_time + duration
# -> datetime.timedelta(1, 5400)

timedelta转换为HH:MM:SS格式:

hours, seconds = divmod(arrival_time.total_seconds(), 3600)
minutes, seconds = divmod(seconds, 60)
print("%(hours)02d:%(minutes)02d:%(seconds)02d" % vars())
# -> 25:30:00