天天看點

介紹Linux下時間處理的相關操作(RTC、延時、鬧鐘、轉換)

一、系統時間設定

這篇文章主要介紹Linux下時間處理的相關函數與操作。

比如: 系統時間設定,讀取、RTC時間設定,讀取、時間機關轉換、延時函數、鬧鐘信号等等。

Linux下存在兩種時間: 1. 系統時間,2. RTC時間

系統時間是每次作業系統啟動之後,從RTC驅動裡讀取進行設定的,一般隻會在系統上電啟動的時候自動(啟動腳本)同步一次,後續使用者也可以通過特定的指令再次同步;在系統界面上看到的時間就是系統時間;系統時間每次系統關機之後就會丢失,需要每次上電從RTC驅動裡擷取。

系統時間設定的方法如下:需要有管理者權限

[wbyq@wbyq linux_c]$ date -s "2020-10-12 9:28:20"
date: 無法設定日期: 不允許的操作
2020年 10月 12日 星期一 09:28:20 CST
[wbyq@wbyq linux_c]$ sudo date -s "2020-10-12 9:28:20"
[sudo] password for wbyq: 
2020年 10月 12日 星期一 09:28:20 CST
[wbyq@wbyq linux_c]$            

RTC時間掉電不會停止運作,電源是後備電源單獨供給的;可以一直運作,友善給系統提供準确的時間。

RTC時間讀取與設定方法:需要有管理者權限

hwclock -r   顯示RTC時間 (讀取RTC時間顯示)
hwclock -w   設定RTC時間 (将系統時間傳遞給RTC驅動,設定RTC的驅動時間)
hwclock -s   設定系統時間(将RTC時間讀取出來設定給系統時間)           

也可以通過代碼實作:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/rtc.h>

/*
RTC_SET_TIME
RTC_RD_TIME
*/

struct rtc_time time;
int main(int argc,char **argv)
{
    if(argc!=2)
    {
        printf("./app /dev/rtcX\n");
        return 0;
    }
    //1.打開裝置檔案
    int fd=open(argv[1],2);
    if(fd<2)
    {
        printf("%s 裝置檔案打開失敗.\n",argv[1]);
        return 0;
    }
    
    //2.擷取RTC驅動的時間
    ioctl(fd,RTC_RD_TIME,&time);
    printf("應用層讀取的時間: %d-%d-%d %d:%d:%d\n",
            time.tm_year+1900,
            time.tm_mon+1,
            time.tm_mday,
            time.tm_hour,
            time.tm_min,
            time.tm_sec);
    
    //3.設定RTC驅動的時間
    time.tm_year=2021-1900;
    time.tm_mon=10-1;
    time.tm_mday=1;
    time.tm_hour=11;
    time.tm_min=10;
    time.tm_sec=20;
    ioctl(fd,RTC_SET_TIME,&time);
    
    //4. 關閉驅動
    close(fd);
    return 0;
}           

二、時間處理相關函數介紹(time.h)

#include <time.h>
struct tm {
    int tm_sec;         /* seconds */
    int tm_min;         /* minutes */
    int tm_hour;        /* hours */
    int tm_mday;        /* day of the month */
    int tm_mon;         /* month */
    int tm_year;        /* year */
    int tm_wday;        /* day of the week */
    int tm_yday;        /* day in the year */
    int tm_isdst;       /* daylight saving time */
};

char *asctime(const struct tm *tm); //内部有一個全局空間存放轉換的時間
char *asctime_r(const struct tm *tm, char *buf); //使用者可以指定自己的空間
函數功能: 将tm時間結構體裡的時間轉為字元串格式傳回(指針傳回).

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
函數功能: 将秒機關的時間轉為字元串格式傳回.

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
函數功能: 将秒機關的時間轉為格林威治時間傳回---使用tm結構體。

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
函數功能: 将秒機關的時間轉為本地時間傳回.---使用tm結構體

time_t mktime(struct tm *tm);
函數功能: 将tm結構體時間轉為秒機關傳回.

time_t time(time_t *t);
函數功能:如果形參填NULL就表示擷取當期系統的秒機關時間.

