Archive for the ‘Engineering’ Category

Q: “Once in a blue moon” is a rare event. But what does “blue moon” really mean?

Monday, December 21st, 2009

calendar-iconThis was the final question last week at Pub Trivia, and our team won the evening with the same answer to this question that almost everyone else gave, probably the answer you’ve heard before: a blue moon means two full moons in one calendar month. This month, December 2009, has a blue moon on the 31, since it also had a full moon on the 2nd. But I had the nagging feeling that I’d read or heard somewhere (probably on QI) that the popular definition is wrong, that the real blue moon isn’t that straightforward. When I got home, Google confirmed it: We were wrong, quizmasters and all.

This idea that a month with two full moons is unusual points out the incongruity between the lunar cycle and our familiar 12 month, 365 day calendar. Month and Moon are nearly the same word–why don’t they mean the same thing? The answer, of course, is that they once did. But nature seems to hate an integer* as much as it reportedly hates a vacuum, so a year isn’t an even 365 or 366 days or 12 orbits of the moon around the Earth; neither does the moon take exactly 29 or 30 days to go around. So the problem is one of successive approximation, dividing one number by another and dealing with the remainder.

A first approximation: the lunar calendar

The moon was a useful timekeeper, despite its reluctance to synchronize with the cycles of the seasons. Instead of having to remember “day 237 after the winter solstice is wheat-planting day”, a farmer simply knew that the eighth moon is the wheat-planting moon. The wheat didn’t care much if one year’s full moon was 11 days earlier (or 18 days later) than the last year’s. Before the unveiling of the Julian calendar in 45 BC (more on that later) nearly all cultures on the planet used a calendar based on the cycles of the moon. (The Mesoamericans are a notable exception, and that 2012 thing is complete malarkey. Just saying.)

calendarjoke2

So here’s the first rounding error: Twelve cycles of the moon take between 354 and 355 days, but it’s 365 or 366 days between winter solstices. Or in other words, every two or three years will have thirteen full moons in it instead of twelve. And this is where the “blue” moon comes in, the moon that’s different from the others. (Though at this time, months and moons were the same thing so there’s not any question of having two of one in the other.) If you have a Stonehenge or other primitive-but-awesome celestial observatory handy (robed druids included) to tell you exactly when the solstice occurs you’ll notice that the phase of the moon and the solstice just about line up every 19 years. (But not exactly. Again, nature versus integers.) And even if you don’t, you’d eventually notice that, on average, 7 of every 19 cycles of the seasons need 13 months instead of 12 to keep everything in sync, or you’ll planting your wheat at the wrong time of year.

One interesting exception to this is the Islamic calendar, which has twelve lunar months and doesn’t adjust to match the seasons. This calendar is used for civil and religious purposes; a separate solar calendar must be used for agriculture. An Islamic, or hijri, year is on average 354.36 days long, so their calendar shifts forward by around 11 days each year relative to the seasons. Ramadan isn’t just on different dates every year (which would be more alarming to us if we didn’t have to deal with Easter), it crosses the seasons and returns to the same time of year every 33 years. This also means their years are counting up around 3% faster than seasonal years: It’s currently 1430 AH in the Islamic calendar, but in the year 20874 (AH and AD) their years will “catch up” with ours.

Next approximation: the solar calendar

In the Roman Republic the months were determined, as in most other cultures at the time, by priests (or imams, robed druids, etc.) who announced the presence of a new moon–and thus a new month–just after sunset. (The word “calendar” comes from the Latin calare “to call out, announce”, by way of kalends, the first day of the Roman month.) The Pontifex Maximus, high priest of the Roman religion, had the additional task of determining how long the year would be, 12 or 13 months, and therefore which day of the week important holidays would fall on–important stuff for a superstitious lot like the Romans. Originally, this was a purely religious role, but by the middle of the first century BC Roman politics had become particularly nasty and the Pontifex Maximus could use the calendar as a political tool, choosing to add an extra month to years when allies were in power or denying an extra month to his enemies.

To what extent Julius Caesar did this after becoming Pontifex Maximus in 63 BC isn’t clear, but 17 years later, in 46 BC, Caesar reformed the calendar, taking the power to define the year away from the position. The Julian reform declared that the Roman Republic would use a 365 day calendar with an extra day every 4 years. (Sound familiar?) In other words, the year was approximated at 365 1/4 days. To align the new calendar with the equinox, though, he had to add another two months to the already extended year, making 46 BC 445 days long. Cicero called this “the last year of confusion”.

