void settimeofday()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
// 根据时间戳设置时间
tv.tv_sec =time(NULL);
printf("tv_sec:%d,tv_usec:%d.\n",tv.tv_sec,tv.tv_usec);
printf("tz_minuteswest:%d,tz_dsttime:%d.\n",tz.tz_minuteswest,tz.tz_dsttime);
if(settimeofday(&tv,&tz) == -1)
{
if(errno == EPERM)
printf("The calling process has insufficient privilege to call settimeofday()\n");
else if( errno == EINVAL)
printf("Timezone (or something else) is invalid.\n");
else if(errno == EFAULT)
printf("One of tv or tz pointed outside the accessible address space.\n");
else
printf("Unknow Error.\n");
}
}
void set_sys_time()
{
struct timeval time;
struct tm *date;
//获取系统时间戳
gettimeofday(&time,NULL);
date = gmtime(&time.tv_sec);
date->tm_sec = clock_syn.msec_t1/1000;//替换秒
time.tv_sec = mktime(date);//换算为秒数
time.tv_usec = (__suseconds_t)(clock_syn.msec_t1%1000*1000);
//更新系统时间
settimeofday(&time,NULL);
//将系统时间同步到硬件时钟
//system("hwclock -w");
}