size_t strftime(char *s, size_t max, const char *format,
                       const struct tm *tm);
函數功能:  将tm結構體的時間按照指定的格式轉成字元串傳回.
const char *format 格式有以下格式可以填:
%H 小時(以 00-23 來表示)
%M 分鐘(以 00-59 來表示)
%S 秒(以本地的慣用法來表示)
%Y 年份(以四位數來表示)
%m 月份(以 01-12 來表示)
%d 日期(以 01-31 來表示)。           

時間擷取與轉換示例:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc,char **argv)
{
    /*1.擷取本地的秒機關時間*/
    time_t sec_time=time(NULL);
    printf("目前系統的總秒數:%d\n",sec_time);
    /*2. 将秒機關時間轉為字元串傳回*/
    char time_buff[100];
    ctime_r(&sec_time,time_buff);
    printf("字元串格式時間(系統預設):%s\n",time_buff);
    /*3. 将秒機關時間轉為tm結構體傳回*/
    struct tm tm_time;
    gmtime_r(&sec_time,&tm_time);
    printf("國際時間: %d-%d-%d %d:%d:%d\n",tm_time.tm_year+1900,
                                 tm_time.tm_mon+1,
                                 tm_time.tm_mday,
                                 tm_time.tm_hour,
                                 tm_time.tm_min,
                                 tm_time.tm_sec);

    localtime_r(&sec_time,&tm_time);
    printf("本地時間: %d-%d-%d %d:%d:%d\n",tm_time.tm_year+1900,
                                 tm_time.tm_mon+1,
                                 tm_time.tm_mday,
                                 tm_time.tm_hour,
                                 tm_time.tm_min,
                                 tm_time.tm_sec);
    /*4. 将tm結構體時間轉為秒機關傳回.*/
    printf("總秒數:%d\n",mktime(&tm_time));

    /*5. 将tm結構體時間格式按照指定格式轉為字元串*/
    strftime(time_buff,sizeof(time_buff),"%Y_%m_%d_%H_%M_%S.mp4",&tm_time);
    printf("time_buff=%s\n",time_buff);

    return 0;
}           

三、常用的一些延時函數

#include <unistd.h>
unsigned int sleep(unsigned int seconds);
函數功能: 秒機關的延時函數.

int usleep(useconds_t usec);
函數功能: 微秒機關的延時函數.

#include <time.h>
int nanosleep(const struct timespec *req, struct timespec *rem);
函數功能: 秒+納秒的延時函數.

struct timespec {
    time_t tv_sec;        /* seconds */
    long   tv_nsec;       /* nanoseconds */
};

以上的函數都是可中斷的延時函數。 
比如: 延時10秒,有可能10秒鐘還沒有到達,它可以被其他信号終止.           

示例代碼:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int main(int argc,char **argv)
{
    printf("1234\n");
    //sleep(5);
    //usleep(1000*1000);

    struct timespec req={5,1000}; //将要延時的時間
    struct timespec rem; //儲存是延時結束剩餘的時間
    nanosleep(&req,&rem);

    printf("5678\n");
    return 0;
}           

四、系統定時器信号: 鬧鐘信号

函數原型介紹:

#include <unistd.h>
unsigned int alarm(unsigned int seconds);
鬧鐘逾時之後會産生SIGALRM鬧鐘信号。

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
函數功能: 捕獲程序收到的信号.
函數參數:
int signum 要捕獲的信号
sighandler_t handler  捕獲信号之後調用的處理函數           
例子代碼:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>

void sighandler_func(int sig)
{
    printf("鬧鐘時間到達.\n");
    //定義一個鬧鐘
    alarm(1); //重複定時
}

int main(int argc,char **argv)
{
    //聲明要捕獲的信号
    signal(SIGALRM,sighandler_func);
    //定義一個鬧鐘
    alarm(1);
    while(1)
    {

    }
    return 0;
}           

運作效果:

[wbyq@wbyq linux_c]$ gcc app.c 
[wbyq@wbyq linux_c]$ ./a.out 
鬧鐘時間到達.
鬧鐘時間到達.
鬧鐘時間到達.
鬧鐘時間到達.
鬧鐘時間到達.           

繼續閱讀