溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法

發(fā)布時(shí)間:2021-08-31 16:36:38 來源:億速云 閱讀:234 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要介紹“Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法”,在日常操作中,相信很多人在Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

    一、寫入時(shí)間
    1、預(yù)備知識(shí):
    a、mktime
    頭文件#include <time.h>
    函數(shù)time_t mktime(struct tm *timeptr)
    函數(shù)說明:mktime()用來將timeptr所指的tm結(jié)構(gòu)體數(shù)據(jù)換成從公元1970年1月1日0時(shí)0分0 秒算起至今的本地時(shí)間所經(jīng)過的秒數(shù)。
    返回值:返回經(jīng)過的秒數(shù)。當(dāng)發(fā)生錯(cuò)誤的時(shí)候,返回-1。
    b、settimeofday
    頭文件#include <sys/time.h>
                  #include <unistd.h>
    函數(shù):int settimeofday(const struct timeval *tv,const struct timezone *tz)
    函數(shù)說明:settimeofday()會(huì)把目前時(shí)間設(shè)成由tv所指的結(jié)構(gòu)體信息,當(dāng)?shù)貢r(shí)區(qū)信息則設(shè)成tz所指的結(jié)構(gòu)體。
    返回值:只有root權(quán)限才能使用此函數(shù)修改時(shí)間。成功則返回0,失敗返回-1,錯(cuò)誤代碼存于errno。
    2、實(shí)踐:
    通過mktime和settimeofday配合使用,即可完成時(shí)間的寫入。
    3、代碼如下:

#include <stdio.h>#include <time.h>#include <sys/time.h>#include <unistd.h>#include <stdlib.h>#include <bits/types.h>#include <linux/rtc.h>struct my_timeval
{
    __time_t tv_sec;
    __suseconds_t tv_usec;
};/*************************************************
*函數(shù)名       :    System_SetTime
*功能         :    寫入系統(tǒng)時(shí)間
*使用方法      :    char* dt = "2016-04-15 21:00:00";
                   System_SetTime(dt);
**************************************************/int System_SetTime(char* dt)
{struct rtc_time tm;struct tm _tm;struct my_timeval tv;
    time_t timep;sscanf(dt,"%d-%d-%d %d:%d:%d",&tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
    _tm.tm_sec = tm.tm_sec;
    _tm.tm_min = tm.tm_min;
    _tm.tm_hour = tm.tm_hour;
    _tm.tm_mday = tm.tm_mday;
    _tm.tm_mon = tm.tm_mon - 1;
    _tm.tm_year = tm.tm_year - 1900;

    timep = mktime(&_tm);                               
    tv.tv_sec = timep;
    tv.tv_usec = 0;if(settimeofday(&tv, (struct timezone *) 0) < 0)
    {printf("Set system datetime error!\n");return -1;
    }   return 0;
}void main(void)
{char *dt = "2016-4-15 21:00:00";
    System_SetTime(dt);
}

    4、測(cè)試結(jié)果:
Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法
    二、保存時(shí)間
    從上面的測(cè)試結(jié)果可以看出,可以正常寫入系統(tǒng)時(shí)間了。我起初也以為這樣就可以了,但是我發(fā)現(xiàn),這樣是不行的。因?yàn)橐坏┪抑匦聠?dòng)開發(fā)板,系統(tǒng)時(shí)間又會(huì)回復(fù)到原來的時(shí)間。想想也是,我們只是寫入了系統(tǒng)時(shí)間,沒有將系統(tǒng)時(shí)間同步到硬件時(shí)間,這樣系統(tǒng)每次重啟讀取的硬件時(shí)間是沒有改變的,啟動(dòng)后得到的系統(tǒng)時(shí)間CST = UTC + 8,還是換來的系統(tǒng)時(shí)間。那怎樣將我們?cè)O(shè)置的系統(tǒng)時(shí)間同步到硬件時(shí)間呢?我們知道在終端里,可以通過hwclock –systohc將系統(tǒng)時(shí)間同步到硬件時(shí)間上去,在應(yīng)用層怎么實(shí)現(xiàn)呢?我不知道有沒有其他好的解決辦法,我想出來的辦法就是在應(yīng)用層創(chuàng)建子進(jìn)程,在子進(jìn)程里調(diào)用腳本文件,腳本里的指令就是hwclock –systohc。這樣就完成了同步。當(dāng)然如果有更簡(jiǎn)單和更合適的方法,歡迎指導(dǎo)、交流。說了這么多,言歸正傳。
    1、預(yù)備知識(shí):
    a、fork創(chuàng)建子進(jìn)程,代碼如下:

