How to validate if a date indicated as a string belongs to an interval of 2 dates indicated in another string?

Question:

import os, datetime

content = os.listdir("when")
print(content)
#this print...
#['2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm.txt', '2023-02-05 00pp00 am.txt']

for i in range(len(content)):
    content[i] = content[i].replace("_-_", "-").replace("pp", ":")
print(content) #I prepare the input to use it to search
#this print...
#['2022-12-29 12:33 am _--_ 2023-01-25 19:13 pm.txt', '2023-02-05 00:00 am.txt']

input_to_search_in_folder = "2022_-_01_-_05 12:33 am"  #file data to find in the 'when' folder

I have changed the : to pp (referring to point-point) because you cannot place : in folders or/and files, at least not in Windows

2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm
            initial date _--_ final date

In this case input_to_search_in_folder = "2022_-_01_-_05 12:33 am" does not match a file with a specific date name. But if it belongs to the interval of days indicated in the file name '2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm.txt'

How could I validate that this date "2022_-_01_-_05 12:33 am" does belong to that time interval '2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm' or if it’s this date '2023-02-05 00:00 am'?

If the validation is successful, the program should print the content inside that .txt (in this case inside the 2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm.txt )

text_file = open("when/" + , "r")
data_inside_this_file = text_file.read()
text_file.close()

#And finally prints the content of the .txt file that matches the date specified in the 'input_to_search_in_folder' variable
print(repr(data_inside_this_file))
Asked By: Matt095

||

Answers:

I would clean the strings fully, convert them to datetime objects (because these can be compared to each other), then compare then and you have the result and can do whatever with it:

import os
from datetime import datetime


content = os.listdir("when")
print(content)
#['2022_-_12_-_29 12pp33 am _--_ 2023_-_01_-_25 19pp13 pm.txt', '2023-02-05 00pp00 am.txt']

for i in range(len(content)):
    content[i] = content[i].replace("_-_", "-").replace("pp", ":")
#['2022-12-29 12:33 am _--_ 2023-01-25 19:13 pm.txt', '2023-02-05 00:00 am.txt']

cleaned_filename = os.path.splitext(content[0])[0] #="2022-12-29 12:33 am _--_ 2023-01-25 19:13 pm"
start_dt = datetime.strptime(content[0].split(" _--_ ")[0], "%Y-%m-%d %H:%M")
#="2022-12-29 12:33 am" = datetime(2022, 12, 29, 12, 33)
last_dt = datetime.strptime(content[0].split(" _--_ ")[1], "%Y-%m-%d %H:%M")
#="2023-01-25 19:13 pm"
third_dt = datetime.strptime(os.path.splitext(content[1])[0], "%Y-%m-%d %H:%M")
#="2023-02-05 00:00 am"

input_to_search = "2022_-_01_-_05 12:33 am".replace("_-_", "-") 
#"2022-01-05 12:33 am".
input_dt = datetime.strptime(input_to_search, "%Y-%m-%d %H:%M")
#="datetime(2022, 01, 05, 12, 33)"

if start_dt <= input_dt <= last_dt:
    print("in between")
elif input_dt == third_dt:
    print("Match")
else:
    print("No!")
Answered By: mrblue6