How to fetch records between a particular time?

Viewed 36

Suppose I have a list of timestamps something like this from a specific time to specific time (8:20AM to 12:20AM). How do I fetch only those whose time is from 8:20AM to 9:20AM?

[2021-03-19 08:20:00.923000+00:00
2021-03-19 08:20:02.221000+00:00
2021-03-19 08:20:02.268000+00:00
2021-03-19 08:20:03.194000+00:00
2021-03-19 08:20:03.253000+00:00
2021-03-19 08:20:03.485000+00:00
2021-03-19 08:20:04.275000+00:00
2021-03-19 08:20:04.945000+00:00
2021-03-19 08:20:05.285000+00:00
2021-03-19 08:20:05.530000+00:00
2021-03-19 08:20:05.609000+00:00
2021-03-19 08:20:05.556000+00:00
2021-03-19 08:20:07.453000+00:00
2021-03-19 08:20:07.860000+00:00
2021-03-19 08:20:07.876000+00:00
2021-03-19 08:20:08.450000+00:00
2021-03-19 08:20:08.806000+00:00
2021-03-19 08:20:09.680000+00:00
2021-03-19 08:20:09.699000+00:00
2021-03-19 08:20:11.081000+00:00
.
.
. ] 

I tried doing it using:

lastHourDateTime = datetime.datetime.now() - datetime.timedelta(hours = 1)
print lastHourDateTime.strftime('%Y-%m-%d %H')

However that is not what I am expecting. since the lastHourDatetime has datetime.now, so I have to manually keep changing the code. How do I fetch records of timestamps from 8:20AM to 9:20AM?

1 Answers

How about this?

from dateutil import parser

timestamps = [
    "2021-03-19 07:20:00.923000+00:00",
    "2021-03-19 08:20:00.923000+00:00",
    "2021-03-19 08:20:02.221000+00:00",
    "2021-03-19 08:20:03.485000+00:00",
    "2021-03-19 08:20:04.275000+00:00",
    "2021-03-19 08:20:05.285000+00:00",
    "2021-03-19 08:20:07.876000+00:00",
    "2021-03-19 08:20:08.450000+00:00",
    "2021-03-19 08:20:08.806000+00:00",
    "2021-03-19 09:20:09.680000+00:00",
    "2021-03-19 09:20:09.699000+00:00",
    "2021-03-19 09:20:11.081000+00:00",
    "2021-03-19 10:20:11.081000+00:00",
]

print("\n".join(t for t in timestamps if parser.parse(t).time().strftime("%H:%M") in ["08:20", "09:20"]))

Output:

2021-03-19 08:20:00.923000+00:00
2021-03-19 08:20:02.221000+00:00
2021-03-19 08:20:03.485000+00:00
2021-03-19 08:20:04.275000+00:00
2021-03-19 08:20:05.285000+00:00
2021-03-19 08:20:07.876000+00:00
2021-03-19 08:20:08.450000+00:00
2021-03-19 08:20:08.806000+00:00
2021-03-19 09:20:09.680000+00:00
2021-03-19 09:20:09.699000+00:00
2021-03-19 09:20:11.081000+00:00

EDIT:

As pointed out in the comments, if you want to match any time between 08:20 and 09:20 then try this:

from dateutil import parser

timestamps = [
    "2021-03-19 07:20:00.923000+00:00",
    "2021-03-19 08:20:00.923000+00:00",
    "2021-03-19 08:20:02.221000+00:00",
    "2021-03-19 08:20:03.485000+00:00",
    "2021-03-19 08:20:04.275000+00:00",
    "2021-03-19 08:20:05.285000+00:00",
    "2021-03-19 08:20:07.876000+00:00",
    "2021-03-19 08:21:08.450000+00:00",
    "2021-03-19 08:30:08.806000+00:00",
    "2021-03-19 08:56:08.806000+00:00",
    "2021-03-19 09:09:09.680000+00:00",
    "2021-03-19 09:20:09.680000+00:00",
    "2021-03-19 09:26:09.699000+00:00",
    "2021-03-19 09:58:11.081000+00:00",
    "2021-03-19 10:20:11.081000+00:00",
]

print("\n".join(t for t in timestamps if "08:20" <= parser.parse(t).time().strftime("%H:%M") <= "09:20"))

Or a good old for loop if you feel like having one:

for time_ in timestamps:
    t = parser.parse(time_).time().strftime("%H:%M")
    if "08:20" <= t <= "09:20":
        print(time_)

Output:

2021-03-19 08:20:00.923000+00:00
2021-03-19 08:20:02.221000+00:00
2021-03-19 08:20:03.485000+00:00
2021-03-19 08:20:04.275000+00:00
2021-03-19 08:20:05.285000+00:00
2021-03-19 08:20:07.876000+00:00
2021-03-19 08:21:08.450000+00:00
2021-03-19 08:30:08.806000+00:00
2021-03-19 08:56:08.806000+00:00
2021-03-19 09:09:09.680000+00:00
2021-03-19 09:20:09.680000+00:00
2021-03-19 08:20:00.923000+00:00
2021-03-19 08:20:02.221000+00:00
2021-03-19 08:20:03.485000+00:00
2021-03-19 08:20:04.275000+00:00
2021-03-19 08:20:05.285000+00:00
2021-03-19 08:20:07.876000+00:00
2021-03-19 08:21:08.450000+00:00
2021-03-19 08:30:08.806000+00:00
2021-03-19 08:56:08.806000+00:00
2021-03-19 09:09:09.680000+00:00
2021-03-19 09:20:09.680000+00:00
Related