Date functions: days_in_month


days_in_month() #@ Return number of days in given month
{               #@ USAGE: days_in_month [VAR] MONTHNUM [year]
                #@ Required functions: is_leap_year(), is_var()
                #@ Global variables set: days_in_month
  local var
  is_var "$1" && shift
  case ${1#0} in
    9|4|6|11) days_in_month=30 ;; ## 30 days hath September...
    1|3|5|7|8|10|12) days_in_month=31 ;;
    2) is_leap_year ${2:-`date +%Y`} &&
        days_in_month=29 ||
        days_in_month=28
        ;;
    *) return 5 ;;
  esac
  [[ "$var" ]] &&
     printf -v "$var" %d "$days_in_month" ||
     printf "%s\n" "$days_in_month"
}
    Thirty days hath September
      April, June and November.
        All the rest have thirty-one
          Excepting February alone
            Which has but twenty-eight
              Or twenty-nine each leap year