One last tweak: our modern calendar

calendarjokeBy the end of the Middle Ages, it had become apparent that the Julian calendar had drifted away from the seasonal calendar. This distressed the Catholic Church terribly because the most important thing in the world for the Catholic Church was celebrating Easter on the correct day. It had spent the previous thousand years trying to sort it out and now it found that their calendar, the calendar that Caesar had set in motion 1600 years before, was missing the equinox by more than a week.

During this time, some very clever people had figured out that the Julian calendar was moving forward around 3 days every 400 years, and came up with a modification to the leap year scheme: Every 100 years would not be a leap year, except for every 400 years. (You may remember that 2000 was a double exception year–it wouldn’t have been a leap year, but it was. So if you weren’t paying attention, you didn’t really miss anything.) In 1582, Pope Gregory XIII announced a reform of the calendar, the new leap year scheme along with a 10-day shift to get the calendar synced back up with the seasons.

The Church and a number of Catholic countries adopted the new calendar on Friday, October 15th of 1582, one day after (Julian) Thursday, October 4th. Protestant Europe resisted this papist conspiracy until the 18th century; the British Empire (including the American colonies) saw Thursday, September 14th follow Wednesday the 2nd in 1752. Sweden tried to make a gradual change by ignoring leap years until their calendar matched the Gregorian, but lost its nerve and switched back to the Julian calendar after 12 years. To make up for a day gained in the interim, they added a 30th of February to 1712. In Alaska, the change took place in 1867 when US took possession of the territory from Russia, which still used the Julian calendar. Because the international date line shifted at the same time, Friday, October 6th was followed by another Friday, the 18th of October.

Time in the future

Modern astronomy tells us that a year is, to within one day in a million years, 365.242199 days long. The Julian calendar put 1 year as 365.25 days, for an error of 7.8 days per 1000 years. The Gregorian calendar put 1 year as 365.2425 days, for an error of 0.3 days every 1000 years. The astronomer Sir John Herschel proposed that the year 4000 and every 4,000th year after should not be a leap year in order to reduce this remaining error to about half a day every 10,000 years, but the standard has not yet been adopted. New calendars have been proposed (mostly to solve the problem of figuring out which day of the week any given date is) but the Gregorian calendar’s ordering of leap years is sufficient to keep the calendar aligned to the seasons as far into the future as we need.

Well, almost. At this scale we begin to see that the speed at which the Earth rotates on its axis isn’t constant, or even predictable. The pull of the moon’s gravity is slowing the rotation of the Earth, making each day longer by a fairly constant 2.3 milliseconds per century, but other factors, most notably shifting mass on and inside the planet, add to or subtract from this by a different amount every year. Since 1967, when the length of a second was standardized in precise atomic terms, days have been between 0.3 and 1 ms longer than the traditional 24 * 60 * 60 = 86,400 seconds. As a result, every once in a while astronomers add a “leap second” to UTC, Coordinated Universal Time. Surprisingly, while just about every year between 1972 and 1999 required a leap second to keep the clock synchronized with the Earth’s rotation, there have only been two leap seconds added in the last ten years. The Earth has sped up just a bit, and no one knows why.

Time in Cocoa

Since this is a Panic Engineering post, I should probably write something about calendars and programming. If you’re not a Cocoa programmer (and even if you are), feel free to skip this–it won’t be on the test.

First, here’s the code I used to find the year that the Islamic and Gregorian calendars meet. The NSCalendar and NSDateComponents classes were introduced in OS X 10.4 to replace NSCalendarDate, and are also available on the iPhone OS. They provide calendar operations such as converting NSDate objects to and from units of years, months, days, etc., and finding the difference between two NSDates. (Since we mentioned it above, it’s worth noting that as far as the OS is concerned every day is 86,400 seconds long. NSCalendar does not know about leap seconds.)

  1. NSCalendar* gregorian = [NSCalendar currentCalendar];
  2. NSCalendar* islamic = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
  3. NSDateComponents* components = [[NSDateComponents alloc] init];
  4.  
  5. [components setDay:1];
  6. [components setMonth:11];
  7. [components setYear:20874];
  8.  
  9. // Always use a format string in NSLog, even if it's just @"%@"!
  10. NSLog(@"%@", [gregorian dateFromComponents:components]);
  11. NSLog(@"%@", [islamic dateFromComponents:components]);

