日期:2014-05-16 浏览次数:20735 次
#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include <errno.h>
#include <time.h>
#include<sys/time.h>
#include <string.h>
int set_time(unsigned char *settime)
{
struct timeval new_tv;
int ret;
time_t timep,timeq;
struct tm *p;
time(&timep);
p = localtime(&timep);
p->tm_year = settime[0]+2000-1900;
p->tm_mon = settime[1]-1;
p->tm_mday = settime[2];
p->tm_hour = settime[3];
p->tm_min = settime[4];
p->tm_sec = settime[5];
printf("%d %d %d %d %d %d \n",p->tm_year,p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
timeq = mktime(p);
printf("timeq %ld\n",timeq);
new_tv.tv_sec = (long)timeq;
new_tv.tv_usec = 0;
ret = settimeofday(&new_tv,NULL);
if(ret == -1)
{
printf("settimeofday error.%s\n",strerror(errno));
return ret;
}
else
system("hwclock -w");
return 0;
}
int main()
{
unsigned char timebuf[] = {13,07,05,17,25,55};
set_time(timebuf);
return 0;
}