clock.c revision 1.1
11.1Sscw/* $NetBSD: clock.c,v 1.1 2002/02/27 21:02:27 scw 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.1Sscw 111.1Sscw#include "stand.h" 121.1Sscw#include "libsa.h" 131.1Sscw#include "bugsyscalls.h" 141.1Sscw 151.1Sscw#define FROMBCD(x) (int)((((unsigned int)(x)) >> 4) * 10 +\ 161.1Sscw (((unsigned int)(x)) & 0xf)) 171.1Sscw 181.1Sscw#define SECDAY (24 * 60 * 60) 191.1Sscw#define SECYR (SECDAY * 365) 201.1Sscw#define LEAPYEAR(y) (((y) & 3) == 0) 211.1Sscw#define YEAR0 68 221.1Sscw 231.1Sscw/* 241.1Sscw * This code is defunct after 2068. 251.1Sscw * Will Unix still be here then?? 261.1Sscw */ 271.1Sscwconst short dayyr[12] = 281.1Sscw{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; 291.1Sscw 301.1Sscwstatic u_long 311.1Sscwchiptotime(int sec, int min, int hour, int day, int mon, int year) 321.1Sscw{ 331.1Sscw int days, yr; 341.1Sscw 351.1Sscw sec = FROMBCD(sec); 361.1Sscw min = FROMBCD(min); 371.1Sscw hour = FROMBCD(hour); 381.1Sscw day = FROMBCD(day); 391.1Sscw mon = FROMBCD(mon); 401.1Sscw year = FROMBCD(year) + YEAR0; 411.1Sscw if (year < 70) 421.1Sscw year = 70; 431.1Sscw 441.1Sscw /* simple sanity checks */ 451.1Sscw if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31) 461.1Sscw return (0); 471.1Sscw days = 0; 481.1Sscw for (yr = 70; yr < year; yr++) 491.1Sscw days += LEAPYEAR(yr) ? 366 : 365; 501.1Sscw days += dayyr[mon - 1] + day - 1; 511.1Sscw if (LEAPYEAR(yr) && mon > 2) 521.1Sscw days++; 531.1Sscw /* now have days since Jan 1, 1970; the rest is easy... */ 541.1Sscw return (days * SECDAY + hour * 3600 + min * 60 + sec); 551.1Sscw} 561.1Sscw 571.1Sscwtime_t 581.1Sscwgetsecs() 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