如何检查时间是否在日期范围内

时间:2017-02-13 07:27:30

标签: python datetime

我找到了一些解决方案,但大多数都谈到了当前的日期。我想要的是检查当前时间是否在第二天23:00:0007:00:00之内。我面临困难,因为日期也参与其中。因此,如果当前时间为00:00:00,那么日期也会发生变化,并将同一天的时间23:00:00进行比较会给我一个错误的结果。这就是我现在想出来的。请帮我找到解决方案

cur_time = dt.strptime('2/9/16 00:00', "%m/%d/%y %H:%M")
prev_date = dt.strftime(cur_time.date() - timedelta(days=1), "%m/%d/%y")
same_date = dt.strftime(cur_time.date(), '%m/%d/%y')
dosta = dt.strptime(prev_date + "22:00", "%m/%d/%y%H:%M")
satta = dt.strptime(same_date + "07:00", "%m/%d/%y%H:%M")
if dosta < cur_time < satta:
    tariff = 3
else:
    tariff = 6
print(tariff)

这会导致关税为3,但如果我改变cur_time这样的

cur_time = dt.strptime('2/9/16 23:00', "%m/%d/%y %H:%M")

这不会产生3.这会给我6.因为这是将当前时间与前一天00:00和当前日07:00进行比较,当前日23:00显然是更大。

修改1

Input: 2/9/16 06:00:00

Output: 3...because 06:00:00 is less than 07:00:00 that same day(2/9/2016) and more than 22:00:00 previous day(2/8/16)

Input: 2/9/16 23:00:00 Output: 3...because 23:00:00 is more than 22:00:00 that same day(2/9/2016)

Input: 2/10/16 00:30:00 Output: 3...because 00:30:00 is more than 22:00:00 that previous day(2/9/2016) and less than 07:00:00 same day(2/10/2016)

Input: 2/10/16 08:00:00 Output: 6...because 08:00:00 is more than 07:00:00 that same day(2/9/2016) and less than 22:00:00 on the same day(2/9/2016)

编辑2 timedelta所指出的,它仍无法与@a_guest一起使用。 这是代码

for each in zip(data1, data2):
    # client.write_points(json_body_bill_data)
    cur_time = dt.strptime(each[1]['Start'], "%m/%d/%y %H:%M")
    # print(cur_time.date())
    dosta = dt.strptime('2200', '%H%M').time()
    # print(dosta)
    lower = datetime.datetime.combine(cur_time.date(), dosta)
    upper = lower + timedelta(hours=8.5)
    print("lower", lower)
    print("upper", upper)
    print(cur_time, lower < cur_time < upper)

如果cur_time00:00:00 9/2/16

,这会让我失意

编辑3

可能的答案

from datetime import datetime, time
import csv
import os


CSV_FILE_ASCO = os.path.join('..', 'res', 'ASCO_METER.csv')
with open(CSV_FILE_ASCO, 'r') as f:
    reader = csv.DictReader(f)
    data = list(reader)

for each in data:
    # print()
    cur_date = datetime.strptime(each['Start'], "%m/%d/%y %H:%M")
    if time(7, 0) <= cur_date.time() < time(22, 0):
        tarrif = 6
    else:
        tarrif = 3
    print(cur_date, end=" ")
    print(tarrif)

可以进一步改进吗?我敢打赌,必须采用更加pythonic的方式来做到这一点

2 个答案:

答案 0 :(得分:1)

听起来最简单的解决办法就是完全摆脱日期,只是比较小时值。如果给定时间大于7 AM但小于11 PM,tarrif = 3,则为tarrif = 6。您可以使用datetime对象的hour属性:

执行此操作
from datetime import datetime

cur_date = datetime.strptime('2/9/16 07:00', "%m/%d/%y %H:%M")
if 7 <= cur_date.hour < 23:
    tarrif = 3
else:
    tarrif = 6

为了准确起见,我假设tarrif在点上午7点从6变为3,在点上11点变回6。

答案 1 :(得分:0)

在比较期间忽略日期并检查时间是否> 23:00或&lt; 07:00。检查工作fiddle

import datetime
dosta = datetime.datetime.strptime("23:00", '%H:%M')
satta = datetime.datetime.strptime("07:00", '%H:%M')

print dosta.time(), satta.time()

for h in range(0,24):

    h = str(h).zfill(1)
    cur_time = datetime.datetime.strptime('2/9/16 {h}:00'.format(h=h), "%m/%d/%y %H:%M")

    if cur_time.time() > dosta.time() or cur_time.time() < satta.time():
        print "IN RANGE " + h
    else:
        print "OUT OF RANGE " + h