Vxworks获取日历时间代码

获取正确的日历时间的前提是: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 );   
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值