clock.c revision 1.5
1/*	$NetBSD: clock.c,v 1.5 2005/06/28 20:26:04 junyoung Exp $ */
2
3#include <sys/types.h>
4#include <machine/prom.h>
5
6#include "stand.h"
7#include "libsa.h"
8
9/*
10 * BCD to decimal and decimal to BCD.
11 */
12#define FROMBCD(x)      (int)((((unsigned int)(x)) >> 4) * 10 +\
13				(((unsigned int)(x)) & 0xf))
14#define TOBCD(x)        (int)((((unsigned int)(x)) / 10 * 16) +\
15				(((unsigned int)(x)) % 10))
16
17#define SECDAY          (24 * 60 * 60)
18#define SECYR           (SECDAY * 365)
19#define LEAPYEAR(y)     (((y) & 3) == 0)
20#define YEAR0		68
21
22/*
23 * This code is defunct after 2068.
24 * Will Unix still be here then??
25 */
26const short dayyr[12] =
27{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
28
29u_long
30chiptotime(int sec, int min, int hour, int day, int mon, int year)
31{
32	int days, yr;
33
34	sec = FROMBCD(sec);
35	min = FROMBCD(min);
36	hour = FROMBCD(hour);
37	day = FROMBCD(day);
38	mon = FROMBCD(mon);
39	year = FROMBCD(year) + YEAR0;
40	if (year < 70)
41		year = 70;
42
43	/* simple sanity checks */
44	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
45		return (0);
46	days = 0;
47	for (yr = 70; yr < year; yr++)
48		days += LEAPYEAR(yr) ? 366 : 365;
49	days += dayyr[mon - 1] + day - 1;
50	if (LEAPYEAR(yr) && mon > 2)
51		days++;
52	/* now have days since Jan 1, 1970; the rest is easy... */
53	return (days * SECDAY + hour * 3600 + min * 60 + sec);
54}
55
56time_t
57getsecs(void)
58{
59	struct mvmeprom_time m;
60
61	mvmeprom_rtc_rd(&m);
62	return chiptotime(m.sec_BCD, m.min_BCD, m.hour_BCD, m.day_BCD,
63			  m.month_BCD, m.year_BCD);
64}
65