And here’s a snippet showing the jump from the Julian to the Gregorian calendar on October 15th, 1582. Be aware that the default NSCalendar uses the Julian calendar before this date. (For “fun”, check out October 1582 in iCal…)

  1. [components setDay:15];
  2. [components setMonth:10];
  3. [components setYear:1582];
  4.  
  5. NSDate* date = [gregorian dateFromComponents:components];
  6. NSLog(@"%@", date); // 1582-10-15 00:00:00 -0800
  7.  
  8. NSDateComponents* offset = [[NSDateComponents alloc] init];
  9. [offset setSecond:-1];
  10.  
  11. date = [gregorian dateByAddingComponents:offset toDate:date options:0];
  12. NSLog(@"%@", date); // 1582-10-04 23:59:59 -0800

OS X 10.4 provided Gregorian, Buddhist, Hebrew, Islamic, Islamic Civil, and Japanese calendars. 10.6 added Republic of China (for calendrical operations only, not formatting), Persian, Indian, and ISO8601, the traditional calendar of ISO8601istan. NSDateFormatter can format dates in any of these calendars:

  1. NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
  2. [formatter setDateStyle:NSDateFormatterFullStyle];
  3.  
  4. NSLog(@"Today's Gregorian date: %@", [formatter stringFromDate:[NSDate date]]);
  5.  
  6. NSCalendar* buddhist = [[NSCalendar alloc] initWithCalendarIdentifier:NSBuddhistCalendar];
  7. [formatter setCalendar:buddhist];
  8.  
  9. NSLog(@"Today's Buddhist date: %@", [formatter stringFromDate:[NSDate date]]);
  10.  
  11. NSCalendar* islamic = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
  12. [formatter setCalendar:islamic];
  13.  
  14. NSLog(@"Today's Islamic date: %@", [formatter stringFromDate:[NSDate date]]);

Blue Moons

The article that told me I was wrong about what “blue moon” means (and hilariously refers to that definition as “trendy”) explains that the “two full moons in a month” definition is due to a misreading of the Maine Farmers’ Almanac: the real Blue Moon is the third full moon in a season that has four full moons. Why the third? The first full moon of the season is particularly significant; e.g., the Easter moon. The moons before and after have names as well–the Lent Moon precedes the Easter Moon, whether it’s the third or fourth moon of the winter–so the third moon of four is the extra one. By this definition, 2009 does not have a blue moon, since the full moon on December 31st is after the solstice and belongs to next year’s winter.

For an example of the importance of the sequence of the moons to daily life, consider Charlemagne’s naming of the months of the year (a solar year, but even so), used for over 700 years after his death:

Wintarmanoth, winter month
Hornung, the month when the male red deer sheds its antlers
Lentzinmanoth, Lent month
Ostarmanoth, Easter month
Wonnemanoth, love-making month
Brachmanoth, plowing month
Heuvimanoth, hay month
Aranmanoth, harvest month
Witumanoth, wood month
Windumemanoth, wine month
Herbistmanoth, autumn harvest month
Heilagmanoth, holy month

One etymology claims “blue” comes from belewe, “betrayer”. Imagine if an extra moon showed up and delayed Wonnemanoth for 29 days. Betrayer moon!

A: The third of four full moons in a season

The next day I emailed the quizmaster to let her know how we were all terribly, horribly wrong, that we’d need to recount the scores, possibly have a do-over. She said they’d take either answer.

* A Correction

“Nature hates integers” is a gross oversimplification and ignores not just all of chemistry, where everything happens in even ratios, but the surprising phenomenon of orbital resonance. Pluto and Neptune, for example, are phase-locked in a stable 2:3 resonance: Pluto orbits the sun exactly twice for every three of Neptune’s orbits, and has for millions of years. Our moon’s rotation is locked to its orbit around the Earth due to tidal forces, which is why we only ever see one side of it. For the same reason, the Earth’s rotation is also slowing down and will eventually match the orbit of the moon. At that point, billions of years from now, both a day and a month will last for around 47 of our current 86,400-second-long days. One side of the Earth will face the moon; the other will never see it again.

Developer Color Picker 1.2

Monday, December 7th, 2009

Hey, Wade here.

A while ago I released Developer Color Picker, my Mac OS X color picker add-on that gives developer specific color readings.

I just added a couple of new features worth mentioning:

  • NSColor now supports HSB and device colors
  • UIColor now supports HSB
  • The picker will now remember the last used color mode

Hopefully these improvements will help ease your conversion from mockup to working application.