Eliminate the 2038 year check, because clock_ymdhms_to_sec already performs

that check.  So instead, we test to make sure we have a non-negative second
after the conversion.

While here, we also add a check to round the second counter up when setting
time if we are more than half-way into the second.
This commit is contained in:
gdamore 2006-09-07 04:51:42 +00:00
parent 68c7194a9c
commit 8e82fb3308
1 changed files with 15 additions and 16 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_todr.c,v 1.9 2006/09/07 04:24:26 gdamore Exp $ */
/* $NetBSD: kern_todr.c,v 1.10 2006/09/07 04:51:42 gdamore Exp $ */
/*
* Copyright (c) 1992, 1993
@ -76,7 +76,7 @@
* @(#)clock.c 8.1 (Berkeley) 6/10/93
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.9 2006/09/07 04:24:26 gdamore Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.10 2006/09/07 04:51:42 gdamore Exp $");
#include <sys/param.h>
@ -257,20 +257,16 @@ todr_gettime(todr_chip_handle_t tch, struct timeval *tvp)
if (tch->todr_gettime_ymdhms) {
if ((rv = tch->todr_gettime_ymdhms(tch, &dt)) != 0)
return rv;
/*
* If tv_sec is 32 bits, then the "End of Time" is Mon
* Jan 18 22:14:07 2038 (US/Eastern) This code copes
* with RTC's past the end of time if tv_sec is an
* int32 or less. Needed because sometimes RTCs screw
* up or are badly set, and that would cause the time
* to go negative in the calculation below, which
* causes Very Bad Mojo. This at least lets the user
* boot and fix the problem. Note the code is self
* eliminating once tv_sec goes to 64 bits.
* Formerly we had code here that explicitly checked
* for 2038 year rollover.
*
* However, clock_ymdhms_to_secs performs the same
* check for us, so we need not worry about it. Note
* that this assumes that the tvp->tv_sec member is is
* a time_t.
*/
if (/* CONSTCOND */ sizeof(tvp->tv_sec) <= sizeof(int32_t))
if (dt.dt_year >= 2038)
return -1;
/* simple sanity checks */
if (dt.dt_mon > 12 || dt.dt_day > 31 ||
@ -279,7 +275,7 @@ todr_gettime(todr_chip_handle_t tch, struct timeval *tvp)
tvp->tv_sec = clock_ymdhms_to_secs(&dt) + rtc_offset * 60;
tvp->tv_usec = 0;
return 0;
return tvp->tv_sec < 0 ? -1 : 0;
}
return -1;
@ -294,7 +290,10 @@ todr_settime(todr_chip_handle_t tch, volatile struct timeval *tvp)
return tch->todr_settime(tch, tvp);
if (tch->todr_settime_ymdhms) {
clock_secs_to_ymdhms(tvp->tv_sec - rtc_offset * 60, &dt);
time_t sec = tvp->tv_sec - rtc_offset * 60;
if (tvp->tv_usec >= 500000)
sec++;
clock_secs_to_ymdhms(sec, &dt);
return tch->todr_settime_ymdhms(tch, &dt);
}