#include #include #define MINUTE 60 #define HOUR (60*MINUTE) #define DAY (24*HOUR) #define YEAR (365*DAY) int tage_pro_monat[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int CMOS_READ(int address) { if(address < 10) { outb(0x70, 0xa); while((inb(0x71) & (1 << 7)) != 0) { asm volatile("nop"); } } outb(0x70, address); return inb(0x71); } int bcd_to_bin(int val) { //Ueberpruefen, ob BCD verwendet wird if((val & (1 << 2)) == 0) { return (val & 0xf) + ((val >> 4) & 0xf) * 10; } else { return val; } } time_t system_time; void init_time() { do { system_time.tm_sec = bcd_to_bin(CMOS_READ(0)); system_time.tm_min = bcd_to_bin(CMOS_READ(2)); system_time.tm_hour = bcd_to_bin(CMOS_READ(4)); system_time.tm_mday = bcd_to_bin(CMOS_READ(7)); system_time.tm_mon = bcd_to_bin(CMOS_READ(8)); system_time.tm_year = bcd_to_bin(CMOS_READ(9)) + 2000; } while (system_time.tm_sec != bcd_to_bin(CMOS_READ(0))); system_time.tm_timestamp = time_t_to_unix(system_time); } long time_t_to_unix(time_t tm) { int jahr, stunde, minute, sekunde, monat, tag; jahr = tm.tm_year; stunde = tm.tm_hour; minute = tm.tm_min; sekunde = tm.tm_sec; monat = tm.tm_mon; tag = tm.tm_mday; const short tage_bis_monatsanfang[12] = /* ohne Schalttag */ {0,31,59,90,120,151,181,212,243,273,304,334}; long unix_zeit; long long jahre=jahr-1970; int schaltjahre=((jahr-1)-1968)/4 - ((jahr-1)-1900)/100 + ((jahr-1)-1600)/400; unix_zeit=sekunde + 60*minute + 60*60*stunde + (tage_bis_monatsanfang[monat-1]+tag-1)*60*60*24 + (jahre*365+schaltjahre)*60*60*24; if ( (monat>2) && (jahr%4==0 && (jahr%100!=0 || jahr%400==0)) ) unix_zeit+=60*60*24; /* +Schalttag wenn jahr Schaltjahr ist */ return unix_zeit; }