I am trying to convert from a text-based date into DateTime, and then integer timestamp, however, I am getting issues on how to manage the code in the right way for timestamp translation
def get_past_date(str_days_ago):
TODAY = datetime.date.today()
splitted = str_days_ago.split()
if len(splitted) == 1 and splitted[0].casefold() == 'today':
return str(TODAY.isoformat())
elif len(splitted) == 1 and splitted[0].casefold() == 'yesterday':
date = TODAY - relativedelta(days=1)
return str(date.isoformat())
elif len(splitted) > 1 and splitted[1].casefold() in ['hour', 'hours', 'hr', 'hrs', 'h','uur', 'Stunden','horas','or\u0103','ore','godz.','timmar','\u0447\u0430\u0441\u0430']:
date = datetime.datetime.now() - relativedelta(hours=int(splitted[0]))
return str(date.date().isoformat())
elif len(splitted) > 1 and splitted[1].casefold() in ['day', 'days', 'd','dagar','dag','dagen','Tag','Tagen','d\u00edas','dni','\u0434\u043d\u044f','\u0434\u043d\u0435\u0439','\u0434\u043d\u044f','giorni']:
date = TODAY - relativedelta(days=int(splitted[0]))
return str(date.isoformat())
elif len(splitted) > 1 and splitted[1].casefold() in ['mon', 'mons', 'month', 'months', 'm','maanden','mesi','mies.','m\u00e5nader','\u043c\u0435\u0441\u044f\u0446\u0435\u0432']:
date = TODAY - relativedelta(months=int(splitted[0]))
return str(date.isoformat())
elif len(splitted) > 1 and splitted[2].casefold() in ['stunden','horas','or\u0103','ore']:
date = datetime.datetime.now() - relativedelta(hours=int(splitted[1]))
return str(date.date().isoformat())
elif len(splitted) > 1 and splitted[2].casefold() in ['tag','tagen','d\u00edas','zile']:
date = TODAY - relativedelta(days=int(splitted[1]))
return str(date.isoformat())
elif len(splitted) > 1 and splitted[2].casefold() in ['monaten','meses','luni']:
date = TODAY - relativedelta(months=int(splitted[1]))
return str(date.isoformat())
elif len(splitted) > 1 and splitted[4].casefold() in ["heures"]:
date = datetime.datetime.now() - relativedelta(hours=int(splitted[3]))
return str(date.date().isoformat())
elif len(splitted) > 1 and splitted[4].casefold() in ["jours"]:
date = TODAY - relativedelta(days=int(splitted[3]))
return str(date.isoformat())
elif len(splitted) > 1 and splitted[4].casefold() in ["mois"]:
date = TODAY - relativedelta(months=int(splitted[3]))
return str(date.isoformat())
else:
return "Wrong Argument format"
a = get_past_date("7 days ago")
b = get_past_date("14 days ago")
c = get_past_date("17 giorni fa")
d = get_past_date("today")
print(a)
print(b)
print(c)
print(d)
output
2019-10-25
2019-10-18
2019-10-15
2019-11-01
[Finished in 0.0s]
I already tried to change str(TODAY.isoformat())
into int
, but it doesn't seem to work this way