{"id":74,"date":"2007-01-26T08:37:17","date_gmt":"2007-01-26T13:37:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=74"},"modified":"2007-04-05T06:01:21","modified_gmt":"2007-04-05T11:01:21","slug":"calendars-and-leap-years","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/01\/26\/calendars-and-leap-years\/","title":{"rendered":"Calendars and Leap Years"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I'm pleased to introduce Ned Gulley as this week's guest blogger. Ned is a software developer with an amateur's interest in\r\n         astronomy. He was motivated by a recent discussion of numerical precision in this space to put together this piece on the\r\n         precision of calendars.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#2\">The Egyptian calendar<\/a><\/li>\r\n         <li><a href=\"#4\">The Julian calendar<\/a><\/li>\r\n         <li><a href=\"#6\">The Gregorian calendar<\/a><\/li>\r\n         <li><a href=\"#8\">The modern calendar<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>If you pile up enough of them, small errors can turn into big problems. For instance: how long is a year? More specifically,\r\n      if you ignored the leap day every four years, how long would it take to notice something was wrong?\r\n   <\/p>\r\n   <p>We measure time according to the motion of three different bodies in the Solar system: sun, moon, and earth. The rotation\r\n      of the earth around its own axis relative to the sun is the basis for our day, the rotation of the earth around the sun defines\r\n      the year, and the rotation of the moon around the earth relative to the sun defines the month (at least it did originally).\r\n      These motions are essentially unrelated, but we try to fit them all into a single calendar anyway.\r\n   <\/p>\r\n   <p>Historically we have defined the day as the amount of time from noon to noon. This is relatively easy to measure. We could\r\n      measure the year this same way: the amount of time from, say, solstice to solstice. But we don't do this. Instead we try to\r\n      compute it by counting the number of days. This causes all kinds of problems.\r\n   <\/p>\r\n   <p>We wish that the number of days in a year was a nice round number, but it isn't, and there's no reason to expect it to be.\r\n      It's as if you were to pick two numbers at random and expect one to cleanly divide the other. Over the centuries, we've managed\r\n      to \"fix up\" the calendar of days with leap days so it fits a solar year. Lunar calendars are even more of a mess and require\r\n      great exertions to keep on track with a solar year. The Hebrew calendar is lunar, and it periodically inserts extra months\r\n      to make up the shortfall. The Islamic calendar is also lunar, but it does not attempt to stay synchronized with the solar\r\n      year. Thus the month of Ramadan, for example, moves slowly through the seasons with each passing year, sometimes occurring\r\n      in the summer and sometimes in the winter.\r\n   <\/p>\r\n   <h3>The Egyptian calendar<a name=\"2\"><\/a><\/h3>\r\n   <p>So how many days are in a year? It's not exactly 365, but it's close. Does the difference matter? Let's investigate. The length\r\n      of a seasonal year is 365.24219878 days (more or less). Consider a primitive calendar exactly 365 days long with no leap years.\r\n      Such a calendar was actually used by the ancient Egyptians.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">seasYearLen = 365.24219878;\r\nseasYearRem = seasYearLen - 365;\r\n\r\nn = 0:1:2000;\r\n\r\n<span style=\"color: #228B22\">% The Egyptian calendar falls behind by the remainder every year<\/span>\r\negyptDev = -n*seasYearRem;\r\n\r\nplot(n,egyptDev)\r\nxlabel(<span style=\"color: #A020F0\">'years'<\/span>)\r\nylabel(<span style=\"color: #A020F0\">'accumulated error in days'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'Deviation between calendar and season'<\/span>)\r\n\r\nline([0 n(end)],-seasYearLen*[1 1],<span style=\"color: #A020F0\">'Color'<\/span>,<span style=\"color: #A020F0\">'red'<\/span>)\r\ncrossover = seasYearLen\/seasYearRem;\r\nline(crossover,-seasYearLen,<span style=\"color: #A020F0\">'Marker'<\/span>,<span style=\"color: #A020F0\">'o'<\/span>,<span style=\"color: #A020F0\">'Color'<\/span>,<span style=\"color: #A020F0\">'red'<\/span>)\r\nannotation(gcf,<span style=\"color: #A020F0\">'textarrow'<\/span>,[0.7036 0.7161],[0.549 0.3595],<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'TextEdgeColor'<\/span>,<span style=\"color: #A020F0\">'none'<\/span>,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'String'<\/span>,{<span style=\"color: #A020F0\">'Accumulated error'<\/span>,<span style=\"color: #A020F0\">'equals one year!'<\/span>});<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/74\/nedCalendar_01.png\"> <p>Without the leap year correction, every four years the calendar year falls behind the seasons by one day.  After 754 years,\r\n      the seasons would be 180 degrees out of phase with the calendar. The first day of autumn in the northern hemisphere would\r\n      be March 21st instead of September 21st. After 1508 years, the calendar would go all the way around the seasons and would\r\n      once again be synchronized with the sun.\r\n   <\/p>\r\n   <h3>The Julian calendar<a name=\"4\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clf\r\nn = 0:10000;\r\negyptDev = -n*seasYearRem;\r\negyptHandle = plot(n,egyptDev);\r\nxlabel(<span style=\"color: #A020F0\">'years'<\/span>)\r\nylabel(<span style=\"color: #A020F0\">'accumulated error in days'<\/span>)\r\naxis([0 200 -5 5])\r\n\r\njulDev = egyptDev + floor(n\/4);\r\njulHandle = line(n,julDev,<span style=\"color: #A020F0\">'Color'<\/span>,<span style=\"color: #A020F0\">'red'<\/span>);\r\nline( <span style=\"color: #0000FF\">...<\/span>\r\n    [0 length(n) NaN 0 length(n)], <span style=\"color: #0000FF\">...<\/span>\r\n    [1 1 NaN -1 -1], <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'LineStyle'<\/span>,<span style=\"color: #A020F0\">':'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'Color'<\/span>,0.4*[1 1 1])\r\nlegend([egyptHandle julHandle], <span style=\"color: #0000FF\">...<\/span>\r\n    {<span style=\"color: #A020F0\">'Egyptian'<\/span>,<span style=\"color: #A020F0\">'Julian'<\/span>}, <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'NorthEast'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/74\/nedCalendar_02.png\"> <p>Adding a leap year day every four years helps, but as you can see there is still some significant drift. After 200 years we're\r\n      already a day out of synch, but in the other direction. We've made the calendar year longer than it ought to be.\r\n   <\/p>\r\n   <h3>The Gregorian calendar<a name=\"6\"><\/a><\/h3>\r\n   <p>The Gregorian calendar, named after calendar reformer Pope Gregory XIII, solves this problem by leaving out three Julian leap-days\r\n      every 400 years. Algorithmically, if the year is divisible by 100, avoid the leap-day UNLESS the year is divisible by 400.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">gregintDev = julDev - floor(n\/100);\r\n\r\ngregDev = gregintDev + floor(n\/400);\r\n<span style=\"color: #228B22\">% line(n, gregintDev,'Color','magenta')<\/span>\r\ngregHandle = line(n, gregDev,<span style=\"color: #A020F0\">'Color'<\/span>,<span style=\"color: #A020F0\">'green'<\/span>);\r\naxis([0 600 -20 20])\r\nlegend([egyptHandle julHandle gregHandle], <span style=\"color: #0000FF\">...<\/span>\r\n    {<span style=\"color: #A020F0\">'Egyptian'<\/span>,<span style=\"color: #A020F0\">'Julian'<\/span>,<span style=\"color: #A020F0\">'Gregorian'<\/span>}, <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'NorthEast'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/74\/nedCalendar_03.png\"> <p>Zoom in and you can see that after 600 years or so we have drifted by a day.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">axis([0 600 -5 5])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/74\/nedCalendar_04.png\"> <h3>The modern calendar<a name=\"8\"><\/a><\/h3>\r\n   <p>The modern calendar does one final tweak on this: if the year is divisible by 4000, leave out the leap-day despite the Gregorian\r\n      recommendation above.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">modernDev = gregDev - floor(n\/4000);\r\nmodernHandle = line(n, modernDev,<span style=\"color: #A020F0\">'Color'<\/span>,<span style=\"color: #A020F0\">'black'<\/span>);\r\naxis([0 10000 -20 20])\r\n\r\nlegend([egyptHandle julHandle gregHandle modernHandle], <span style=\"color: #0000FF\">...<\/span>\r\n    {<span style=\"color: #A020F0\">'Egyptian'<\/span>,<span style=\"color: #A020F0\">'Julian'<\/span>,<span style=\"color: #A020F0\">'Gregorian'<\/span>,<span style=\"color: #A020F0\">'Modern'<\/span>}, <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'NorthEast'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/74\/nedCalendar_05.png\"> <p>To account for variation beyond this, we insert leap seconds, so we should be in good shape for a long time to come.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_be6a7eeba2ec42f29db18a3ef5f19d31() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='be6a7eeba2ec42f29db18a3ef5f19d31 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' be6a7eeba2ec42f29db18a3ef5f19d31';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_be6a7eeba2ec42f29db18a3ef5f19d31()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.3<br><\/p>\r\n<\/div>\r\n<!--\r\nbe6a7eeba2ec42f29db18a3ef5f19d31 ##### SOURCE BEGIN #####\r\n%% Calendars and Leap Years\r\n%\r\n% I'm pleased to introduce Ned Gulley as this week's guest blogger. Ned is\r\n% a software developer with an amateur's interest in astronomy. He was\r\n% motivated by a recent discussion of numerical precision in this space to\r\n% put together this piece on the precision of calendars.\r\n\r\n%%\r\n% If you pile up enough of them, small errors can turn into big problems.\r\n% For instance: how long is a year? More specifically, if you ignored\r\n% the leap day every four years, how long would it take to notice something\r\n% was wrong? \r\n%\r\n% We measure time according to the motion of three different bodies in the\r\n% Solar system: sun, moon, and earth. The rotation of the earth around its\r\n% own axis relative to the sun is the basis for our day, the rotation of\r\n% the earth around the sun defines the year, and the rotation of the moon\r\n% around the earth relative to the sun defines the month (at least it did\r\n% originally). These motions are essentially unrelated, but we try to fit\r\n% them all into a single calendar anyway.\r\n%\r\n% Historically we have defined the day as the amount of time from noon to\r\n% noon. This is relatively easy to measure. We could measure the year this\r\n% same way: the amount of time from, say, solstice to solstice. But we\r\n% don't do this. Instead we try to compute it by counting the number of\r\n% days. This causes all kinds of problems.\r\n% \r\n% We wish that the number of days in a year was a nice round number, but it\r\n% isn't, and there's no reason to expect it to be. It's as if you were to\r\n% pick two numbers at random and expect one to cleanly divide the other.\r\n% Over the centuries, we've managed to \"fix up\" the calendar of days with\r\n% leap days so it fits a solar year. Lunar calendars are even more of a\r\n% mess and require great exertions to keep on track with a solar year. The\r\n% Hebrew calendar is lunar, and it periodically inserts extra months to\r\n% make up the shortfall. The Islamic calendar is also lunar, but it does\r\n% not attempt to stay synchronized with the solar year. Thus the month of\r\n% Ramadan, for example, moves slowly through the seasons with each passing\r\n% year, sometimes occurring in the summer and sometimes in the winter.\r\n\r\n\r\n%% The Egyptian calendar\r\n% So how many days are in a year? It's not exactly 365, but it's close.\r\n% Does the difference matter? Let's investigate. The length of a seasonal\r\n% year is 365.24219878 days (more or less). Consider a primitive calendar\r\n% exactly 365 days long with no leap years. Such a calendar was actually\r\n% used by the ancient Egyptians.\r\n\r\nseasYearLen = 365.24219878;\r\nseasYearRem = seasYearLen - 365;\r\n\r\nn = 0:1:2000;\r\n\r\n% The Egyptian calendar falls behind by the remainder every year\r\negyptDev = -n*seasYearRem;\r\n\r\nplot(n,egyptDev)\r\nxlabel('years')\r\nylabel('accumulated error in days')\r\ntitle('Deviation between calendar and season') \r\n\r\nline([0 n(end)],-seasYearLen*[1 1],'Color','red')\r\ncrossover = seasYearLen\/seasYearRem;\r\nline(crossover,-seasYearLen,'Marker','o','Color','red')\r\nannotation(gcf,'textarrow',[0.7036 0.7161],[0.549 0.3595],...\r\n    'TextEdgeColor','none',...\r\n    'String',{'Accumulated error','equals one year!'});\r\n\r\n%%\r\n% Without the leap year correction, every four years the calendar year\r\n% falls behind the seasons by one day.  After 754 years, the seasons would\r\n% be 180 degrees out of phase with the calendar. The first day \r\n% of autumn in the northern hemisphere would be March 21st instead of\r\n% September 21st. After 1508 years, the calendar would go all the way\r\n% around the seasons and would once again be synchronized with the sun.\r\n\r\n%% The Julian calendar\r\n\r\nclf\r\nn = 0:10000;\r\negyptDev = -n*seasYearRem;\r\negyptHandle = plot(n,egyptDev);\r\nxlabel('years')\r\nylabel('accumulated error in days')\r\naxis([0 200 -5 5])\r\n\r\njulDev = egyptDev + floor(n\/4);\r\njulHandle = line(n,julDev,'Color','red');\r\nline( ...\r\n    [0 length(n) NaN 0 length(n)], ...\r\n    [1 1 NaN -1 -1], ...\r\n    'LineStyle',':', ...\r\n    'Color',0.4*[1 1 1])\r\nlegend([egyptHandle julHandle], ...\r\n    {'Egyptian','Julian'}, ...\r\n    'Location','NorthEast')\r\n\r\n%%\r\n% Adding a leap year day every four years helps, but as you can see there\r\n% is still some significant drift. After 200 years we're already a day out\r\n% of synch, but in the other direction. We've made the calendar year longer\r\n% than it ought to be.\r\n\r\n%% The Gregorian calendar\r\n% The Gregorian calendar, named after calendar reformer Pope Gregory XIII,\r\n% solves this problem by leaving out three Julian leap-days every 400\r\n% years. Algorithmically, if the year is divisible by 100, avoid the\r\n% leap-day UNLESS the year is divisible by 400.\r\n\r\ngregintDev = julDev - floor(n\/100);\r\n\r\ngregDev = gregintDev + floor(n\/400);\r\n% line(n, gregintDev,'Color','magenta')\r\ngregHandle = line(n, gregDev,'Color','green');\r\naxis([0 600 -20 20])\r\nlegend([egyptHandle julHandle gregHandle], ...\r\n    {'Egyptian','Julian','Gregorian'}, ...\r\n    'Location','NorthEast')\r\n\r\n%%\r\n% Zoom in and you can see that after 600 years or so we have drifted by a\r\n% day.\r\naxis([0 600 -5 5])\r\n\r\n\r\n%% The modern calendar\r\n% The modern calendar does one final tweak on this: if the year is\r\n% divisible by 4000, leave out the leap-day despite the Gregorian\r\n% recommendation above.\r\n\r\nmodernDev = gregDev - floor(n\/4000);\r\nmodernHandle = line(n, modernDev,'Color','black');\r\naxis([0 10000 -20 20])\r\n\r\nlegend([egyptHandle julHandle gregHandle modernHandle], ...\r\n    {'Egyptian','Julian','Gregorian','Modern'}, ...\r\n    'Location','NorthEast')\r\n\r\n%%\r\n% To account for variation beyond this, we insert leap seconds, so we\r\n% should be in good shape for a long time to come.\r\n\r\n##### SOURCE END ##### be6a7eeba2ec42f29db18a3ef5f19d31\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I'm pleased to introduce Ned Gulley as this week's guest blogger. Ned is a software developer with an amateur's interest in\r\n         astronomy. He was motivated by a recent discussion... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/01\/26\/calendars-and-leap-years\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/74"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}