Is the Moon Waxing or Waning?

In my last article, I talked about the complications of calculating the phase of the moon and decided simply to scrape the same website that Google uses.

That site provides the current moon illumination level, which lets you break it down into the phases of new moon, crescent, quarter, gibbous and full. Amateur astronomers know that the fun part of tracking the moon's phase is to understand whether it's "waxing" (growing more illuminated) or "waning" (growing less illuminated).

Although at any given moment the moon is illuminated based on its location, and your location, relative to the sun, the full cycle of a moon phase starts and ends with a new (0% illuminated) moon, and the full moon (100% illuminated) is the mid-point of the journey.

Therefore, to ascertain waxing or waning, all you need to do is know the moon's illumination level today and either yesterday or tomorrow. Fortunately, the Moon Giant website obligingly has the ability for you to ascertain the illumination level for a specific date.

A quick visit to the site with a regular web browser reveals that it works using a date-based URL format like this: http://www.moongiant.com/phase/11/6/2016.

So, you can build the date URL for the day before today with a call to the date program. If you've got the GNU version of date, it's easy to back up a day:


$ date
Mon Nov  7 11:40:31 MST 2016
$ date -v -1d
Sun Nov  6 11:40:15 MST 2016

It turns out that you also can specify that you want to back up 24 hours, although, of course, the net result is the same:


$ date -v -24H
Sun Nov  6 11:40:24 MST 2016

More important, you can pass date a format string that you then can evaluate with the eval function, so you can set month, day and year for yesterday in one easy step:


$ eval $( date -v -1d +"mon=%m day=%d year=%Y" )
$ echo month = $mon, day = $day and year $year
month = 11, day = 06 and year 2016

It's quite a handy trick when you need to work with extracting specific elements from date and 10x that when it also involves date math.

Older Date Programs Are More Complicated

But, what if your version of date doesn't include the -v flag and doesn't have all these fancy features? Then, my friend, you are facing a definite challenge. Date math is pretty easy, except for the edge cases.

That is, it's easy to extract the current month, day and year from even the most rudimentary Linux version of date, and it's obviously easy to subtract one from the day, but what if it's the first of the month? Or the first of the year?

That's doable too, but it's just a bit more work. Notably, you'll also want to know about leap years, because one day prior to March 1, 2016, might be February 28, or it might be February 29, depending on whether 2016 was a leap year.

Now a sneaky way to do it simply would be to sidestep the issue. If the day number of the month is greater than 1, subtract one to get yesterday's date. If it is the first, however, add one and reverse the logic of the waxing/waning test.

Fortunately, I do have the more sophisticated date program, so I'm going to do that most frustrating of things and leave this particular facet as the proverbial exercise for the reader.

______________________

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.