0

I have found some solution but most of them talks about the current date. What I want is that to check if a current time is within 23:00:00 and 07:00:00 next day. I am facing difficulty because date is also involved into this. So if the current time is 00:00:00 then the date would also change and comparing the time with same day 23:00:00 would give me a faulty result. This is what I have come up with now. Please help me in finding the solution

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)

This would result the tariff to be 3 but if I change the cur_time like this

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

this won't yield 3. This would give me 6. Because this is comparing the current time with previous day 00:00 and current day 07:00 and current day 23:00 would obviously be greater.

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

Edit 2 It is still not working with timedelta as pointed out by @a_guest. Here is the code

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)

This is giving me false if cur_time is 00:00:00 9/2/16

Edit 3

The probable answer

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)

Can It be improved further. I bet there has to be a more pythonic way to do it

Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38
  • To me, it looks like you have all the code segments you need, but you assemble it the wrong way. The problem is that you subtract one day from the current time, no matter if you have to or not. Can you come up with a possible fix yourself? That's always better than to get an answer made up by someone else... – nostradamus Feb 13 '17 at 07:33
  • What's wrong with using `test = cur_time.date() < datetime.today().date()`? – l'L'l Feb 13 '17 at 07:34
  • I can not use `today()` because `cur_time` is coming from a csv file and I need to compare with the time and date mentioned in the csv file. – Mayukh Sarkar Feb 13 '17 at 07:40
  • @nostradamus If I don't subtract one day from it then, if the current time is `00:00:00` comparing it with `22:00:00` wouldn't work because it would compare with the same date future `23:00:00` o'clock not the previous date – Mayukh Sarkar Feb 13 '17 at 07:42
  • Yes, that's what I meant... So you need an if-statement, and depending on it you have to subtract / add something to the current day. If you can add more examples (input --> outcome), I would understand better and could probably provide you with a solution. – nostradamus Feb 13 '17 at 07:45
  • You can just create a `datetime` object representing the lower boundary and then add a `timedelta(hours=7)` object in order to obtain the upper boundary. – a_guest Feb 13 '17 at 07:53
  • @nostradamus Added some input output cases – Mayukh Sarkar Feb 13 '17 at 07:53
  • @a_guest I have check that out then..Hope that would do the trick. – Mayukh Sarkar Feb 13 '17 at 07:54
  • Just to clarify, you want to compare the date you read from a CSV to the current date on the clock, right? – Daniel Hepper Feb 13 '17 at 08:19
  • @DanielHepper No. I want to compare the date and time from the csv with the csv dates and `22:00` and `07:00` time – Mayukh Sarkar Feb 13 '17 at 08:37
  • I'm confused. The code in **edit 3** prints `2016-02-09 06:00:00 3`. According to **edit 1**, it should be `2016-02-09 06:00:00 6`. You are currently only looking at the hour of each date in the CSV. If you want to compare the dates in the CSV to another date, you obviously need another date in your code. – Daniel Hepper Feb 13 '17 at 09:16
  • @DanielHepper Thanks for mentioning. That was a typo..Changed it – Mayukh Sarkar Feb 13 '17 at 10:13
  • related [python time range validator](https://stackoverflow.com/q/28526012/4279) – jfs Feb 18 '18 at 06:31

2 Answers2

2

It sounds like the easiest solution would be to get rid of the dates altogether and just compare hour values on their own. If a given time is greater than than 7 AM but less than 11 PM, tarrif = 3, else tarrif = 6. You can do this using the datetime object's hour property:

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

For the sake of exactness, I'm assuming the tarrif changes from 6 to 3 at 7AM on the dot, and back to 6 at 11PM on the dot.

daveruinseverything
  • 4,775
  • 28
  • 40
0

Ignore the date during compare and check if time is > 23:00 or < 07:00. Check working 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
Vanojx1
  • 5,574
  • 2
  • 23
  • 37
  • It won't be `or`. It would be `and`. Moreover I can not reject the date. I have to take date and time both. Because till `2/9/16 23:30` comparison has to be done with `2/9/16 22:00` but at `2/10/16 00:00`, comparison can not be done with `2/10/16 22:00` – Mayukh Sarkar Feb 13 '17 at 08:42
  • Every 'next day' between '00:00 ' and '07:00' it's the same of checking the same day between '00:00 ' and '07:00' – Vanojx1 Feb 13 '17 at 09:08