Date and Time Inputs
Date Format
Check the Shape
Scripts often receive dates from arguments or configuration. A lightweight format check catches obvious mistakes before the value reaches a file name or command.
Program
Play the script to choose a date string and see whether it passes the shape check.
date_format_check.sh
#!/usr/bin/env bash
run_date=
if [[ "$run_date" == ????-??-?? ]]; then
status="iso-date"
else
status="bad-date"
fi
echo "$run_date:$status"
#!/usr/bin/env bash
run_date=
if [[ "$run_date" == ????-??-?? ]]; then
status="iso-date"
else
status="bad-date"
fi
echo "$run_date:$status"
ISO date
`YYYY-MM-DD` is easy to sort and compare as text.
shape check
A shape check catches values that are plainly not in the expected format.
input boundary
Validate date text at the boundary before building paths or commands.