我们都熟悉用date来设置系统时间,下面我来介绍下通过系统函数设置系统时间
#include <stdio.h>
#include<sys/time.h>
#include<unistd.h>
#include <time.h>
int SetSystemTime(char *dt)
{
struct tm rtc_time;
struct tm _tm;
struct timeval tv;
time_t timep;
sscanf(dt, "%d-%d-%d %d:%d:%d", &rtc_time.tm_year,
&rtc_time.tm_mon, &rtc_time.tm_mday,&rtc_time.tm_hour,
&rtc_time.tm_min, &rtc_time.tm_sec);
_tm.tm_sec = rtc_time.tm_sec;
_tm.tm_min = rtc_time.tm_min;
_tm.tm_hour = rtc_time.tm_hour;
_tm.tm_mday = rtc_time.tm_mday;
_tm.tm_mon = rtc_time.tm_mon - 1;
_tm.tm_year = rtc_time.tm_year - 1900;
timep = mktime(&_tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
if(settimeofday (&tv, (struct timezone *) 0) < 0)
{
printf("Set system datatime error!/n");
return -1;
}
return 0;
}
int main()
{
SetSystemTime("2006-4-20 20:30:30");
return 0;
}