获取正确的日历时间的前提是:bios时间正确。
time_t BIOSTime()
{
struct tm today;
unsigned char cHour;
unsigned char cMin;
unsigned char cSec;
unsigned char cDay;
unsigned char cMonth;
unsigned char cYear;
sysOutByte(0x70,0x00/*second*/);
cSec = sysInByte(0x71);
today.tm_sec = (cSec & 0x0F) + 10 * ((cSec&0xF0) >> 4);
sysOutByte(0x70,0x02/*minut*/);
cMin = sysInByte(0x71);
today.tm_min = (cMin & 0x0F) + 10*((cMin&0xF0) >> 4);
sysOutByte(0x70,0x04/*hour*/);
cHour = sysInByte(0x71);
today.tm_hour = (cHour&0x0F) + 10*((cHour&0xF0) >> 4);
sysOutByte(0x70,0x07/*day*/);
cDay = sysInByte(0x71);
today.tm_mday = (cDay&0x0F) + 10*((cDay&0xF0) >> 4);
sysOutByte(0x70,0x08/*month*/);
cMonth = sysInByte(0x71);
today.tm_mon = (cMonth&0x0F) + 10*((cMonth&0xF0) >> 4) - 1;
sysOutByte(0x70,0x09/*year*/);
cYear = sysInByte(0x71);
today.tm_year = 100 + (cYear&0x0F) + 10*((cYear&0xF0) >> 4);
return(mktime(&today));
}
void GetTime()
{
struct tm *_today ;
time_t today ;
int res;
struct timespec ts;
/*只要在加载时执行一次就可以了*/
ts.tv_sec = BIOSTime();
ts.tv_nsec = 0;
res = clock_settime(CLOCK_REALTIME, &ts);
/*获取时间时利用此函数就可以*/
time(&today) ;
_today = localtime(&today) ;
printf("%d-%d-%d/n" , _today->tm_year + 1900 , _today->tm_mon + 1, _today->tm_mday );
printf("%d:%d:%d/n" , _today->tm_hour , _today->tm_min , _today->tm_sec );
}