clock.c revision 1.3
11.3Scegger/*	$NetBSD: clock.c,v 1.3 2009/03/18 10:22:33 cegger Exp $	*/
21.1Sscw
31.1Sscw/*
41.1Sscw * This is a slightly modified version of mvme68k's standalone clock.c.
51.1Sscw * As there was no attribution/copyright header on that file, there's
61.1Sscw * not going to be one for this file.
71.1Sscw */
81.1Sscw
91.1Sscw#include <sys/types.h>
101.1Sscw
111.1Sscw#include "stand.h"
121.2Stsutsui#include "net.h"
131.1Sscw#include "libsa.h"
141.1Sscw#include "bugsyscalls.h"
151.1Sscw
161.1Sscw#define FROMBCD(x)      (int)((((unsigned int)(x)) >> 4) * 10 +\
171.1Sscw			      (((unsigned int)(x)) & 0xf))
181.1Sscw
191.1Sscw#define SECDAY          (24 * 60 * 60)
201.1Sscw#define SECYR           (SECDAY * 365)
211.1Sscw#define LEAPYEAR(y)     (((y) & 3) == 0)
221.1Sscw#define YEAR0		68
231.1Sscw
241.1Sscw/*
251.1Sscw * This code is defunct after 2068.
261.1Sscw * Will Unix still be here then??
271.1Sscw */
281.1Sscwconst short dayyr[12] =
291.1Sscw{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
301.1Sscw
311.1Sscwstatic u_long
321.1Sscwchiptotime(int sec, int min, int hour, int day, int mon, int year)
331.1Sscw{
341.1Sscw	int days, yr;
351.1Sscw
361.1Sscw	sec = FROMBCD(sec);
371.1Sscw	min = FROMBCD(min);
381.1Sscw	hour = FROMBCD(hour);
391.1Sscw	day = FROMBCD(day);
401.1Sscw	mon = FROMBCD(mon);
411.1Sscw	year = FROMBCD(year) + YEAR0;
421.1Sscw	if (year < 70)
431.1Sscw		year = 70;
441.1Sscw
451.1Sscw	/* simple sanity checks */
461.1Sscw	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
471.1Sscw		return (0);
481.1Sscw	days = 0;
491.1Sscw	for (yr = 70; yr < year; yr++)
501.1Sscw		days += LEAPYEAR(yr) ? 366 : 365;
511.1Sscw	days += dayyr[mon - 1] + day - 1;
521.1Sscw	if (LEAPYEAR(yr) && mon > 2)
531.1Sscw		days++;
541.1Sscw	/* now have days since Jan 1, 1970; the rest is easy... */
551.1Sscw	return (days * SECDAY + hour * 3600 + min * 60 + sec);
561.1Sscw}
571.1Sscw
581.2Stsutsuisatime_t
591.3Sceggergetsecs(void)
601.1Sscw{
611.1Sscw	struct bug_rtc_rd rr;
621.1Sscw
631.1Sscw	bugsys_rtc_rd(&rr);
641.1Sscw
651.1Sscw	return (chiptotime(rr.rr_second, rr.rr_minute, rr.rr_hour,
661.1Sscw	    rr.rr_dayofmonth, rr.rr_month, rr.rr_year));
671.1Sscw}
68