如何在70分钟内使用日期时间?

时间:2014-12-08 23:40:02

标签: python date time

当程序到达这行代码时出现问题:

baggageDeliveryTime = datetime.datetime.strptime(baggageDeliveryTime, '%M')

当它到达这一行时,程序显示:

baggageDeliveryTime = datetime.datetime.strptime('70', '%M')

但是会议纪要最多只有59分 那么如何修改下面的代码超过59分钟呢?

flight = ('TAP98', 'Rio de Janeiro', '09:55', '323', '467')

import doctest, sort, datetime

def baggageEndDeliveryTime(flight):
    """
    Time that the delivery of all baggage is complete.
    Requires: flight is a tuple with four elements.
    Ensures: string which represents the time when the delivery of all baggage is complete.
    """
    baggageNumber = int(flight[4])

    baggageStartDeliveryTime = flight[2]
    baggageStartDeliveryTime = datetime.datetime.strptime(baggageStartDeliveryTime, "%H:%M")
    baggageStartDeliveryTime = datetime.timedelta(hours = baggageStartDeliveryTime.hour, minutes = baggageStartDeliveryTime.minute)

    baggageDeliveryTime = str(baggageNumber * 30 / 200)
    baggageDeliveryTime = datetime.datetime.strptime(baggageDeliveryTime, '%M')
    baggageDeliveryTime = datetime.timedelta(hours = baggageDeliveryTime.hour, minutes = baggageDeliveryTime.minute)

    baggageEndDeliveryTime = baggageStartDeliveryTime +  baggageDeliveryTime
    baggageEndDeliveryTime = baggageEndDeliveryTime.seconds
    baggageEndDeliveryTime = '{:02}:{:02}'.format(baggageEndDeliveryTime // 3600, baggageEndDeliveryTime % 3600 // 60)

    return baggageEndDeliveryTime

2 个答案:

答案 0 :(得分:0)

 baggageDeliveryTime = datetime.datetime.strptime('0', '%M') + datetime.timedelta(seconds = 60 * 70)

可能?

但是在评论中指出你应该只使用timedelta或者datetime.time对象

答案 1 :(得分:0)

嗯......你完全落后了......

如果您想计算与您的交货时间结束相对应的日期时间: 您的飞机抵达时间的日期时间+ timedelta需要获得行李...不是一个奇怪的计算到达时间的时间值+日期时间奇怪地从获得行李所需的时间计算...

而且datetime对象具有格式化能力......

def func(flight):
    nb_baggage = int(flight[4])

    start_time = datetime.datetime.strptime(flight[2], "%H:%M")
    delivery_time = datetime.timedelta(minutes=(nb_baggage * 30 / 200))
    end_time = start_time + delivery_time

    return end_time.strftime('%H:%M')

https://docs.python.org/3.4/library/datetime.html