Implementation of time system function, to obtain the current time and the UTC i

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

For various reasons, little recently on the cnblogs. Just write a time function, can obtain the current time and in January 1, 1970 0 when the 0 minutes 0 seconds difference through the function, accurate to seconds, can be used in some no use time is not correct to call the hardware clock. The HWCLOCK can be opened, closed the case is the use of date access system. The opening is embedded Linux Application in the PPC architecture, the specific source code.:

#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;
}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Dana at November 13, 2013 - 8:10 AM