Tk Library Source Code
Artifact [937306495e]
Not logged in
Bounty program for improvements to Tcl and certain Tcl packages.

Artifact 937306495e81e874c693747753d31948dd7a1d0e33e0d4f91f608cd6e7c8b59c:

Attachment "calendar2.diff" to ticket [c800a75eb5] added by emiliano 2025-11-07 15:15:43.
Index: modules/widget/calendar.tcl
==================================================================
--- modules/widget/calendar.tcl
+++ modules/widget/calendar.tcl
@@ -542,16 +542,28 @@
 	# Replace month/year and do any necessary substs
 	return [subst [string map [list %m $month %Y $year] $format]]
     }
 
     method numberofdays {month year} {
-	if {$month == 12} {set month 0; incr year}
-	if {$month > 11} {
-	    set month [expr {$month % 12}]
-	    incr year
+	# We can be called with months 0 or 13 at year boundaries,
+	# with 0 meaning december last year and 13 january next year.
+	# In those cases, the number of days is always 31
+	if {($month == 0) || ($month == 13)} {
+	    return 31
 	}
-	clock format [clock scan "[incr month]/1/$year	1 day ago"] -format %d
+
+	# Determine if this is a leap year
+	set isleapyear [expr {
+	    ($year % 400) == 0 ||
+	    (($year % 4) == 0 && ($year % 100) != 0)
+	}]
+
+	# In the lists below, insert a first dummy value in order to match
+	# month -> index
+	return [lindex {{0 31 28 31 30 31 30 31 31 30 31 30 31}
+			{0 31 29 31 30 31 30 31 31 30 31 30 31}} \
+			    $isleapyear $month]
     }
 
     method lcycle _list {
 	upvar $_list list
 	set list [concat [lrange $list 1 end] [list [lindex $list 0]]]