Home | History | Annotate | Line # | Download | only in libsa
chiptotime.c revision 1.5
      1 /*	$NetBSD: chiptotime.c,v 1.5 2014/11/17 02:15:48 christos 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 YEAR0		68
     19 
     20 /*
     21  * This code is defunct after 2068.
     22  * Will Unix still be here then??
     23  */
     24 const short dayyr[12] =
     25     {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
     26 
     27 u_long
     28 chiptotime(int sec, int min, int hour, int day, int mon, int year)
     29 {
     30 	int days, yr;
     31 
     32 	sec = FROMBCD(sec);
     33 	min = FROMBCD(min);
     34 	hour = FROMBCD(hour);
     35 	day = FROMBCD(day);
     36 	mon = FROMBCD(mon);
     37 	year = FROMBCD(year) + YEAR0;
     38 	if (year < 70)
     39 		year = 70;
     40 
     41 	/* simple sanity checks */
     42 	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
     43 		return (0);
     44 	days = 0;
     45 	for (yr = 70; yr < year; yr++)
     46 		days += days_per_year(yr);
     47 	days += dayyr[mon - 1] + day - 1;
     48 	if (is_leap_year(yr) && mon > 2)
     49 		days++;
     50 	/* now have days since Jan 1, 1970; the rest is easy... */
     51 	return days * SECS_PER_DAY + hour * SECS_PER_HOUR
     52 	    + min * SECS_PER_MINUTE + sec;
     53 }
     54