# Parsing date strings, inferring the year such that the date is not in the future

I got this program that I am working on and in it, I got a section that has a try statement and two error except statements.

try:
...
if ...: raise SyntaxError
except SyntaxError:
...
except ValueError:
...


The thing is that the SyntaxErrors are technically ValueErrors, but the errors occur for different reasons, therefore need different code to solve/get around the problem. So in order to prevent the code in all the "excepts" from being executed, I had to separate/make the type of Errors different. Is it okay to do that even though the SyntaxErrors are techincally ValueErrors, that I label them as SyntaxErrors? Or is there a method in solving this problem?

Here is the code of the program:

from datetime import datetime

today = datetime(2019, 1, 1).date()
this_year = 2019
last_year = this_year - 1

text = ["Dec 31 ", "@@@@@@@@@@@@@@@@@@@@@Dec 31 "]

for line in text:
date_str = line
try:
date = datetime.strptime(date_str + str(this_year), "%b %d %Y").date()
if date > today: raise SyntaxError
print("+", date)
except SyntaxError:
date = datetime.strptime(date_str + str(last_year), "%b %d %Y").date()
print("-", date)
except ValueError:
print("abnormality")
continue


Obtaining a date that is in the future is in no way a "syntax error", and it would be inappropriate to raise a SyntaxError. Instead of abusing the exception-handling mechanism, you should just use a conditional.

for date_str in text:
try:
date = datetime.strptime(date_str + str(this_year), "%b %d %Y").date()
if date <= today:
print("+", date)
else:
date = datetime.strptime(date_str + str(last_year), "%b %d %Y").date()
print("-", date)
except ValueError:
print("abnormality")


Note that you might be misinterpreting some ValueErrors as abnormalities. For example, "Feb 29 2021" would cause a ValueError, but then you would never get a chance to try "Feb 29 2020", which is a valid day in a leap year.

• Ok thank you. But if the current date is Feb 29 2021, there would be no point in look at Feb 29 2020 since it is a year behind. – StrangeRanger Aug 14 '18 at 6:14
• I think you thought that the program is supposed to look into future dates/year. But it is not supposed to do that. It looks at the past, in a sense. It moves the year backwards, but keeps the months and days the same. This is to prevent the variable “date” from equaling “Dec 31 2020” if Ithe current date is “Jan 1 2020”. The reason for that dives into a much bigger program. The current program that I posted was a small snippet that could work independently from the actual program it was apart of. – StrangeRanger Aug 14 '18 at 6:22
• Your goal may be to ensure that date is in the past. However, your implementation works by initially attempting to parse the date in the this_year calendar year, which may trigger an unintended ValueError before you even get a chance to do the if date > today test. – 200_success Aug 14 '18 at 6:25
• But a ValueError shouldn’t even come up because if I am looking at this years calendar and if there a a valid leap year, or if there is not, it will follow the rule of the calendar. – StrangeRanger Aug 14 '18 at 6:28
• It shouldn’t say Feb 29 if on this years calendar there is no Feb 29 since the program goes by this years calendar. – StrangeRanger Aug 14 '18 at 6:29

just a note on syntax error. a syntax error occurs when you have a fault with your writing of python, not with your logic or values. if the lexer goes through your program and finds if x == 3 ::, it will raise a syntax error as it finds an unexpected :. but 3 + 'm' is not of this type as the lexer finds nothing wrong in it.