1 /* $NetBSD: clock.c,v 1.5 2014/11/20 16:34:25 christos 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 #include <dev/clock_subr.h> 11 12 #include "stand.h" 13 #include "net.h" 14 #include "libsa.h" 15 #include "bugsyscalls.h" 16 17 #define YEAR0 68 18 19 /* 20 * This code is defunct after 2068. 21 * Will Unix still be here then?? 22 */ 23 const short dayyr[12] = 24 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; 25 26 static u_long 27 chiptotime(int sec, int min, int hour, int day, int mon, int year) 28 { 29 int days, yr; 30 31 sec = bcdtobin(sec); 32 min = bcdtobin(min); 33 hour = bcdtobin(hour); 34 day = bcdtobin(day); 35 mon = bcdtobin(mon); 36 year = bcdtobin(year) + YEAR0; 37 if (year < 70) 38 year = 70; 39 40 /* simple sanity checks */ 41 if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31) 42 return (0); 43 days = 0; 44 for (yr = 70; yr < year; yr++) 45 days += days_per_year(yr); 46 days += dayyr[mon - 1] + day - 1; 47 if (is_leap_year(yr) && mon > 2) 48 days++; 49 /* now have days since Jan 1, 1970; the rest is easy... */ 50 return days * SECS_PER_DAY + hour * SECS_PER_HOUR 51 + min * SECS_PER_MINUTE + sec; 52 } 53 54 satime_t 55 getsecs(void) 56 { 57 struct bug_rtc_rd rr; 58 59 bugsys_rtc_rd(&rr); 60 61 return (chiptotime(rr.rr_second, rr.rr_minute, rr.rr_hour, 62 rr.rr_dayofmonth, rr.rr_month, rr.rr_year)); 63 } 64