搞不明白哪里有问题
执行结果显示errno22(EINVAL 时区或某个数据是不正确的,无法正确设置时间)
我不知道哪里出问题帮我解决一下呗!!
unsigned char set_time(unsigned char *settime)
{
struct timeval new_tv;
struct timezone new_tz;
time_t timep,timeq;
struct tm *p;
#if 0
int ret;
ret = gettimeofday(&new_tv,&new_tz);
if(ret == -1)
{
qDebug("Gettimeofday err .ret = %d",ret);
return ;
}
ret = time(&timep);
if(ret == (time_t) -1 )
{
qDebug("This err .ret = %d",ret);
return ;
}
#endif
time(&timep);
p = localtime(&timep);
p->tm_year = settime[0];
p->tm_mon = settime[1];
p->tm_mday = settime[2];
p->tm_hour = settime[3];
p->tm_min = settime[4];
p->tm_sec = settime[5];
timeq = mktime(p);
new_tv.tv_sec = (long)timeq;
new_tv.tv_usec = 0;
if((settimeofday(&new_tv,NULL)) < 0)
{
fprintf(stderr,"settimeofday err:\n",strerror(errno));
printf("%d\n",errno);
return -1;
}
system("hwclock -w");
return 0;
}
int main()
{
unsigned char timebuf[] = {13,05,10,17,25,55};
set_time(timebuf);
return 0;
}
------解决方案--------------------
#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];