溫馨提示×

溫馨提示×

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

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

linux是否有計算時間的函數(shù)

發(fā)布時間:2023-04-19 10:25:51 來源:億速云 閱讀:126 作者:iii 欄目:建站服務(wù)器

這篇“l(fā)inux是否有計算時間的函數(shù)”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“l(fā)inux是否有計算時間的函數(shù)”文章吧。

linux有計算時間的函數(shù),例如:可獲取秒級時間差的函數(shù)time()、可獲取微秒級時間差的函數(shù)gettimeofday()和settimeofday()、可獲取納秒級時間差的函數(shù)clock_gettime()等等。

1.獲取時間相關(guān)函數(shù)

1.1 獲取秒級時間差函數(shù)

#include <time.h>
time_t time(time_t *timer);//通過函數(shù)返回值或者timer 變量均可以獲取到當(dāng)前時間

time_t實際上是一個長整型,表示UTC時間(1970年1月1日0時0分0秒,Linux系統(tǒng)的Epoch時間)到當(dāng)前系統(tǒng)時間的秒數(shù)級時間差

1.2 獲取微秒級時間差函數(shù)

#include <sys/time.h>
#include <unistd.h>
struct timeval {
    time_t       tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};

struct timezone{
 
    int tz_minuteswest; /*miniutes west of Greenwich*/
 
    int tz_dsttime; /*type of DST correction*/
 
};

//函數(shù)執(zhí)行成功返回0,失敗返回-1. 其中timezone 是時區(qū)相關(guān)的結(jié)構(gòu)體
int gettimeofday(struct timeval *tv, struct timezone *tz);

//用來設(shè)置制定的時間和時區(qū)信息
int settimeofday(const struct timeval *tv, const struct timezone *gz);

1.3獲取納秒級時間差函數(shù)

#include <time.h>

/*
其中clk_id 用來制定對應(yīng)的時鐘類型,不同的類型可以用來獲取不同的時間值,具體有四種:
CLOCK_REALTIME: 			系統(tǒng)實時時間,從UTC開始計時,若時間被用戶更改計數(shù)時間相應(yīng)改變;
CLOCK_MONOTONIC:			從系統(tǒng)啟動開始計時,即使用戶更改時間也沒有影響;
CLOCK_PROCESS_CPUTIME_ID:	本進程開始到執(zhí)行到當(dāng)前程序系統(tǒng)CPU花費的時間;
CLOCK_THREAD_CPUTIME_ID:	本線程開始到執(zhí)行到當(dāng)前程序系統(tǒng)CPU花費的時間

*/

struct timespec{
    time_t tv_sec;    //s
    long tv_nsec;    //ns
};

int clock_gettime(clockid_t clk_id, struct timespec* tp);

2.轉(zhuǎn)換時間相關(guān)函數(shù)

2.1將上述獲取時間函數(shù)獲取到的時間參數(shù)time_t轉(zhuǎn)換為結(jié)構(gòu)體

struct tm,該結(jié)構(gòu)體包含年月日等非常詳細的域。下如所示:

#include <time.h>
 
struct tm{
    int tm_sec;   //秒
    int tm_min;   //分
    int tm_hour;  //時;取值區(qū)間為[0, 23]
    int tm_mday;  //日;取值區(qū)間為[1, 31]
    int tm_mon;   //月份;取值區(qū)間為[0, 11]; 0表示1月份依次遞增到12月份
    int tm_year;   //年份;其值為1900年至今年數(shù)
    int tm_wday;  //星期;0代表星期天,1代表星期1,以此類推
    int tm_yday;   //日期;0代表1月1日
    int tm_isdst;   //夏令時標(biāo)識符;使用夏令時為正,不使用t為0,不確定時為負*/
};

將time_t轉(zhuǎn)換成struct tm結(jié)構(gòu)體常用的函數(shù)如下:

#include <time.h> 
struct tm* gmtime(const time_t* timep);
struct tm*localtime(const time_t* timep);

gmtime() 和localtime() 函數(shù)可以將time_t 數(shù)據(jù)格式轉(zhuǎn)化成tm格式的類型。區(qū)別是gmtime()轉(zhuǎn)換的結(jié)果是GMT(中央時區(qū))對應(yīng)的信息,而localtime() 函數(shù)轉(zhuǎn)換的結(jié)果是當(dāng)前所在時區(qū)的信息。

2.2將time_t轉(zhuǎn)換成我們習(xí)慣性使用的時間和日期字符串

對應(yīng)轉(zhuǎn)換函數(shù)如下:

#include <time.h>
char* ctime(time_t* timep);

2.3 將struct tm 轉(zhuǎn)換成 time_t

對應(yīng)函數(shù)如下:

#include <time.h>
time_t mktime(struct tm *p_tm);

2.4將struct tm轉(zhuǎn)換成我們習(xí)慣性使用的時間和日期字符串

對應(yīng)函數(shù)如下:

#include <time.h>
char *asctime(const struct tm *p_tm); //習(xí)慣性字符串 Thu Dec  9 07:13:35 2021

2.5 將時間字符串轉(zhuǎn)換成 struct tm格式

/**************************************
** description: 將struct tm 按照指定的format格式轉(zhuǎn)化成字符串
** parameter:
		** *s : 需要被轉(zhuǎn)換的時間字符串
		** *format:時間字符串的格式
		** *tm:轉(zhuǎn)換后的tm時間
**************************************/
char *strptime(const char *s, const char *format, struct tm *tm);

