svn commit: r321400 - head/sys/kern

Ian Lepore ian at FreeBSD.org
Sun Jul 23 21:28:02 UTC 2017


Author: ian
Date: Sun Jul 23 21:28:00 2017
New Revision: 321400
URL: https://svnweb.freebsd.org/changeset/base/321400

Log:
  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.

Modified:
  head/sys/kern/subr_clock.c

Modified: head/sys/kern/subr_clock.c
==============================================================================
--- head/sys/kern/subr_clock.c	Sun Jul 23 20:41:58 2017	(r321399)
+++ head/sys/kern/subr_clock.c	Sun Jul 23 21:28:00 2017	(r321400)
@@ -142,18 +142,27 @@ clock_ct_to_ts(struct clocktime *ct, struct timespec *
 {
 	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");


More information about the svn-src-head mailing list