0

I need an input prompt that will allow the user to enter a date at the point the function is called and then run to concert the date to Unix date for an API call later in the program. At the moment every attempt seems to hand back an error regarding Str, to Int.

I get the function wants to convert the date in line 9 to a date time value but I can't create an input that I could then convert.

def unixconvert() :
# importing datetime module
    import datetime
    import time
    # assigned regular string date
    date_time = datetime.datetime(2021, 7, 26, 21, 20)
    # print regular python date&time
    print("date_time =>",date_time) 
    # displaying unix timestamp after conversion
    print("unix_timestamp => ",
        (time.mktime(date_time.timetuple())))

I have tried a standard input('enter a date') I have tried passing a multiple variables and trying to build the input, year =, month = etc.

  • Have you tried casting your inputs from str to int by `var = int(var)`? – Stitt May 26 '23 at 10:23
  • I did although I may not have done it properly. I got this error ValueError: invalid literal for int() with base 10: '2022, 11, 12, 00, 99' – Christopher May 26 '23 at 10:26
  • You will need to parse each number separately if you want to input in this way, `int()` will not handle commas. See some examples here https://stackoverflow.com/questions/15226898/python-3-2-input-date-function – Stitt May 26 '23 at 10:28
  • Traceback (most recent call last): File "/WeatherApp/code_1.py", line 17, in unixconvert() File "/WeatherApp/code_1.py", line 14, in unixconvert (time.mktime(date_time.timetuple()))) AttributeError: 'str' object has no attribute 'timetuple' – Christopher May 26 '23 at 10:32
  • To parse the user input, use `dateutil.parser.parse()`. – C14L May 26 '23 at 10:35

1 Answers1

0

What about something like this:

def unixconvert():                                                
                                         
    date_time_input = input() #something like 2021-7-26-21-20

    date_time = datetime.datetime(*(int(x) for x in date_time_input.split('-')))

    print("date_time =>",date_time)

    print("unix_timestamp => ", (time.mktime(date_time.timetuple())))

It's basically split by - and convert every number to int.


Maybe something like this is more clear:

year, month, day, hours, minutes = (int(x) for x in date_time_input.split('-'))
date_time = datetime.datetime(year, month, day, hours, minutes)
Pablo C
  • 4,661
  • 2
  • 8
  • 24
  • This worked! Just from a knowledge perspective and believe me my python is built only around SARIMA and a few Stat modules what is the * for before the int? – Christopher May 26 '23 at 10:37
  • @Christopher it's called stared expression and in this case is for unpacking what is inside of the parenthesis :) – Pablo C May 26 '23 at 10:40
  • side note, datetime objects have a `timestamp` method, which can be used instead of `time.mktime(date_time.timetuple())` to get the corresponding Unix time. – FObersteiner May 26 '23 at 10:52