Posted to tcl by msiism at Mon Feb 08 22:22:48 GMT 2021view pretty

proc _days_in_month {year month} {
  ## Return the number of days in a given month of a given year

  set procname _days_in_month
  set days_per_month {31 28 31 30 31 30 31 31 30 31 30 31}

  set month_norm [scan $month %d]
  set year_norm [scan $year %d]

  if {$month_norm < 1 || $month_norm > 12} {
    error "$procname: invalid month number -- '$month'"
  }

  if {[clock scan ${year_norm}-${month_norm}-1 -format %Y-%m-%d] < 0} {
    error\
     [format {%s %s} "$procname: specified month includes negative Unix time"\
       "-- '$year-$month'"]
  }

  if {$month_norm == 2} {
    set secs_per_day 86400
    lset days_per_month 1 [clock format\
      [expr {[clock scan $year_norm-3-1 -format %Y-%m-%d] - $secs_per_day}]\
      -format %d]
  }

  return [lindex $days_per_month [expr {$month_norm - 1}]]
}