clock.c revision 1.1
1/*	$NetBSD: clock.c,v 1.1 2002/02/27 21:02:27 scw Exp $	*/
2
3/*
4 * This is a slightly modified version of mvme68k's standalone clock.c.
5 * As there was no attribution/copyright header on that file, there's
6 * not going to be one for this file.
7 */
8
9#include <sys/types.h>
10
11#include "stand.h"
12#include "libsa.h"
13#include "bugsyscalls.h"
14
15#define FROMBCD(x)      (int)((((unsigned int)(x)) >> 4) * 10 +\
16			      (((unsigned int)(x)) & 0xf))
17
18#define SECDAY          (24 * 60 * 60)
19#define SECYR           (SECDAY * 365)
20#define LEAPYEAR(y)     (((y) & 3) == 0)
21#define YEAR0		68
22
23/*
24 * This code is defunct after 2068.
25 * Will Unix still be here then??
26 */
27const short dayyr[12] =
28{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
29
30static u_long
31chiptotime(int sec, int min, int hour, int day, int mon, int year)
32{
33	int days, yr;
34
35	sec = FROMBCD(sec);
36	min = FROMBCD(min);
37	hour = FROMBCD(hour);
38	day = FROMBCD(day);
39	mon = FROMBCD(mon);
40	year = FROMBCD(year) + YEAR0;
41	if (year < 70)
42		year = 70;
43
44	/* simple sanity checks */
45	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
46		return (0);
47	days = 0;
48	for (yr = 70; yr < year; yr++)
49		days += LEAPYEAR(yr) ? 366 : 365;
50	days += dayyr[mon - 1] + day - 1;
51	if (LEAPYEAR(yr) && mon > 2)
52		days++;
53	/* now have days since Jan 1, 1970; the rest is easy... */
54	return (days * SECDAY + hour * 3600 + min * 60 + sec);
55}
56
57time_t
58getsecs()
59{
60	struct bug_rtc_rd rr;
61
62	bugsys_rtc_rd(&rr);
63
64	return (chiptotime(rr.rr_second, rr.rr_minute, rr.rr_hour,
65	    rr.rr_dayofmonth, rr.rr_month, rr.rr_year));
66}
67