# dayofweek(yr, mo, day) takes a date in (yr, mo, day) format and
# returns an integer in range 0-6.
#
# Table: 0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday,
# 5=Friday, 6=Saturday
#
# Copyright 2013 by Charles Thayer
#
# Released under Creative Commons Attribution-ShareAlike 3.0 Unported
# http://creativecommons.org/licenses/by-sa/3.0/
define dayofweek(yr, mo, day) {
scale = 0
# Helper for days each month in normal year
daysmo[0] = 31; daysmo[1] = 28; daysmo[2] = 31;
daysmo[3] = 30; daysmo[4] = 31; daysmo[5] = 30;
daysmo[6] = 31; daysmo[7] = 31; daysmo[8] = 30;
daysmo[9] = 31; daysmo[10] = 30; daysmo[11] = 31;
# Doomsday Anchor (adjusted later for leap years)
feb_last = 59;
# Get day of year by adding days for all months preceding 'mo'
# January = 1 adds 0, February = 2 adds 31, ...
# Notice that daysmo[11] is superfluous.
day_ytd = day
for (i=0; i < (mo - 1); i++) day_ytd += daysmo[i];
# Start calculating Doomsday by getting century anchor
century = yr / 100;
doomsday = (16 - 2 * (century % 4)) % 7;
# Now get the offset for the year in the century
year = yr % 100;
doomsday += (year + year / 4) % 7;
doomsday %= 7;
# Leap years: Start by checking for yr = 4*N
leap = ((year % 4) == 0);
# Leap exception if XX00
leap_except = ((century % 4) == 0);
# Check for leapness
if (year == 0) leap = leap_except;
# Adjust for leap day after Doomsday
if (day_ytd > feb_last) day_ytd += leap;
feb_last += leap;
# Return the dayofweek value
# Return the dayofweek value
ans = (420 + day_ytd - feb_last + doomsday) % 7;
"Day of the week is offset number: "; ans
tmp = getdayname(ans);
return ans;
}
# dayofweek2(yr, mo, day) takes a date in (yr, mo, day) format and
# returns an integer in range 0-6.
#
# Table: 0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday,
# 5=Friday, 6=Saturday
define dayofweek2(yr, mo, day) {
scale = 0
# Start calculating Doomsday by getting century anchor
century = yr / 100;
doomsday = (16 - 2 * (century % 4)) % 7;
# Now get the offset for the year in the century
year = yr % 100;
doomsday += (year + year / 4) % 7;
doomsday %= 7;
# Leap years: Start by checking for yr = 4*N
leap = ((year % 4) == 0);
# Leap exception if XX00
leap_except = ((century % 4) == 0);
# Check for leapness
if (year == 0) leap = leap_except;
# Doomsday Anchor (adjusted later for leap years)
# Default: Doomsday for a month is the month as a number
# - Works for 4/4, 6/6, 8/8, 10/10, 12/12
month_pos = mo;
# Handle odd months after Doomsday: 7 March, 9 May, 11 July
if ((mo >= 3) && ((mo % 2) > 0)) {
month_pos += 4;
# Adding +4 doesn't work for 5 September, 7 November
# - Gives 13 September and 15 November
# - Since 12 September and 14 November are close enough,
# just subtract 1 from calculated offsets.
if (mo > 7) --month_pos;
}
# Handle January and February
if (mo < 3) {
# Add 6 - 3*mo + leap: January = 3 or 4, February = 0 or 1
month_pos = 6 - 3 * mo + leap;
}
# Return the dayofweek value
ans = (35 + day - month_pos + doomsday) % 7;
"Day of the week is offset number: "; ans
tmp = getdayname(ans);
return ans;
}
# Cheat to get a string out of the dayofweek() functions
define getdayname(ioffset) {
if (ioffset == 0) {
"Sunday
"
return 0;
}
if (ioffset == 1) {
"Monday
"
return 0;
}
if (ioffset == 2) {
"Tuesday
"
return 0;
}
if (ioffset == 3) {
"Wednesday
"
return 0;
}
if (ioffset == 4) {
"Thursday
"
return 0;
}
if (ioffset == 5) {
"Friday
"
return 0;
}
# Default for any other value
"Saturday
"
return 0;
}