Python:如何检查时间是否超过指定时间?

我正在尝试编写一个停车场收费计算器。提供多种费率:上午 7 点至晚上 10.30 - 每 30 分钟 0.60 美元 夜间费 晚上 10.30 至上午 7 点 - 固定费用 5 美元


如果停车时间从白天费率溢出到夜间费率,我该如何计算?下面是我现在所在的位置。我不确定从这里该去哪里,或者这是否是正确的开始方式。


def getCharge(self, endTime, startTime, rate):

    endTime = datetime.strptime(endTime, '%d/%m/%Y %H:%M')

    startTime = datetime.strptime(startTime, '%d/%m/%Y %H:%M')

    # elapsedDays = (endTime - startTime).days

    # print(elapsedDays)

    duration = (endTime - startTime).total_seconds() / 60

    print(duration)

    blocks = math.ceil( duration / 30 )

    print(blocks)

    charge = blocks * rate

    return charge

总结一下:

  1. 如果停车时间超过晚上 10.30(将收取夜间平价费用),我该如何添加支票

  2. 如果停车时间超过夜间时间并进入第二天早上7点后,如何添加支票(前一天的日费+夜间费+第二天的日费)


一只名叫tom的猫
浏览 131回答 2
2回答

森林海

根据 Alex 的回答,这里有一个稍微优化和改进的解决方案:import datetimeHALFHOUR_RATE = 0.6NIGHT_FEE = 5def get_charge2(start_datetime, end_datetime):&nbsp; &nbsp; tmp_datetime = start_datetime&nbsp; &nbsp; charged = 0&nbsp; &nbsp; while tmp_datetime < end_datetime:&nbsp; &nbsp; &nbsp; &nbsp; if tmp_datetime.hour >= 7 and tmp_datetime.hour < 22 or (tmp_datetime.hour == 22 and tmp_datetime.minute < 30):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # It is a day [7, 22:30)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charged += HALFHOUR_RATE * 10 # avoid rounding error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp_datetime += datetime.timedelta(minutes=30)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # It is a night [22:30, 7)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charged += NIGHT_FEE * 10&nbsp; # avoid rounding error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp_datetime += datetime.timedelta(hours=8.5) # night == 8.5 hours&nbsp; &nbsp;&nbsp; &nbsp; return charged/10 # adjust result

白板的微信

最简单的方法是每 30 分钟迭代一次并以这种方式计算费用。HALFHOUR_RATE = 0.6NIGHT_FEE = 5def get_charge(start_datetime, end_datetime):&nbsp; &nbsp; tmp_datetime = start_datetime&nbsp; &nbsp; charged = 0&nbsp; &nbsp; last_night_charged_day = None&nbsp; &nbsp; while tmp_datetime < end_datetime:&nbsp; &nbsp; &nbsp; &nbsp; if tmp_datetime.hour >= 22 or tmp_datetime.hour < 7:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if tmp_datetime.hour == 22 and tmp_datetime.minute < 30:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Still not a night&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charged += HALFHOUR_RATE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # It is a night&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; day = tmp_datetime.day&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if tmp_datetime.hour < 7:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; day -= 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if day != last_night_charged_day:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last_night_charged_day = day&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Charge one time night fee&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charged += NIGHT_FEE&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # It is a day&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charged += HALFHOUR_RATE&nbsp; &nbsp; &nbsp; &nbsp; tmp_datetime += datetime.timedelta(minutes=30)&nbsp; &nbsp; return charged
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python