Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

winapi: Refactor GetDayOfWeek function #671

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ryzee119
Copy link
Contributor

As mentioned in #670, GetDayOfWeek() function had an unusual code style and did not behave well with clang-format.

The notation for this function was originally a copy paste of the "Keith" method from https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week.

This PR refactors the function to a more standard format. The functionality has been verified against the original wiki implementation with the below code .

I changed the function inputs to INTs are that is what the alogirthm calls for. This was technically a bug.

#include <assert.h>
#include <windows.h>

static int keith (int y, int m, int d)
{
    return (d+=m<3 ? y-- : y-2,23*m/9+d+4+y/4-y/100+y/400) % 7;
}

static UCHAR GetDayOfWeek (INT year, INT month, INT day) {
    if (month < 3) {
        day += year;
        year--;
    } else {
        day += year - 2;
    }
    return (day + 23 * month / 9 + 4 + year / 4 - year / 100 + year / 400) % 7;
}

void test_me() {
  for (int year = 0; year < 9999; year++) {
      for(int month = 1; month <= 12; month++) {
          for(int day = 1; day <= 31; day++) {
              int dow = keith (year, month, day);
              UCHAR dow1 = GetDayOfWeek (year, month, day);
              assert(dow == dow1);
          }
      }
  }
}

int main()
{
  test_me();
  return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant