I recently ran across some code that drove a user interface for producing reports. Since the default report period was one year, the code fetched the current date, subtracted a year, then created a new date from it to use as the start date of the report. It all looked simple enough except for a small problem. On one day every four years, this code crashes. Obviously I'm talking about leap years. They may make coding painful but they are a fact of life we must deal with. If you just do naive calculations like this, you're going to trip across leap years and have problems that occur rarely but predictably. Make sure that when you perform date calculations that you not only take care of leap years but that your tests also include tests to cover leap years.
Download