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.
Thank you so much.
I write nearly everything I do in awk and I needed the day of the week for my dates.
No probs. No idea if awks are Y2038 compliant. Of course I chickened out and only tested to 2035 …
Scruss,
I got a Sunday instead of Monday.
2020 MAR 16 SUNDAY
week_day = (day_of_week ? day_of_week : 7) – 1
line = day_of_week ” ” week_day
printf(string_nl, day_of_week)
1 0
Are you sure? Don’t see enough of your code to tell what you’re doing. It did pass a test harness I just wrote:
And yes, I gave it a quick Y2038 check by running from 2000-03-19 to 2040-03-19 (14611 days) with /usr/bin/awk.
Here’s the test harness bash script: