openwrt linux c 修改系統時間

#include <stdio.h>
#include<sys/time.h>
#include<unistd.h>
#include <time.h>

typedef    struct _SettimeInfo_
{
    char settime[32]; //輸入要修改的系統時間 %d-%d-%d %d:%d:%d
}SETTIMEINFO;


int SetSystemTime(SETTIMEINFO * SetTime)
{
    struct tm rtc_time;
    struct tm _tm;
    struct timeval tv;
    time_t timep;
    sscanf(SetTime->settime, "%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;
}

 

int SetSystemTime(SETTIMEINFO * SetTime) //char *dt 
{
    struct timeval stime;
    stime.tv_usec = SetTime->settime;
    settimeofday(&stime,NULL);
    printf("毫秒數是:%ld\n現在的時間是:",stime.tv_usec);
    fflush(stdout);
    system("date");
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章