Awk day of the week function

Yet another in my series of awk functions no-one but me will ever use:

function dow(year, month, day) {
# Modified from C Snippets "calsupp.c" public domain by Ray McVay 
# http://www8.cs.umu.se/~isak/snippets/calsupp.c
# returns 0-6 where 0 == sunday
# tested over 24000 days in range of unix timestamp, 1970-2035

    day_of_week = 0;

    if (month <= 2) {
        month += 12;
        year--;
    }
    day_of_week = (day + month * 2 + int(((month + 1) * 6) / 10) + year + int(year / 4) - int(year / 100) + int(year / 400) + 2);
    day_of_week = day_of_week % 7;
    return ((day_of_week ? day_of_week : 7) - 1);
}

Basically, all this does is calculate a Julian day number, then take its remainder modulo 7. I’d seen an example that parsed the output of ‘cal’. That’s one way of doing it; not necessarily mine.

Send the author to the moon!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>