因种种原因,最近很少上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 ",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] = '