Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass argument to datetime.timedelta

We already know that, datetime.timedelta(hours= 5) is a proper syntax. How is it possible to pass an argument that can replace 'hours' as given in the function down below?

    def check_time(until_when, def_time, how_long):
            if until_when- datetime.datetime.now() > datetime.timedelta(def_time = how_long):
            input('Task has been finished.\nPress any key to quit\n')
            exit()  
like image 769
Ali Avatar asked Feb 17 '23 21:02

Ali


1 Answers

def check_time(until_when, def_time, how_long):
    arg_dict = {def_time:how_long}
    dt = datetime.timedelta(**arg_dict)
    if until_when- datetime.datetime.now() > dt:
        input('Task has been finished.\nPress any key to quit\n')
        exit()

See Understanding kwargs in Python and the tutorial.

like image 102
tacaswell Avatar answered Feb 19 '23 09:02

tacaswell