linux c语言设置系统时间
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
void main()
{
printf("before time set");
fflush(stdout);
system("date");
system("hwclock");
TimeSet(2012,10,10,1,30,8);
system("hwclock -w");
printf("after time set");
fflush(stdout);
system("date");
system("hwclock");
}
void TimeSet(int year,int month,int day,int hour,int min,int sec)
{
struct tm tptr;
struct timeval tv;
tptr.tm_year = year - 1900;
tptr.tm_mon = month - 1;
tptr.tm_mday = day;
tptr.tm_hour = hour;
tptr.tm_min = min;
tptr.tm_sec = sec;
tptr.tm_isdst = 0;//必须设置,不设置mktime大概率返回-1
tv.tv_sec = mktime(&tptr);
tv.tv_usec = 0;
settimeofday(&tv, NULL);
}