I was working around on working up a custom calendar for 2021, for my PC, and a light bulb lit up over my head. Last year, when we were sorting out whether a year was a leap year, or not, me and Bplus worked for a week or more to debug the ExtendedTimer routine.
We didn't have to, thanks to already existing code which we had in our toolbox, that we were too blind to use...
/SIGH!! It's the story of my life -- spend forever to do something the hard way, and then POOF turn around a little while later and realize it was sooooo simple to begin with. It's like rewiring a whole house, when all you needed to do was flip on the light switch to turn on the lights in the kitchen.
I present to you, IsLeapYear, in its simplest form:
FUNCTION IsLeapYear
(yyyy
) 'use 4 digit year IF GetDay
(2, 29, yyyy
) <> GetDay
(3, 1, yyyy
) THEN IsLeapYear
= -1
FUNCTION GetDay
(m
, d
, y
) 'use 4 digit year 'From Zeller's congruence: https://en.wikipedia.org/wiki/Zeller%27s_congruence
mm = m: dd = d: yyyy = y
IF mm
< 3 THEN mm
= mm
+ 12: yyyy
= yyyy
- 1 zerocentury = yyyy \ 100
result
= (dd
+ INT(13 * (mm
+ 1) / 5) + century
+ INT(century
/ 4) + INT(zerocentury
/ 4) + 5 * zerocentury
) MOD 7 GetDay = 7
GetDay = result
'Function changed to return a numeric value instead of a string for this program
' SELECT CASE result
' CASE 7: GetDay$ = "Saturday"
' CASE 1: GetDay$ = "Sunday"
' CASE 2: GetDay$ = "Monday"
' CASE 3: GetDay$ = "Tuesday"
' CASE 4: GetDay$ = "Wednesday"
' CASE 5: GetDay$ = "Thursday"
' CASE 6: GetDay$ = "Friday"
' END SELECT
We had a routine to get the day of the week, from any given date, and it's been working FOREVER AND EVER!! As long as the 29th isn't the same day as the 1st of the next month, we have a leap year. Otherwise, we don't...
One line logic, with no need to do math for mod 4, /400 but not 2000, except when - 29.6 or multiplied by 13.25...
Now, the question is: Why didn't anyone point out how simple this logic is to us back then?? :P