clock.c revision 1.4
11.4Schristos/* $NetBSD: clock.c,v 1.4 2014/11/17 02:15:49 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 FROMBCD(x) (int)((((unsigned int)(x)) >> 4) * 10 +\ 181.1Sscw (((unsigned int)(x)) & 0xf)) 191.1Sscw 201.1Sscw#define YEAR0 68 211.1Sscw 221.1Sscw/* 231.1Sscw * This code is defunct after 2068. 241.1Sscw * Will Unix still be here then?? 251.1Sscw */ 261.1Sscwconst short dayyr[12] = 271.1Sscw{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; 281.1Sscw 291.1Sscwstatic u_long 301.1Sscwchiptotime(int sec, int min, int hour, int day, int mon, int year) 311.1Sscw{ 321.1Sscw int days, yr; 331.1Sscw 341.1Sscw sec = FROMBCD(sec); 351.1Sscw min = FROMBCD(min); 361.1Sscw hour = FROMBCD(hour); 371.1Sscw day = FROMBCD(day); 381.1Sscw mon = FROMBCD(mon); 391.1Sscw year = FROMBCD(year) + YEAR0; 401.1Sscw if (year < 70) 411.1Sscw year = 70; 421.1Sscw 431.1Sscw /* simple sanity checks */ 441.1Sscw if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31) 451.1Sscw return (0); 461.1Sscw days = 0; 471.1Sscw for (yr = 70; yr < year; yr++) 481.4Schristos days += days_per_year(yr); 491.1Sscw days += dayyr[mon - 1] + day - 1; 501.4Schristos if (is_leap_year(yr) && mon > 2) 511.1Sscw days++; 521.1Sscw /* now have days since Jan 1, 1970; the rest is easy... */ 531.4Schristos return days * SECS_PER_DAY + hour * SECS_PER_HOUR 541.4Schristos + min * SECS_PER_MINUTE + sec; 551.1Sscw} 561.1Sscw 571.2Stsutsuisatime_t 581.3Sceggergetsecs(void) 591.1Sscw{ 601.1Sscw struct bug_rtc_rd rr; 611.1Sscw 621.1Sscw bugsys_rtc_rd(&rr); 631.1Sscw 641.1Sscw return (chiptotime(rr.rr_second, rr.rr_minute, rr.rr_hour, 651.1Sscw rr.rr_dayofmonth, rr.rr_month, rr.rr_year)); 661.1Sscw} 67