/**************************
*功能:創(chuàng)建子進(jìn)程fork()測(cè)試
*時(shí)間:2016-4-15
*作者:Jack Cui
***************************/#include <unistd.h>
#include <stdio.h> 
int main (void) 
{ 
    pid_t fpid;                 //fpid表示fork函數(shù)返回的值int count=0;
    fpid=fork(); if (fpid < 0)               //創(chuàng)建子進(jìn)程失敗printf("error\n"); else if (fpid == 0) {
        printf("I am the child process,my process id is %d\n",getpid()); 
        count++;
    }else {
        printf("I am the parent process,my process id is %d\n",getpid()); 
        count++;
    }
    printf("count = %d\n",count);return 0;
}

    b、fork測(cè)試程序結(jié)果顯示:
Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法
    c、execve()應(yīng)用層調(diào)用腳本文件:
    頭文件:#include <unistd.h>
    函數(shù):int execve(const char * filename, char * const argv[], char * const envp[]);
    函數(shù)說明: execve()用來執(zhí)行參數(shù)filename 字符串所代表的文件路徑, 第二個(gè)參數(shù)系利用數(shù)組指針來傳遞給執(zhí)行文件, 最后一個(gè)參數(shù)則為傳遞給執(zhí)行文件的新環(huán)境變量數(shù)組。
    返回值:如果執(zhí)行成功則函數(shù)不會(huì)返回, 執(zhí)行失敗則直接返回-1, 失敗原因存于errno 中。
    d、execve()測(cè)試代碼:

/**************************
*功能:測(cè)試execve
*時(shí)間:2016-4-15
*作者:Jack Cui
***************************/#include <stdio.h>         //perror#include <stdlib.h>        //EXIT_SUCCESS EXIT_FAILURE#include <unistd.h>        //execvevoid main(void)
{char * args[] = {
  
  
  "/home/nfsroot/hwclock.sh", NULL};if(-1 == (execve("/home/nfsroot/hwclock.sh",args,NULL)))
    {
        perror("execve");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

    e、腳本內(nèi)容:
Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法
    f、execve測(cè)試結(jié)果:
Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法
    可以看出execve使用正常,我們將腳本內(nèi)容改為hwclock –systohc就可以實(shí)現(xiàn)將系統(tǒng)時(shí)間同步到硬件時(shí)間了。
    三、整體代碼如下:

/******************************************
*功能:Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的方法
*時(shí)間:2016-4-15
*作者:Jack Cui
*******************************************/#include <stdio.h>#include <time.h>#include <sys/time.h>#include <unistd.h>#include <stdlib.h>#include <bits/types.h>#include <linux/rtc.h>struct my_timeval
{
    __time_t tv_sec;
    __suseconds_t tv_usec;
};/*************************************************
*函數(shù)名        :   System_SetTime
*功能         :   寫入系統(tǒng)時(shí)間
*使用方法   :   char* dt = "2016-04-15 21:00:00";
                System_SetTime(dt);
**************************************************/int System_SetTime(char* dt)
{struct rtc_time tm;struct tm _tm;struct my_timeval tv;
    time_t timep;sscanf(dt,"%d-%d-%d %d:%d:%d",&tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
    _tm.tm_sec = tm.tm_sec;
    _tm.tm_min = tm.tm_min;
    _tm.tm_hour = tm.tm_hour;
    _tm.tm_mday = tm.tm_mday;
    _tm.tm_mon = tm.tm_mon - 1;
    _tm.tm_year = tm.tm_year - 1900;

    timep = mktime(&_tm);                               
    tv.tv_sec = timep;
    tv.tv_usec = 0;if(settimeofday(&tv, (struct timezone *) 0) < 0)
    {printf("Set system datetime error!\n");return -1;
    }   return 0;
}void main(void)
{char *dt = "2016-4-15 21:00:00";
    pid_t fpid;                 //fpid表示fork函數(shù)返回的值fpid=fork(); if (fpid < 0)               //創(chuàng)建子進(jìn)程失敗printf("error\n"); else if (fpid == 0) 
    {char * args[] = {
  
  
  "/home/nfsroot/hwclock.sh", NULL};if(-1 == (execve("/home/nfsroot/hwclock.sh",args,NULL)))
        {
            perror("execve");exit(EXIT_FAILURE);
        }exit(EXIT_SUCCESS);
    }else{
        System_SetTime(dt);
    }return 0;
}

    四、最終結(jié)果顯示:
    1、腳本內(nèi)容:
Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法
    2、測(cè)試結(jié)果:
Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法
    這樣我們重新啟動(dòng)開發(fā)板,系統(tǒng)時(shí)間不會(huì)變,設(shè)置成功~!

到此,關(guān)于“Linux應(yīng)用層系統(tǒng)時(shí)間寫入RTC時(shí)鐘的辦法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI