linux是否有计算时间的函数

49次阅读
没有评论

共计 6032 个字符,预计需要花费 16 分钟才能阅读完成。

这篇“linux 是否有计算时间的函数”文章的知识点大部分人都不太理解,所以丸趣 TV 小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“linux 是否有计算时间的函数”文章吧。

linux 有计算时间的函数,例如:可获取秒级时间差的函数 time()、可获取微秒级时间差的函数 gettimeofday() 和 settimeofday()、可获取纳秒级时间差的函数 clock_gettime() 等等。

1. 获取时间相关函数

1.1 获取秒级时间差函数

#include  time.h 
time_t time(time_t *timer);// 通过函数返回值或者 timer  变量均可以获取到当前时间 

time_t 实际上是一个长整型,表示 UTC 时间(1970 年 1 月 1 日 0 时 0 分 0 秒,Linux 系统的 Epoch 时间)到当前系统时间的秒数级时间差

1.2 获取微秒级时间差函数

#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*/
 
// 函数执行成功返回 0,失败返回 -1.  其中 timezone  是时区相关的结构体
int gettimeofday(struct timeval *tv, struct timezone *tz);
// 用来设置制定的时间和时区信息
int settimeofday(const struct timeval *tv, const struct timezone *gz);

1.3 获取纳秒级时间差函数

#include  time.h 
其中 clk_id  用来制定对应的时钟类型,不同的类型可以用来获取不同的时间值,具体有四种:CLOCK_REALTIME:  系统实时时间,从 UTC 开始计时,若时间被用户更改计数时间相应改变;CLOCK_MONOTONIC: 从系统启动开始计时,即使用户更改时间也没有影响;CLOCK_PROCESS_CPUTIME_ID: 本进程开始到执行到当前程序系统 CPU 花费的时间;CLOCK_THREAD_CPUTIME_ID: 本线程开始到执行到当前程序系统 CPU 花费的时间
struct timespec{
 time_t tv_sec; //s
 long tv_nsec; //ns
int clock_gettime(clockid_t clk_id, struct timespec* tp);

2. 转换时间相关函数

2.1 将上述获取时间函数获取到的时间参数 time_t 转换为结构体

struct tm,该结构体包含年月日等非常详细的域。下如所示:

#include  time.h 
 
struct tm{
 int tm_sec; // 秒
 int tm_min; // 分
 int tm_hour; // 时;取值区间为 [0, 23]
 int tm_mday; // 日;取值区间为 [1, 31]
 int tm_mon; // 月份;取值区间为 [0, 11]; 0 表示 1 月份依次递增到 12 月份
 int tm_year; // 年份;其值为 1900 年至今年数
 int tm_wday; // 星期;0 代表星期天,1 代表星期 1,以此类推
 int tm_yday; // 日期;0 代表 1 月 1 日
 int tm_isdst; // 夏令时标识符;使用夏令时为正,不使用 t 为 0,不确定时为负 */
};

将 time_t 转换成 struct tm 结构体常用的函数如下:

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

gmtime() 和 localtime() 函数可以将 time_t 数据格式转化成 tm 格式的类型。区别是 gmtime() 转换的结果是 GMT(中央时区)对应的信息,而 localtime() 函数转换的结果是当前所在时区的信息。

2.2 将 time_t 转换成我们习惯性使用的时间和日期字符串

对应转换函数如下:

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

2.3 将 struct tm 转换成 time_t

对应函数如下:

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

2.4 将 struct tm 转换成我们习惯性使用的时间和日期字符串

对应函数如下:

#include  time.h 
char *asctime(const struct tm *p_tm); // 习惯性字符串  Thu Dec 9 07:13:35 2021

2.5 将时间字符串转换成 struct tm 格式

/**************************************
** description:  将 struct tm  按照指定的 format 格式转化成字符串
** parameter:** *s :  需要被转换的时间字符串
 ** *format:时间字符串的格式
 ** *tm:转换后的 tm 时间
**************************************/
char *strptime(const char *s, const char *format, struct tm *tm);

2.6 将 struct tm 按照指定的 format 格式转化成字符串

/**************************************
** description:  将 struct tm  按照指定的 format 格式转化成字符串
** parameter:** *s :  生成的时间字符串
 ** max:  字符串最大字符数(即最大可生成的字符数量)** *format:生成的字符串格式
 ** *tm:需要被转换的 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 转换 time_t 成我们习惯性使用的时间和日期字符串  **************\n 
 pstr = ctime(times);
 printf(ctime:%s\n , pstr);
 printf( **************  使用 gmtime 转换 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 转换 struct tm 成我们习惯性使用的时间和日期字符串 **************\n 
 pstr = asctime(ptm);
 printf(asctime:%s\n\n , pstr);
 printf( **************  使用 localtime 转换 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;
}

特定的时间字符相互转换

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 ,  
 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 ,  
 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 ,  
 printf(pstr:%s\n , pstr);
}

若想获取从系统启动开始计时,即使用户更改时间也没有影响的时间,单位毫秒,举例如下:

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); // 获取失败重试,最大执行 3 次
 cnt++;
 }
 time = now.tv_sec * 1000 + now.tv_nsec / (1000000);
 return time;
}

以上就是关于“linux 是否有计算时间的函数”这篇文章的内容,相信大家都有了一定的了解,希望丸趣 TV 小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注丸趣 TV 行业资讯频道。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-12发表,共计6032字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)