#7 Validating Date Formats

One of the most challenging validation tasks, but one that's crucial for shell scripts that work with dates, is to ensure that a specific date is actually possible. If we ignore leap years, this task is not too bad, because the calendar is well behaved and consistent each year. All we need in that case is a table with the days of each month against which to compare a specified date. To take leap years into account, you have to add some additional logic to the script. One set of rules for calculating a leap year is as follows:

  • Years not divisible by 4 are not leap years.

  • Years divisible by 4 and by 400 are leap years.

  • Years divisible by 4, not divisible by 400, and divisible by 100, are not leap years.

  • All other years ...

Get Wicked Cool Shell Scripts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.