chiptotime.c revision 1.5 1 1.5 christos /* $NetBSD: chiptotime.c,v 1.5 2014/11/17 02:15:48 christos Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek #include <sys/types.h>
4 1.1 jdolecek
5 1.1 jdolecek #include <machine/prom.h>
6 1.1 jdolecek
7 1.2 junyoung #include <lib/libsa/stand.h>
8 1.1 jdolecek #include "libsa.h"
9 1.1 jdolecek
10 1.1 jdolecek /*
11 1.1 jdolecek * BCD to decimal and decimal to BCD.
12 1.1 jdolecek */
13 1.1 jdolecek #define FROMBCD(x) (int)((((unsigned int)(x)) >> 4) * 10 +\
14 1.1 jdolecek (((unsigned int)(x)) & 0xf))
15 1.1 jdolecek #define TOBCD(x) (int)((((unsigned int)(x)) / 10 * 16) +\
16 1.1 jdolecek (((unsigned int)(x)) % 10))
17 1.1 jdolecek
18 1.1 jdolecek #define YEAR0 68
19 1.1 jdolecek
20 1.1 jdolecek /*
21 1.1 jdolecek * This code is defunct after 2068.
22 1.1 jdolecek * Will Unix still be here then??
23 1.1 jdolecek */
24 1.1 jdolecek const short dayyr[12] =
25 1.4 tsutsui {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
26 1.1 jdolecek
27 1.1 jdolecek u_long
28 1.4 tsutsui chiptotime(int sec, int min, int hour, int day, int mon, int year)
29 1.1 jdolecek {
30 1.1 jdolecek int days, yr;
31 1.1 jdolecek
32 1.1 jdolecek sec = FROMBCD(sec);
33 1.1 jdolecek min = FROMBCD(min);
34 1.1 jdolecek hour = FROMBCD(hour);
35 1.1 jdolecek day = FROMBCD(day);
36 1.1 jdolecek mon = FROMBCD(mon);
37 1.1 jdolecek year = FROMBCD(year) + YEAR0;
38 1.1 jdolecek if (year < 70)
39 1.1 jdolecek year = 70;
40 1.1 jdolecek
41 1.1 jdolecek /* simple sanity checks */
42 1.1 jdolecek if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
43 1.1 jdolecek return (0);
44 1.1 jdolecek days = 0;
45 1.1 jdolecek for (yr = 70; yr < year; yr++)
46 1.5 christos days += days_per_year(yr);
47 1.1 jdolecek days += dayyr[mon - 1] + day - 1;
48 1.5 christos if (is_leap_year(yr) && mon > 2)
49 1.1 jdolecek days++;
50 1.1 jdolecek /* now have days since Jan 1, 1970; the rest is easy... */
51 1.5 christos return days * SECS_PER_DAY + hour * SECS_PER_HOUR
52 1.5 christos + min * SECS_PER_MINUTE + sec;
53 1.1 jdolecek }
54