2.6 將struct tm 按照指定的format格式轉(zhuǎn)化成字符串

/**************************************
** description: 將struct tm 按照指定的format格式轉(zhuǎn)化成字符串
** parameter:
		** *s : 生成的時間字符串
		** max: 字符串最大字符數(shù)(即最大可生成的字符數(shù)量)
		** *format:生成的字符串格式
		** *tm:需要被轉(zhuǎn)換的tm時間
**************************************/
size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);

3.舉例

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

int main(int argc, char *argv[])
{	
	char *pstr = NULL;
	struct tm *ptm = NULL;

	printf("************** 使用ctime獲取時間time_t **************\n");
	time_t times = 0;
	time(&times);
	printf("time_t:%ld\n\n", times);
	
	printf("************** 使用ctime轉(zhuǎn)換time_t成我們習(xí)慣性使用的時間和日期字符串 **************\n");
	pstr = ctime(&times);
	printf("ctime:%s\n", pstr);
	
	printf("************** 使用gmtime轉(zhuǎn)換time_t成struct tm 時間和日期**************\n");
	ptm = gmtime(&times);
	printf("time : %d:%d:%d\n", ptm->tm_hour,  ptm->tm_min,  ptm->tm_sec);
	printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
	printf("year: wday:%d yday:%d isdst:%d\n\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
	
	printf("************** 使用asctime轉(zhuǎn)換struct tm成我們習(xí)慣性使用的時間和日期字符串**************\n");
	pstr = asctime(ptm);
	printf("asctime:%s\n\n", pstr);
	
	printf("************** 使用localtime轉(zhuǎn)換time_t成struct tm 時間和日期**************\n");
	ptm = localtime(&times);
	printf("time : %d:%d:%d\n", ptm->tm_hour,  ptm->tm_min,  ptm->tm_sec);
	printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
	printf("year: wday:%d yday:%d isdst:%d\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);

	pstr = asctime(ptm);
	printf("asctime:%s\n\n", pstr);

	printf("************** 使用gettimeofday獲取微秒級的時間**************\n");
	struct timeval tv;
	struct timezone tz;
	gettimeofday(&tv, &tz);
	ptm = localtime(&tv.tv_sec);
	printf("time : %d:%d:%d\n", ptm->tm_hour,  ptm->tm_min,  ptm->tm_sec);
	printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
	printf("year: wday:%d yday:%d isdst:%d\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
	printf("tv_usec:%ld\n\n", tv.tv_usec);
	
	printf("************** 使用clock_gettime獲取納秒級的時間**************\n");
	struct timespec tp;
	clock_gettime(CLOCK_REALTIME, &tp);
	ptm = localtime(&tp.tv_sec);
	printf("time : %d:%d:%d\n", ptm->tm_hour,  ptm->tm_min,  ptm->tm_sec);
	printf("date: %d:%d:%d\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
	printf("year: wday:%d yday:%d isdst:%d\n", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);	
	printf("tp.tv_nsec:%ld\n\n", tp.tv_nsec);
	return 0;
}

特定的時間字符相互轉(zhuǎn)換

int str_to_time(void)
{
	char pstr[128] = {0};
    struct tm t;
    strptime("2021-04-23 12:34:56", "%Y-%m-%d %H:%M:%S", &t);
	
    printf("**** tm_isdst: %d, tm_yday:%d, tm_wday%d,\n %d-%d-%d \n %d:%d:%d\n", 
    t.tm_isdst, t.tm_yday, t.tm_wday, t.tm_year+1900, t.tm_mon+1, t.tm_mday,
    t.tm_hour, t.tm_min, t.tm_sec);
    printf("mktime ts:%ld\n", mktime(&t));
	printf("asctime:%s\n", asctime(&t));
	
	strftime(pstr, sizeof(pstr), "%Y-%m-%d %H:%M:%S", &t);
    printf("pstr:%s\n", pstr);
}

int time_to_str(void)
{
	char pstr[128] = {0};
    struct tm t = {
               .tm_sec = 56,    /* Seconds (0-60) */
               .tm_min = 34,    /* Minutes (0-59) */
               .tm_hour = 12,   /* Hours (0-23) */
               .tm_mday = 23,   /* Day of the month (1-31) */
               .tm_mon = 4-1,     /* Month (0-11) */
               .tm_year = 2021-1900,   /* Year - 1900 */
               .tm_wday = 5,     /* Day of the week (0-6, Sunday = 0) */
               .tm_yday = 113,   /* Day in the year (0-365, 1 Jan = 0) */
               .tm_isdst = 0,    /* Daylight saving time */
           };
    strftime(pstr, sizeof(pstr), "%Y-%m-%d %H:%M:%S", &t);
    printf("pstr:%s\n", pstr);
}

若想獲取從系統(tǒng)啟動開始計時,即使用戶更改時間也沒有影響的時間,單位毫秒,舉例如下:

unsigned long long clock_systick_get(void)
{
    int ret = -1;
    unsigned long long time;
    int cnt = 0;
    struct timespec  now = {0, 0};

    while (ret < 0 && cnt < 3)
    {
        ret = clock_gettime(CLOCK_MONOTONIC, &now);	//獲取失敗重試,最大執(zhí)行3次
        cnt++;
    }
    time = now.tv_sec * 1000 + now.tv_nsec / (1000000);
    return time;
}

以上就是關(guān)于“l(fā)inux是否有計算時間的函數(shù)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

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

AI