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

Oktoberfest example uses improper index to lookup days per month #10

Open
clayheaton opened this issue Nov 7, 2018 · 1 comment
Open

Comments

@clayheaton
Copy link

The Oktoberfest example has this lookup:

int[] daysPerMonth = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] daysPerMonthLeapYear = new int[] {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

and this function:

// Not really the exact date, but it's ok for this example
float getExactDate(int year, int month, int day) {
  boolean leapYear = false;

  if (year % 400 == 0) {
    leapYear = true;
  }
  else if (year % 100 == 0) {
    leapYear = false;
  }
  else if (year % 4 == 0) {
    leapYear = true;
  }

  if (leapYear) {
    return year + (month + (day - 1f)/daysPerMonthLeapYear[month])/12f;
  }
  else {
    return year + (month + (day - 1f)/daysPerMonth[month])/12f;
  }
}

The problem is that the return values from the function lookup the month instead of month-1, which leads to grabbing an incorrect value from the lookups. This can be fixed with this change to the function:

// Not really the exact date, but it's ok for this example
float getExactDate(int year, int month, int day) {
  boolean leapYear = false;

  if (year % 400 == 0) {
    leapYear = true;
  }
  else if (year % 100 == 0) {
    leapYear = false;
  }
  else if (year % 4 == 0) {
    leapYear = true;
  }

  if (leapYear) {
    return year + (month + (day - 1f)/daysPerMonthLeapYear[month-1])/12f;
  }
  else {
    return year + (month + (day - 1f)/daysPerMonth[month-1])/12f;
  }
}
@clayheaton
Copy link
Author

Well... I see that the source data for the example has 0 as January... Anyhow, for anybody who finds this, if you are using 1 as january, then the fix I proposed addresses the issue.

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

No branches or pull requests

1 participant