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

  1. proc _days_in_month {year month} {
  2. ## Return the number of days in a given month of a given year
  3.  
  4. set procname _days_in_month
  5. set days_per_month {31 28 31 30 31 30 31 31 30 31 30 31}
  6.  
  7. set month_norm [scan $month %d]
  8. set year_norm [scan $year %d]
  9.  
  10. if {$month_norm < 1 || $month_norm > 12} {
  11. error "$procname: invalid month number -- '$month'"
  12. }
  13.  
  14. if {[clock scan ${year_norm}-${month_norm}-1 -format %Y-%m-%d] < 0} {
  15. error\
  16. [format {%s %s} "$procname: specified month includes negative Unix time"\
  17. "-- '$year-$month'"]
  18. }
  19.  
  20. if {$month_norm == 2} {
  21. set secs_per_day 86400
  22. lset days_per_month 1 [clock format\
  23. [expr {[clock scan $year_norm-3-1 -format %Y-%m-%d] - $secs_per_day}]\
  24. -format %d]
  25. }
  26.  
  27. return [lindex $days_per_month [expr {$month_norm - 1}]]
  28. }