Sometimes Computers are Too Smart
I’m working on an application that needs to be able to read a date from a string. Obviously there are countless libraries that will do this for me, so I essentially renamed the long datetime.datetime.strptime(string, “%m/%d/%Y”) to a shorter name.
I had initially assumed everything would be in mm/dd/yyyy format, but occasionally I would notice people leaving off the first two digits of the year. So I altered my wrapper method to deal with that, and things were working well. Then I noticed that sometimes people left off an initial 0 in front of a month, like 6/31/2008 vs. 06/31/2008. So I altered it again to deal with that.
But I was still having trouble parsing certain dates, and I couldn’t figure out why. Specifically, “6/31/08” wouldn’t work. At first I thought it was because it was both missing a leading 0 and two digits of the year, but after a while I realized that it wasn’t formatting because June has only 30 days, not 31.
Kind of annoying, but I guess it’s a good thing.