From f1b21e2c92936c63a67f7c4b81c04b3b7a85c05c Mon Sep 17 00:00:00 2001 From: Ian Lepore Date: Sun, 23 Jul 2017 21:28:00 +0000 Subject: [PATCH] Add common code to support realtime clocks that store year without century. Most realtime clocks store the year as 2 BCD digits. Some add a century bit to extend the range another hundred years. Every clock driver has its own code to determine the century and pass a full year value to clock_ct_to_ts(). Now clock drivers can just convert BCD to bin and store the result in the clocktime struct and let the common code figure out the century. Clocks with a century bit can just add 100 to year if the century bit is on. --- sys/kern/subr_clock.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/sys/kern/subr_clock.c b/sys/kern/subr_clock.c index 298e42494cf7..50d8e1059205 100644 --- a/sys/kern/subr_clock.c +++ b/sys/kern/subr_clock.c @@ -142,18 +142,27 @@ clock_ct_to_ts(struct clocktime *ct, struct timespec *ts) { int i, year, days; - year = ct->year; - if (ct_debug) { printf("ct_to_ts("); print_ct(ct); printf(")"); } + /* + * Many realtime clocks store the year as 2-digit BCD; pivot on 70 to + * determine century. Some clocks have a "century bit" and drivers do + * year += 100, so interpret values between 70-199 as relative to 1900. + */ + year = ct->year; + if (year < 70) + year += 2000; + else if (year < 200) + year += 1900; + /* Sanity checks. */ if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 || ct->day > days_in_month(year, ct->mon) || - ct->hour > 23 || ct->min > 59 || ct->sec > 59 || + ct->hour > 23 || ct->min > 59 || ct->sec > 59 || year < 1970 || (sizeof(time_t) == 4 && year > 2037)) { /* time_t overflow */ if (ct_debug) printf(" = EINVAL\n");