Calculating Day of the Week
For those of you playing along at home, you'll recall that our intrepid hero is working on a shell script that can tell you the most recent year that 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 a bit tricky, including the need to recognize when the specified date has already passed during the current year, because if it's July and we'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 is often quite easy to assemble. It's all those darn corner cases, those odd, improbable situations where the program needs to recognize and respond properly that makes programming a detail-oriented challenge. It can be fun, but then again, it can be exhausting and take weeks of debugging to ensure excellent coverage.
That's where we are with this script too. On months where the first day of the month is a Sunday, we're already set. Give me a numeric date, and I 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 is Day Of Month, and DOW is Day Of Week. May 3, 2011, has DOM=3 and DOW=3, as it's a Tuesday.
The cal utility shows this month like this:
May 2011
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Look! A perfectly formed month, so it's easy to figure out the DOW for once. But, that's not really fair for our testing, so let's move forward a month to June and look at June 3 instead. That's DOM=3, DOW=6 (Friday):
June 2011
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
The solution I'm going to work with is likely more complicated than necessary, but it's mine and I'm sticking with it.
Here's the idea. As awk goes through the lines, it easily can ascertain NF (number of fields). If NF < 7, we have a month where the first day starts on a DOW other than Sunday. Any matching date for the first week of June 2011, for example, would have NF = 4.
Look back at June though, because it's important to recognize that the last week of the month has a problem too. It has NF=5. Because any match in that line must have DOM > 7, however, we can address this nuance later. Stay tuned, as they say.
The formula we can use to calculate day of week for the first week of a month, however, given all this information and with i the day of the month is DOW=i+(7-NF). A few test cases verify that it works:
June 3 = i=3, NF=4 DOW=(7-4)+3 = 6
July 1 = i=1, NF=2 DOW=(7-2)+1 = 6
May 2 = i=2, NF=7 DOW=(7-7+2 = 2
For any date that doesn't occur on that first week, however, we can ignore all these complicated calculations and simply get the day of the week.
How do you tell if it's in the first week? Another test. Search for the matching DOM and then look at the matching line number. If it's not line 1, we have to calculate the day of week from the matching cal output line:
awk "/$expr/ { for (i=1;i<=NF;i++)
{ if (\$i~/${day}/) { print i }}}"
In my previous columns, I was creating this overly complicated regular expression to match all the edge cases (literally, the cases when the match was the first or last day of a week). Instead, here's a new plan that's faster and less complicated. We'll use sed to pad each calendar with leading and trailing spaces:
cal june 2011 | sed 's/^/ /;s/$/ /'
Now our regular expression to match just the specified date and no others is easy:
[^0-9]DATEINQUESTION[^0-9]
Further, awk easily can give us that NF value too, so here's a rough skeleton of the DOW function for a given day of the month, month and year:
figureDOM()
{
day=$1; caldate="$2 $3"
expr="[^0-9]${day}[^0-9]"
NFval=$(cal $caldate | sed 's/^/ /;s/$/ /' | \
awk "/$expr/ { print NF }")
DOW="$(( $day + ( 7 - $NFval ) ))"
}
That works if we search only for matches that are in the first week of the month, but that, of course, is unrealistic, so here's a better, more robust script:
figureDOW()
{
day=$1; caldate="$2 $3"
expr="[^0-9]${day}[^0-9]"
cal $caldate | sed 's/^/ /;s/$/ /' > $temp
NRval=$(cat $temp | awk "/$expr/ { print NR }")
NFval=$(cat $temp | awk "/$expr/ { print NF }")
if [ $NRval -eq 3 ] ; then
DOW="$(( $day + ( 7 - $NFval ) ))"
else
DOW=$(cat $temp | awk "/$expr/
{ for (i=1;i<=NF;i++) { if (\$i~/${day}/) { print i }}}")
fi
/bin/rm -f $temp
}
A few quick tests:
DOW of 3 june 2011 = 6
DOW of 1 july 2011 = 6
DOW of 2 may 2011 = 2
DOW of 16 may 2011 = 2
Looks good!
Next time, we'll tie this all together. We have a function that calculates day of week for a given date, we already have figured out how to parse user input to get a desired day of week for a specified month/day pair, and we know how to figure out if the starting point for our backward date search is the current year (for example, whether we're past that point in the year already).
Calendar image via Shutterstock.com.
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python
- myip
10 min 35 sec ago - Keeping track of IP address
2 hours 1 min ago - Roll your own dynamic dns
7 hours 14 min ago - Please correct the URL for Salt Stack's web site
10 hours 26 min ago - Android is Linux -- why no better inter-operation
12 hours 41 min ago - Connecting Android device to desktop Linux via USB
13 hours 10 min ago - Find new cell phone and tablet pc
14 hours 8 min ago - Epistle
15 hours 37 min ago - Automatically updating Guest Additions
16 hours 45 min ago - I like your topic on android
17 hours 32 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
date does DOW
This does it all with the current date:
date "+%u" (or date "+%a")
Or you can specify a date (MMDDHHMMYYYY) and use -j to prevent setting the date:
date -j 0530002012 "+%a"
However, just for the fun of it, back in 1985, I wrote this independent bash script (you pass it a date yyyymmdd and an optional second parameter "word" to get text instead of a digit):
#!/bin/bash
function f_dow { # yyyymmdd
# Copyright (c) Paul A. Scott, 1985, 2000, All rights reserved.
declare -a mtab=(6 2 2 5 0 3 5 1 4 6 2 4)
declare -a wtab=(sun mon tue wed thu fri sat)
yy=${1%????}
mm=${1#????}
mm=${mm%??}
dd=${1#??????}
cc=$(( $yy / 100 ))
dec=$(( $yy % 100 ))
weekday=$(( (($yy/4)-$cc)+($yy/400)+$dd+$yy+${mtab[$mm-1]} ))
if (( (($yy%400==0)||(($yy%4==0)&&($yy%100!=0))) )) ; then
if (( $mm<3 )) ; then
weekday=$(( $weekday-1 ))
fi
fi
case $2 in
word | [Ww][Oo][Rr][Dd])
echo "${wtab[$(($weekday%7))]}"
;;
*)
echo "$(($weekday%7))"
;;
esac
}
f_dow $1 $2
Forgot to mention . . .
The 'date' command solution was for BSD, but Linux 'date' has support with different syntax; consult the man pages for more info. The independent bash script runs anywhere.
check out rosettacode.org
solutions in many languages.
http://rosettacode.org/wiki/Day_of_the_week
greetings, eMBee.
Simple solution using the 'date' command
For any reader who would prefer a (much) simpler solution to the whole problem, I repeat the one I gave as a comment in the previous related post:
#!/bin/shdow=`date -d $1 +%a`
shift
count=0
if [ `date +%s` -lt `date -d "$*" +%s` ]
then
count=1
fi
until [ `date -d "$count year ago $*" +%a` = $dow ]
do
count=`expr $count + 1`
done
date -d "$count year ago" +%Y
Executions:
$ ./whatyear.sh THURSDAY february 24 # Notice the free formatting2011
$ ./whatyear.sh Sat Sep 15 # True this year... but in the future
2007
Easier if you use the korn shell
#!/bin/ksh93
DOW=$(printf '%(%w)T' "$1")
printf "DOW of $1 = $((++DOW))\n"
:)
Doomsday's algorithm!