I passed filenames such as abcef-1591282803
into this function and the function worked fine:
def parse_unixtimestamp(filename):
ts = re.findall(r'\d{10}', filename)[0]
return ts
However, then I modified the function to this so the it also works when the timestamp is of 13 digits instead of 10. file20-name-1591282803734
def parse_unixtimestamp(filename):
ts = re.findall(r'\d{13}|\d{10}', filename)[0]
return ts
It didn't throw an error until here. But in case of 13 digits, I get ts values like this 1591282803734
. Now when I pass this value to this function for year
:
def get_dateparts(in_unixtimestamp):
dt_object = datetime.fromtimestamp(int(in_unixtimestamp))
date_string = dt_object.strftime("%Y-%m-%d")
year_string = dt_object.strftime("%Y")
month_string = dt_object.strftime("%m")
day_string = dt_object.strftime("%d")
logger.info(f'year_string: {year_string}')
result = {"date_string": date_string, "year_string": year_string, "month_string": month_string,
"day_string": day_string}
return result
I get an error that:
year 52395 is out of range
I wouldn't get this error when the unixtimestamp passed into parse_unixtimestamp
Is only 10 digits. How can I modify this function such that it works in both cases?