Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Leap years? How does that work?

by talexb (Chancellor)
on Feb 02, 2022 at 02:33 UTC ( [id://11141046]=perlmeditation: print w/replies, xml ) Need Help??

In my defence, I'm a nerd. Dad's a retired actuary, and I've been fiddling with math and logic puzzles forever. So it made sense that an idea that's been bubbling away in my head decided to wake me this morning. I wanted to write it down immediately, but I had to have breakfast, then I had to head out for my weekly grocery shopping during a pandemic thing, then I had to put in my work day. Then I had supper and a couple of glasses of wine.

So at this point, I'm ready to jot down a few thoughts on leap years.

Imagine, if you will, the time and effort it took for people to realize that while 365 days in a year was very close, it was not quite the right value. Then imagine the you are the brave astronomer and local science dude who has to have a polite conversation with, say, The Pope about this, having already braved the underlings and the Pope's secretary. Why the Pope? Because he's the one who is going to say, OK, we're going to change the calendar now., and people are going to listen and obey.

    Astronomer: You Holiness, Thank you for taking time out of your day to see me. I will be brief.

    The Pope: Galileo! So good to see you. Please, call me Jules!

    A: Your Holiness, please.

    TP: OK, OK, I kid. So, they tell me you want to change the calendar? How's that work?

    A: OK, You may recall we have been using 365 days in a year, but you will also know that we've discovered this is incorrect -- we're falling behind. The Winter solstice is now mid-January, and we need to sort things out.

    TP: Yes, yes, that's fine, we'll just publish a decree to reset the date. Will that be all?

    A (quaking a little): You holiness, it's a bit more complicated. We need to add an occasional day to make things work out. A year is about 365.24 days.

    TP: Ah, so your first approximation is to add a day every four years?

    A (surprised): Your Holiness! You have an excellent grasp of the mathematics!

    TP: Yes, yes, thank you. But this gets us only part the way there. What else?

    A: Well, every hundred years we'll skip the extra day.

    TP: Is that it?

    A: Also, every four hundred years, we have to add it back in.

    TP: Well, that sounds fine. So we'll an extra day in December, and make it 32 days every .. once in a while?

    A: Well, your holiness, that is a possibility, but I'd like to suggest making the shortest month the recipient of this extra day. Say, February, which currently has just 28 days.

    TP: An extra day of winter? Are you sure that's wise?

    A: As you know, holiness, winter is 91 days, no matter when they fall.

    TP: Very well, very well. So, February 29th every now and then?

    A: Yes, that's all.

Anyway, this is a Perl site, so the obligatory code snippet is

sub is_leap_year { my $year = shift; # We assume that validation has taken place. if ( $year % 400 == 0 ) { return 1; } elsif ( $year % 100 == 0 ) { return 0; } elsif ( $year % 4 == 0 ) { return 1; } else { return 0; } }
And I pray to the FSM that this code's right.

And .. someone below will no doubt chime in about the details of the Julian calendar .. you're quite right, I did absolutely no research on this (apart from my likely faulty memory), but I just thought it would have been an entertaining discussion about Changing The Calendar .. although that far back, it probably had far less impact on most people living their lives.

    Peasant #1: Oy! What day is it?

    Peasant #2: Haven't the faintest. Epiphany was a few days ago, so late January, early February is my guess. Why do you care?

    P1: Good point. Just curious .. I heard they were changing the calendar.

    P2: Madness. Change the calendar? Why?

Edit: Yeah, I got the code wrong. Fixed it already. ugh

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re: Leap years? How does that work?
by soonix (Canon) on Feb 02, 2022 at 15:46 UTC
    While your story hardly happened at the time you imagined, you might have hit the right place. Quite possibly, a roman priest (pre-christian) may have been involved in such a conversation:

    From what I have heard/read (I wasn't around at the time, so this is all hearsay, obviously) - both the Egyptians and the Mesoamerican people knew the correct year length (the Mesoamerican even at least as precise as our 365.24219 days) - not due to precise instruments, but through patient long-term observation. But they refrained from modifying anything in their calendar, for fear of invoking the wrath of some god or another - well, one Pharao tried, but his priests didn't follow.

    Much later (but earlier than any pope), the romans had a god named Terminus, who was the god of border stones and limits (yes, the word "terminal" is derived from his name, an airport terminal is at the boundary between "on the ground" and "up in the air", and someone terminally ill is at a boundary, too). The romans didn't even dare to move an altar dedicated to him (or maybe it was a mere border stone, as all border stones were kind of an altar for Terminus), at a place where a Zeus temple was about to be constructed, instead they left a hole in the roof of Zeus' temple, to fulfil the condition that Terminus' altar remain under the open sky.

    This god, as most others, had a festival, and his, of course, was under no circumstances to be moved, either. So, when the romans noticed that - with a fixed-number-of-days year length - the year would move through the seasons, they feared that might invoke the wrath of this god, and so they did opt to use leap days.

    In order to maintain the correct distance between the day of Terminus' festival, and both the beginning and the end of the year, they inserted any correction days (at least once even a whole month) at that specific date, which is the reason why most popes observed their leap days on February 24 (with any festivities between Feb.24 and Feb.28 in leap years moved by a day). Not before 1969 did the Pope have his leap day on February 29.

      I believe that's mostly baloney, starting with the assertion that the concept of terminal derives from the name of the god, whereas in fact it was the other way around.

      In any case, you've left out what I feel is a more important fact: that the ancient Roman calendar began with March. This is why February is the month with the oddball (and varying) number of days. (It is also why the names of the months of September, etc. make (or made) sense.)

      I refer all interested to this wikipedia article.

      I reckon we are the only monastery ever to have a dungeon staffed with 16,000 zombies.

        I thought the months started with January, because that month was named after the two-faced Roman god Janus, god of beginnings/endings and doors. (Janitor comes from Janus, for example.) There were ten months, finishing with December, the tenth month (the year's last four months are September, October, November, December -- months 7, 8, 9, and 10). Then Julius Caesar added two months in the summer, named after himself (Julius Augustus Caesar, hence July and August) to get to twelve months.

        Alex / talexb / Toronto

        Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

        whereas in fact it was the other way around
        Yes, that was my novelist alter ego going overboard with me ;-)

        But I stick to the theory that it were the Romans and their high priests who made leap years popular (see this other Wikipedia article). And i stick to the leap day being February 24.

        How could I forget the year started in March! Yes, I had read that, too, and it coincides with the fact that my favourite algorithm (from 1963) (partially explained here) (in ALGOL) starts out with
        if m > 2 then m := m - 3 else begin m := m + 9 y := y - 1 end
        thus re-scaling the year from 1=January .. 12=December to 0=March .. 11=February (only for easement of the calculation, not because of history, but that makes it even nicer)
Re: Leap years? How does that work?
by syphilis (Archbishop) on Feb 03, 2022 at 14:37 UTC
    Amusing stuff ++.
    I thought it might be fun to make the subroutine a little less clear:
    use warnings; use strict; for(1900, 2000, 2100, 2200, 2300, 2400, 1964, 1965, 1966, 1967, 1968) +{ print "$_\n" if is_leap_year($_); } sub is_leap_year { my $year = shift; return !($year % 400) || $year % 100 && !($year % 4); # Or: # return !($year % 400 && !($year % 100) || $year % 4); }
    I hope it's correct, at least.

    Cheers,
    Rob
Re: Leap years? How does that work?
by soonix (Canon) on Feb 03, 2022 at 19:34 UTC
    Yeah, I got the code wrong.
    Don't despair, Date::Leapyear is at version 1.72 and still has issues 😉
    (OK, novelist again, the "issues" seem to ask for a nicer license or so, and those version diffs that I looked at, contained documentation and tests. Don't despair, anyway)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://11141046]
Approved by Albannach
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-25 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found