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;

    tv.tv_sec = mktime(&tptr);
    tv.tv_usec = 0;
    settimeofday(&tv, NULL);

}

posted @ 2019-01-23 12:07  youngliu91  阅读(4018)  评论(0编辑  收藏  举报