Subject: Re: RTC on a 345
To: None <port-hp300@netbsd.org>
From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
List: port-hp300
Date: 10/20/2001 03:03:19
In article <200110191716.TAA00414@Morgoth.>
ml@rz.uni-potsdam.de wrote:

> I just reinstalled 1.5.2 on my 345. I didn't touch it since 12/2000 ( no time... ) and it sait "WARNING: bad date in battery clock" now. Fine, the battery 
> is 7 years old so I replaced it. But the message still appears, so I have to set the time manually every time I reboot ( the clock has then the time of the 
> last shutdown ). What can I do about this ? I thought these y2k-related problems were sorted out long ago ? 

hp300/clock.c had year-2000 and year-2001 bugs.
They have been fixed in -current, but the fix cannot
be pulled up because it uses newer MI todr(9) functions.

Would you please try the attached patch for 1-5-release branch?
If it works, I'll send a request to pullup.
---
Izumi Tsutsui
tsutsui@ceres.dti.ne.jp

Index: clock.c
===================================================================
RCS file: /cvsroot/syssrc/sys/arch/hp300/hp300/clock.c,v
retrieving revision 1.21
diff -u -r1.21 clock.c
--- clock.c	1997/10/12 18:37:56	1.21
+++ clock.c	2001/10/19 17:54:02
@@ -396,17 +396,20 @@
 void
 resettodr()
 {
-	int i;
+	int i, year;
 	struct bbc_tm *tmptr;
 
 	tmptr = gmt_to_bbc(time.tv_sec);
+	year = tmptr->tm_year - 1900;
+	if (year >= 100)
+		year -= 100;
 
 	decimal_to_bbc(0, 1,  tmptr->tm_sec);
 	decimal_to_bbc(2, 3,  tmptr->tm_min);
 	decimal_to_bbc(4, 5,  tmptr->tm_hour);
 	decimal_to_bbc(7, 8,  tmptr->tm_mday);
 	decimal_to_bbc(9, 10, tmptr->tm_mon);
-	decimal_to_bbc(11, 12, tmptr->tm_year);
+	decimal_to_bbc(11, 12, year);
 
 	/* Some bogusness to deal with seemingly broken hardware. Nonsense */
 	bbc_registers[5] = ((tmptr->tm_hour / 10) & 0x03) + 8;
@@ -437,7 +440,7 @@
 	rt.tm_sec  = (hms % 3600) % 60;
 
 	/* Number of years in days */
-	for (i = STARTOFTIME - 1900; day >= days_in_year(i); i++)
+	for (i = STARTOFTIME; day >= days_in_year(i); i++)
 	  	day -= days_in_year(i);
 	rt.tm_year = i;
 	
@@ -451,7 +454,7 @@
 
 	/* Days are what is left over (+1) from all that. */
 	rt.tm_mday = day + 1;  
-	
+
 	return(&rt);
 }
 
@@ -475,11 +478,12 @@
 	day   = bbc_to_decimal(8, 7);
 	month = bbc_to_decimal(10, 9);
 	year  = bbc_to_decimal(12, 11) + 1900;
+	if (year < STARTOFTIME)
+		year += 100;
 
 	range_test(hour, 0, 23);
 	range_test(day, 1, 31);
 	range_test(month, 1, 12);
-	range_test(year, STARTOFTIME, 2000);
 
 	tmp = 0;
 
Index: clockreg.h
===================================================================
RCS file: /cvsroot/syssrc/sys/arch/hp300/hp300/clockreg.h,v
retrieving revision 1.5
diff -u -r1.5 clockreg.h
--- clockreg.h	1994/10/26 07:25:26	1.5
+++ clockreg.h	2001/10/19 17:54:02
@@ -119,7 +119,8 @@
 #define BBC_READ_REG	0xc3
 #define NUM_BBC_REGS	12
 
-#define	leapyear(year)		((year) % 4 == 0)
+#define	leapyear(year)		\
+	(((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0)
 #define	range_test(n, l, h)	if ((n) < (l) || (n) > (h)) return(0)
 #define	days_in_year(a) 	(leapyear(a) ? 366 : 365)
 #define	days_in_month(a) 	(month_days[(a) - 1])