rtc: Fix rtc_calc_weekday

This function appeared previously unused (called only from rtc_display, also unused),
but it returned an incorrect weekday.  Change the algorithm to use Zeller's Rule, a
well-known algorithm for calculuating weekdays.

Change-Id: Ibce6822942f8d9d9f39c2b6065cd785dca9e8e09
Signed-off-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Reviewed-on: https://review.coreboot.org/c/31557
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Tim Wawrzynczak 2019-02-21 15:02:26 -07:00 committed by Patrick Georgi
parent ec645cb086
commit b47c633c31
1 changed files with 22 additions and 38 deletions

View File

@ -27,50 +27,34 @@
#define DAYS_IN_YEAR(a) (LEAP_YEAR(a) ? 366 : 365) #define DAYS_IN_YEAR(a) (LEAP_YEAR(a) ? 366 : 365)
#define DAYS_IN_MONTH(a) (month_days[(a) - 1]) #define DAYS_IN_MONTH(a) (month_days[(a) - 1])
static const int month_offset[] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
static const char *const weekdays[] = { static const char *const weekdays[] = {
"Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur"
}; };
static int leaps_to_year(int year) /* Zeller's rule */
{
return year / 4 - year / 100 + year / 400;
}
/* This only works for the Gregorian calendar after Jan 1 1971. */
static int rtc_calc_weekday(struct rtc_time *tm) static int rtc_calc_weekday(struct rtc_time *tm)
{ {
int leaps_to_date;
int day;
if (tm->year < 1971) if (tm->year < 1971)
return -1; return -1;
day = 4; /* Jan 1 1970 was a Thursday. */ /* In Zeller's rule, January and February are treated as if they
are months 13 and 14 of the previous year (March is still month 3) */
/* Number of leap corrections to apply up to end of last year */ const int zyear = ((tm->mon < 3) ? tm->year - 1 : tm->year);
leaps_to_date = leaps_to_year(tm->year - 1) - leaps_to_year(1970); const int q = tm->mday;
const int m = (tm->mon < 3) ? tm->mon + 12 : tm->mon;
const int K = zyear % 100;
const int J = zyear / 100;
/* /*
* This year is a leap year if it is divisible by 4 except when it is * Because of the way the modulo operator works with negative numbers,
* divisible by 100 unless it is divisible by 400 * the traditional formulation of Zeller's rule must be modified
* * slightly to make the numerator positive (i.e., add 5J instead of
* e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 is. * subtracting 2J). Also subtract 1 so that Sunday is day 0.
*/ */
if ((tm->year % 4) && const int h = (q + (13 * (m + 1)) / 5
((tm->year % 100 != 0) || (tm->year % 400 == 0)) && + K + (K / 4) + (J / 4) + (5 * J) - 1) % 7;
(tm->mon > 2)) {
/* We are past Feb. 29 in a leap year */
day++;
}
day += (tm->year - 1970) * 365 + leaps_to_date +
month_offset[tm->mon-1] + tm->mday;
tm->wday = day % 7;
tm->wday = h;
return 0; return 0;
} }
@ -147,7 +131,7 @@ unsigned long rtc_mktime(const struct rtc_time *tm)
void rtc_display(const struct rtc_time *tm) void rtc_display(const struct rtc_time *tm)
{ {
printk(BIOS_INFO, "Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n", printk(BIOS_INFO, "Date: %5d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
tm->year, tm->mon, tm->mday, tm->year, tm->mon, tm->mday,
(tm->wday < 0 || tm->wday > 6) ? "unknown " : weekdays[tm->wday], (tm->wday < 0 || tm->wday > 6) ? "unknown " : weekdays[tm->wday],
tm->hour, tm->min, tm->sec); tm->hour, tm->min, tm->sec);