0

I need to define a function to get a [list] of str values of time from "M" minutes after the current time, up to the current time (including current time), and then check whether any of the values match the time value in the Time column of a given CSV file.

I thought of the idea for a for loop but could not figure out how to add and append the list for M no. of times ( The Quality of an Absolute Beginner). So, I adjusted with the following code which only supports M = 1 :

def Time_Check_Min(M= 1):
    #Check the current system time
    timestr = datetime.now()
    Check_TIMEnow = timestr.strftime("%H:%M")

    #Add 'M' min to Current Time
    Ahead_Time = (timestr + timedelta(minutes=M))
    Check_TIME = Ahead_Time.strftime("%H:%M")

    #Check if the current time is mentioned in the Dataframe
    if Check_TIME in df_.Time.values or Check_TIMEnow in df_.Time.values:
        return True
    else:
        return False

I require the output as a list in ('%H:%M') format so as to then check if any of them is present in the CSV. For Example, Taking the current system time to be '16:50' and M = 3, then the list should contain 4 elements, like :

['16:50', '16:51', '16:52', '16:53']

Also, I thought of using the between_time method since I am using pandas. But again, I do not know if this would really help. Also, I am sincerely speaking, I required a solution to this problem only... But I just asked this question: Get a List of Time Values before a (certain) Minutes from the Current time upto the Current Time in Python, just as a Simp. Thus, The whole lines are the same (Copied), Just the question is reversed/corrected.

Do I need to change my approach? If Yes, then which would be the best way to do so... and if not, HOW to get that damn list?

Thanks for Helping and Understanding the Problem...

PNxZEN
  • 51
  • 2
  • 6

1 Answers1

0

I think you can use this code. Put in any number of values for M

from datetime import datetime
from dateutil.relativedelta import relativedelta
now=datetime.now()
current_time=now.strftime("%H:%M")
time_list=[]
M=70
for i in range(0,M+1):
    six_months = now + relativedelta(minutes=i)
    print(six_months.strftime("%H:%M"))
    time_list.append(six_months.strftime("%H:%M"))