实现系统函数time,获取当前时间与UTC的间隔

因种种原因,最近很少上cnblogs了。刚写了一个实现time的函数,可以通过该函数获取当前时间与1970年1月1日 0时0分0秒的差值,精确到秒,可以用在某些没有时候使用time不正确而不得不调用硬件时钟的场合。把其中HWCLOCK打开即可,关闭的话使用的是date获取的系统时间。其中打开时是应用在PPC架构的嵌入式linux上,具体源码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned short YEAR;
typedef unsigned short MONTH;
typedef unsigned short DAY;
typedef unsigned short HOUR;
typedef unsigned short MIN;
typedef unsigned short SEC;

char g_month[12][4] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

#define HWCLOCK


typedef struct struct_time
{
    YEAR    year;
    MONTH    mon;
    DAY        day;
    HOUR    hour;
    MIN        min;
    SEC        sec;
}time_s;

void PrintTimeStruct(time_s *t)
{
    if(t)
        printf("%d-%d-%d %d:%d:%d\n",t->year,t->mon,t->day,t->hour,t->min,t->sec);

}

void FillInElem(time_s *t,char *buf,int index)
{
    int i = 0;
    
    switch(index)
    {
        //week
        case 1:
            {                
                break;
            }
        //month
        case 2:
            {
                for(i = 0 ; i < 12 ; i++)
                {
                    if(strcasecmp(buf,g_month[i]) == 0)
                    {
                        t->mon = i+1;
                        break;
                    }
                }
                break;
            }
        //day    
        case 3:
            {
                t->day = atoi(buf);
                break;
            }
        //time    
        case 4:
            {
#ifdef HWCLOCK
                buf[2] = '\0';
                t->hour = atoi(buf);

                buf[5] = '\0';
                t->min = atoi(buf+3);

                t->sec = atoi(buf+6);
#else
                sscanf(buf,"%d:%d:%d",&t->hour,&t->min,&t->sec);
#endif
                break;
            }
#ifdef HWCLOCK
        case 5:
            {
                t->year = atoi(buf);
                break;
            }
#else
        //cst
        case 5:
            {
                break;
            }
        //year    
        case 6:
            {
                t->year = atoi(buf);
                break;
            }
#endif

        default:break;
        
    }

}

void GetTimeStruct(time_s *t,char *buf)
{
    int len = strlen(buf);
    int i = 0;
    int j = 0;
    int index = 0;
    
    char temp[20];

    if(!t || !buf)
    {
        return ;
    }

    memset(temp,0,sizeof(temp));    
    for(;i < len ; i++)
    {
        if(buf[i] != ' ')
        {
            temp[j] = buf[i];
            j++;            
        }
        else
        {
            index++;
            //printf("get %s index %d\n",temp,index);
            FillInElem(t,temp,index);
            memset(temp,0,sizeof(temp));
            j = 0;
#ifdef HWCLOCK
            if(index == 2)
            {
                i++;
            }
            if(index >= 5)
            {
                return;
            }
#endif    
        }
        
    }

    //printf("get %s \n",temp);
    index++;
    FillInElem(t,temp,index);
}


int GetHWClock(time_s *t)
{
    char buf[128];
#ifdef HWCLOCK
    system("hwclock > time.txt");

#else
    system("date > time.txt");

#endif

    FILE *fp = NULL;

    if((fp = fopen("time.txt","r")) != NULL)
    {
        fgets(buf,sizeof(buf),fp);
        fclose(fp);

        GetTimeStruct(t,buf);
        //printf("get :%s",buf);
        system("rm time.txt");
        return 1;
    }
    else
    {
        return 0;
    }
    
}

int IsRunYear(YEAR y)
{
    if( (y % 400 == 0) || ( (y % 4 == 0)&&(y % 100 != 0) ))
    {
        return 1;
    }
    return 0;
}

unsigned long long GetTickCount(time_s *t)
{
    if(t)
    {
        unsigned long long temp = 0;
        int RunYearNum = 0;// run nian  365

        int i = 0;

        //run nian
        for(i = 1970 ;i < t->year ; i++)
        {
            if( IsRunYear(i) == 1)
            {
                RunYearNum++;
            }
        }

        temp += RunYearNum* 366 * 24 * 60 * 60;
        temp += (t->year - 1970 - RunYearNum)* 365 * 24 * 60 * 60;
        //printf("%d run temp %llu\n",RunYearNum,temp);
        
        //month
        int day = 0;
        for(i = 1; i < t->mon ; i++)
        {
            if(i == 2)
            {
                if(IsRunYear(t->year) == 1)
                {
                    day += 29;
                }
                else
                {
                    day += 28;
                }
            }
            else if((i == 3) || (i == 5) || (i == 7) || (i == 8) || (i == 10) || (i == 12))
            {
                day += 31;
            }
            else
            {
                day += 30;
            }
        }
        
        temp += day*24*60*60;
        //printf("day  %d temp %llu\n",day,temp);
        //day
        temp += (t->day - 1)*24*60*60;

        //hour 
        temp += (t->hour)*60*60;

        //min
        temp += (t->min)*60;

        //sec
        temp += t->sec;

        return temp;
        
    }
    else
    {
        return 0;
    }

}

int main(void)
{
    time_s t;
    
    while(1)
    {
        GetHWClock(&t);
        PrintTimeStruct(&t);
    
        printf("tick  %llu \n",GetTickCount(&t));
        sleep(1);
    }

    
    
    return 0;
}

 

posted on 2013-08-28 13:20  净坛使者  阅读(1726)  评论(1编辑  收藏  举报

导航