3 ms·
I distinctly remember being shown this ~35 years ago when I was first shown UNIX as a demonstration of how high-quality the commands were implemented... even so
by bodyfour 5y ago
I distinctly remember being shown this ~35 years ago when I was first shown UNIX as a demonstration of how high-quality the commands were implemented... even something as weird as the calendar changeover was accounted for.
And, yes, it exists even in the earliest versions of the "cal" command, dating back to the mid 1970s: https://github.com/dspinellis/unix-history-repo/blob/Research-V5-Snapshot-Development/usr/source/s1/cal.c#L136 https://github.com/dspinellis/unix-history-repo/blob/Researc...
The implementation is pretty clever as well. The jan1() function simply returns the day-of-week the year starts on (and knows about the calendar-system change). The cal() function compares jan1(y) and jan1(y+1) to detect leap years by noticing if the weekday of Jan 1st advances one or two days.. and if it's anything else it assumes it's handling the 1752 case. Then inside of the printing loop it just jumps from 2 to 14 whenever it is rendering a month that happens to contain 19 days.