当前位置: 技术问答>linux和unix
搞不明白哪里有问题
来源: 互联网 发布时间:2017-04-17
本文导语: 执行结果显示errno22(EINVAL 时区或某个数据是不正确的,无法正确设置时间) 我不知道哪里出问题帮我解决一下呗!! unsigned char set_time(unsigned char *settime) { struct timeval new_tv; struct timezone new_tz; time_t timep,t...
执行结果显示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)) 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 %ldn",timeq);
new_tv.tv_sec = (long)timeq;
new_tv.tv_usec = 0;
ret = settimeofday(&new_tv,NULL);
if(ret == -1)
{
printf("settimeofday error.%sn",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;
}
root运行
时区自改
我不知道哪里出问题帮我解决一下呗!!
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)) 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 %ldn",timeq);
new_tv.tv_sec = (long)timeq;
new_tv.tv_usec = 0;
ret = settimeofday(&new_tv,NULL);
if(ret == -1)
{
printf("settimeofday error.%sn",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;
}
root运行
时区自改