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
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Validate an E-Mail Address with PHP, the Right Way
- Introduction to MapReduce with Hadoop on Linux
- RSS Feeds
- Weechat, Irssi's Little Brother
- New Products
- Developer Poll
- Reply to comment | Linux Journal
1 hour 37 min ago - Reply to comment | Linux Journal
2 hours 23 min ago - Didn't read
2 hours 33 min ago - Reply to comment | Linux Journal
2 hours 38 min ago - Poul-Henning Kamp: welcome to
4 hours 48 min ago - This has already been done
4 hours 49 min ago - Reply to comment | Linux Journal
5 hours 34 min ago - Welcome to 1998
6 hours 23 min ago - notifier shortcomings
6 hours 46 min ago - heroku?
8 hours 23 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
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!