Linux 中的時間函數

常見的延時函數

Linux系統中常用的延時函數爲sleep,usleep,包含在頭文件unistd.h中;

  • unsigned int sleep(unsigned int seconds);
    -延時成功返回0,失敗返回剩餘時間(秒)
  • int usleep(useconds_t usec);
    -usec需要小於1000000
    -延時成功返回0,失敗返回-1

Linux內核中常用的延時函數有ndelay,udelay,mdealy,包含在頭文件linux/delay.h中。

  • void ndelay(unsigned long nsecs); //納秒
  • void udelay(unsigned long usecs); //微秒
  • void mdelay(unsigned long msecs); //毫秒

標準時間的獲取與轉換

需要先了解一下UNIX TIME,UNIX TIME是指從 1970 年 1 月 1 日 00:00:00 UTC 開始所經過的秒數。
在 linux 中時間日期會以這種方式存儲時間,有效位是秒。例如經過了 1 分鐘,這個數值會變大 60 秒。
GMT 是指太陽經過英國格林尼治的時間,北京時間指的是東八區的時間,和格林尼治時間相差 8 個小時,所以北京的時間爲GMT+8。

Linux中的時間函數time
該函數 用於獲取當前系統時間

#include <time.h>

time_t time(time_t *tloc);

If tloc is non-NULL, the return value is also stored in the memory pointed to by tloc.
tloc爲空,返回值依舊會保存到指向tloc的內存中
On success, the value of time in seconds since the Epoch is returned.On error, ((time_t) -1) is returned, and errno is set appropriately.
出現錯誤返回-1。

所以,只需要下面兩句即可獲取當前系統的時間

time_t timep;
time(&timep);

Linux時間轉換函數
獲取到時間之後,要轉換爲年月日時分秒等這樣常規的時間顯示格式。
Linux中有一個tm結構體,可用於存儲標準格式的時間

struct tm
{
		int tm_sec;		//秒 0-59 		
		int tm_min;		//分 0-59
		int tm_hour;		//時 0-23
		int tm_mday;	//日 1-31
		int tm_mon;		//月 1-12
		 int tm_year;	//年 將其加上1900即爲當前實際年份
		int tm_wday;	//星期 0-6 0代表星期天
		int tm_yday;		//從每年的1月1日開始的天數 取值範圍 0-365 其中0表示1月1日
		int tm_isdst;		//夏令時標識符,實行夏令時時,tm_isdst爲正,不實行夏令時時爲0,不瞭解情況時爲負
};

函數c_time:char *ctime(const time_t *timep); //將時間轉換爲字符串格式
函數gmt_time:struct tm *gmtime(const time_t *timep); //將時間轉換爲格林威治時間,以tm結構體存儲
函數asctime:char *asctime(const struct tm *tm); //將時間轉換爲字符格式,函數的參數是tm結構的
函數localtime:struct tm *localtime(const time_t *clock); //將時間轉換爲本地時間

int main()
{
    time_t timep;
    struct tm *tblock;

    time(&timep);       //獲取當前時間
    printf("UTC time:0x%08x\n\n", timep);     
    
    printf("ctime/timep is %s\n", ctime(&timep));       //將時間轉換爲字符串
    printf("asctime is %s\n", asctime(gmtime(&timep))); //將時間轉換爲格林威治時間並再轉換爲字符串
    
    tblock = localtime(&timep);     //獲取當地時間並存儲到結構體中
    
    printf("localtime is :%s\n", asctime(tblock));
    printf("localtime is :%s\n", ctime(&timep));

    return 0;
}

運行結果如下

UTC time:0x4fb94f5e

ctime/timep is Sun May 20 20:09:02 2012

asctime is Sun May 20 20:09:02 2012

localtime is :Sun May 20 20:09:02 2012

localtime is :Sun May 20 20:09:02 2012


獲取高精度時間函數與設置時間函數

高精度時間函數
高精度時間函數在時間精度上達到了微秒級別,可以使用設個函數來計算某段代碼執行所花費的時間。

int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);

上面兩個時間函數都包含在頭文件<sys/time.h>中
其中的兩個結構體分別爲

struct timeval {
time_t tv_sec;					/* seconds */
suseconds_t tv_usec;		/* microseconds */
};

struct timezone {
int tz_minuteswest;		/* minutes west of Greenwich,格林威治時間的時差 */
int tz_dsttime;				/* type of DST correction,時間修正的方式 */
};
#include <stdio.h>
#include <time.h>
#include <sys/time.h>


int main(int argc, char  *argv[])
{
    time_t gtime;           //用於獲取系統當前時間
    struct timeval stime;

    stime.tv_sec = atoi(argv[1]);   //UNIX時間 秒
    stime.tv_usec = 0;              //微秒直接設置爲0
    
    time(&gtime);
    printf("asctime is %s\n", asctime(gmtime(&gtime)));     //顯示系統當前時間

    settimeofday(&stime,NULL);                              //將系統時間更改爲輸入的時間
    time(&gtime);
    printf("asctime is %s\n", asctime(gmtime(&gtime))); 

    return 0;
}

運行結果如下

[root@iTOP-4412]# ./mnt/udisk/settime 1554028024

asctime is Sun May 20 23:25:25 2012

asctime is Sun Mar 31 10:27:04 2019

然後開發板再執行hwclock -w即可將更新的時間寫入硬件中

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