Skip to content

Instantly share code, notes, and snippets.

@seancallaway
Created February 8, 2014 15:09
Show Gist options
  • Save seancallaway/8885137 to your computer and use it in GitHub Desktop.
Save seancallaway/8885137 to your computer and use it in GitHub Desktop.
Schwerdtfeger's Variation of Kraitchik's Algorithm Without Using Lookup Tables
enum day_of_week_t { SUN, MON, TUES, WED, THURS, FRI, SAT };
day_of_week_t calcDayOfWeek(unsigned int day, unsigned int month, unsigned int year)
{
int Y = 0, y = 0, c = 0, m = 0, d = 0, w = 0;
// Check for vaild arguments
year = year % 10000;
if (month < 1 || month > 12)
{
std::string error = "Invalid Month.";
//throw InvalidDateException(error); // TODO: Implement InvalidDateException
throw error;
}
if (day > daysInMonth(month))
{
std::string error = "Day exceeds days in Month.";
//throw InvalidDateException(error); // TODO: Implement InvalidDateException
throw error;
}
// Determine Y, which is the year minus 1 for January or February, and the year for the rest of the year
Y = year;
if (month < 3)
{
Y--;
}
// Determine y, which is the last 2 digits of Y
y = Y % 100;
// Determine c, which is the first 2 digits of Y
c = ((int)(Y / 100)) % 100;
// Determine d, which is the day of the month
d = day;
// Determine m, which is the month shifted left by 2 (March = 1,... February = 12)
m = ( (month - 2) < 1 ? month + 10 : month - 2 );
int temp = d + ((2.6f*m) - .2f) + y + (y / 4) + (c / 4) - (2*c);
w = temp % 7;
return (day_of_week_t)w;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment