11.5Schristos/*	$NetBSD: clock.c,v 1.5 2014/11/20 16:34:25 christos Exp $	*/
21.1Sscw
31.1Sscw/*
41.1Sscw * This is a slightly modified version of mvme68k's standalone clock.c.
51.1Sscw * As there was no attribution/copyright header on that file, there's
61.1Sscw * not going to be one for this file.
71.1Sscw */
81.1Sscw
91.1Sscw#include <sys/types.h>
101.4Schristos#include <dev/clock_subr.h>
111.1Sscw
121.1Sscw#include "stand.h"
131.2Stsutsui#include "net.h"
141.1Sscw#include "libsa.h"
151.1Sscw#include "bugsyscalls.h"
161.1Sscw
171.1Sscw#define YEAR0		68
181.1Sscw
191.1Sscw/*
201.1Sscw * This code is defunct after 2068.
211.1Sscw * Will Unix still be here then??
221.1Sscw */
231.1Sscwconst short dayyr[12] =
241.1Sscw{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
251.1Sscw
261.1Sscwstatic u_long
271.1Sscwchiptotime(int sec, int min, int hour, int day, int mon, int year)
281.1Sscw{
291.1Sscw	int days, yr;
301.1Sscw
311.5Schristos	sec = bcdtobin(sec);
321.5Schristos	min = bcdtobin(min);
331.5Schristos	hour = bcdtobin(hour);
341.5Schristos	day = bcdtobin(day);
351.5Schristos	mon = bcdtobin(mon);
361.5Schristos	year = bcdtobin(year) + YEAR0;
371.1Sscw	if (year < 70)
381.1Sscw		year = 70;
391.1Sscw
401.1Sscw	/* simple sanity checks */
411.1Sscw	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
421.1Sscw		return (0);
431.1Sscw	days = 0;
441.1Sscw	for (yr = 70; yr < year; yr++)
451.4Schristos		days += days_per_year(yr);
461.1Sscw	days += dayyr[mon - 1] + day - 1;
471.4Schristos	if (is_leap_year(yr) && mon > 2)
481.1Sscw		days++;
491.1Sscw	/* now have days since Jan 1, 1970; the rest is easy... */
501.4Schristos 	return days * SECS_PER_DAY + hour * SECS_PER_HOUR
511.4Schristos	    + min * SECS_PER_MINUTE + sec;
521.1Sscw}
531.1Sscw
541.2Stsutsuisatime_t
551.3Sceggergetsecs(void)
561.1Sscw{
571.1Sscw	struct bug_rtc_rd rr;
581.1Sscw
591.1Sscw	bugsys_rtc_rd(&rr);
601.1Sscw
611.1Sscw	return (chiptotime(rr.rr_second, rr.rr_minute, rr.rr_hour,
621.1Sscw	    rr.rr_dayofmonth, rr.rr_month, rr.rr_year));
631.1Sscw}
64