chiptotime.c revision 1.2 1 /* $NetBSD: chiptotime.c,v 1.2 2005/06/28 21:03:02 junyoung Exp $ */
2
3 #include <sys/types.h>
4
5 #include <machine/prom.h>
6
7 #include <lib/libsa/stand.h>
8 #include "libsa.h"
9
10 /*
11 * BCD to decimal and decimal to BCD.
12 */
13 #define FROMBCD(x) (int)((((unsigned int)(x)) >> 4) * 10 +\
14 (((unsigned int)(x)) & 0xf))
15 #define TOBCD(x) (int)((((unsigned int)(x)) / 10 * 16) +\
16 (((unsigned int)(x)) % 10))
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 */
27 const short dayyr[12] =
28 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
29
30 u_long
31 chiptotime(sec, min, hour, day, mon, year)
32 int sec, min, hour, day, mon, year;
33 {
34 int days, yr;
35
36 sec = FROMBCD(sec);
37 min = FROMBCD(min);
38 hour = FROMBCD(hour);
39 day = FROMBCD(day);
40 mon = FROMBCD(mon);
41 year = FROMBCD(year) + YEAR0;
42 if (year < 70)
43 year = 70;
44
45 /* simple sanity checks */
46 if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
47 return (0);
48 days = 0;
49 for (yr = 70; yr < year; yr++)
50 days += LEAPYEAR(yr) ? 366 : 365;
51 days += dayyr[mon - 1] + day - 1;
52 if (LEAPYEAR(yr) && mon > 2)
53 days++;
54 /* now have days since Jan 1, 1970; the rest is easy... */
55 return (days * SECDAY + hour * 3600 + min * 60 + sec);
56 }
57