Calculating Day of the Week

May 30, 2012 By Dave Taylor  inHOW-TOsprogramming

For those of you playing along at home, you'll recall that our intrepidhero is working on a shell script that can tell you the most recent yearthat a specific date occurred on a specified day of the week—for example,the most recent year when Christmas occurred on a Thursday.

There are, as usual, nuances and edge cases that make this calculation abit tricky, including the need to recognize when the specified date hasalready passed during the current year, because if it's July andwe're searching for the most recent May 1st that was on a Sunday,we'd miss 2011 if we just started in the previous year.

In fact, as any software developer knows, the core logic of your program isoften quite easy to assemble. It's all those darn corner cases, thoseodd, improbable situations where the program needs to recognize and respondproperly that makes programming a detail-oriented challenge. It can be fun,but then again, it can be exhausting and take weeks of debugging to ensureexcellent coverage.

That's where we are with this script too. On months where the first dayof the month is a Sunday, we're already set. Give me a numeric date, andI can tell you very quickly what day of the week it is. Unfortunately,that's only 1/7th of the possible month configurations.

What DOW Is That DOM?

For purposes of this discussion, let's introduce two acronyms: DOM isDay Of Month, and DOW is Day Of Week. May 3, 2011, has DOM

View the Original article

No comments:

Post a Comment

Thank You , For Immediate Assistance Plz Put Email Copy to Deviceporting@gmail.com