linux是否有计算时间的函数

服务器   发布日期:2025年03月11日   浏览次数:223

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

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

1.获取时间相关函数

1.1 获取秒级时间差函数

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

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

1.2 获取微秒级时间差函数

  1. #include <sys/time.h>
  2. #include <unistd.h>
  3. struct timeval {
  4. time_t tv_sec; /* seconds */
  5. suseconds_t tv_usec; /* microseconds */
  6. };
  7. struct timezone{
  8. int tz_minuteswest; /*miniutes west of Greenwich*/
  9. int tz_dsttime; /*type of DST correction*/
  10. };
  11. //函数执行成功返回0,失败返回-1. 其中timezone 是时区相关的结构体
  12. int gettimeofday(struct timeval *tv, struct timezone *tz);
  13. //用来设置制定的时间和时区信息
  14. int settimeofday(const struct timeval *tv, const struct timezone *gz);

1.3获取纳秒级时间差函数

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

2.转换时间相关函数

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

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

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

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

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

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

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

对应转换函数如下:

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

2.3 将struct tm 转换成 time_t

对应函数如下:

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

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

对应函数如下:

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

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

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

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

  1. /**************************************
  2. ** description: 将struct tm 按照指定的format格式转化成字符串
  3. ** parameter:
  4. ** *s : 生成的时间字符串
  5. ** max: 字符串最大字符数(即最大可生成的字符数量)
  6. ** *format:生成的字符串格式
  7. ** *tm:需要被转换的tm时间
  8. **************************************/
  9. size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);

3.举例

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. int main(int argc, char *argv[])
  7. {
  8. char *pstr = NULL;
  9. struct tm *ptm = NULL;
  10. printf("************** 使用ctime获取时间time_t **************
  11. ");
  12. time_t times = 0;
  13. time(&times);
  14. printf("time_t:%ld
  15. ", times);
  16. printf("************** 使用ctime转换time_t成我们习惯性使用的时间和日期字符串 **************
  17. ");
  18. pstr = ctime(&times);
  19. printf("ctime:%s
  20. ", pstr);
  21. printf("************** 使用gmtime转换time_t成struct tm 时间和日期**************
  22. ");
  23. ptm = gmtime(&times);
  24. printf("time : %d:%d:%d
  25. ", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  26. printf("date: %d:%d:%d
  27. ", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
  28. printf("year: wday:%d yday:%d isdst:%d
  29. ", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
  30. printf("************** 使用asctime转换struct tm成我们习惯性使用的时间和日期字符串**************
  31. ");
  32. pstr = asctime(ptm);
  33. printf("asctime:%s
  34. ", pstr);
  35. printf("************** 使用localtime转换time_t成struct tm 时间和日期**************
  36. ");
  37. ptm = localtime(&times);
  38. printf("time : %d:%d:%d
  39. ", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  40. printf("date: %d:%d:%d
  41. ", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
  42. printf("year: wday:%d yday:%d isdst:%d
  43. ", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
  44. pstr = asctime(ptm);
  45. printf("asctime:%s
  46. ", pstr);
  47. printf("************** 使用gettimeofday获取微秒级的时间**************
  48. ");
  49. struct timeval tv;
  50. struct timezone tz;
  51. gettimeofday(&tv, &tz);
  52. ptm = localtime(&tv.tv_sec);
  53. printf("time : %d:%d:%d
  54. ", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  55. printf("date: %d:%d:%d
  56. ", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
  57. printf("year: wday:%d yday:%d isdst:%d
  58. ", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
  59. printf("tv_usec:%ld
  60. ", tv.tv_usec);
  61. printf("************** 使用clock_gettime获取纳秒级的时间**************
  62. ");
  63. struct timespec tp;
  64. clock_gettime(CLOCK_REALTIME, &tp);
  65. ptm = localtime(&tp.tv_sec);
  66. printf("time : %d:%d:%d
  67. ", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  68. printf("date: %d:%d:%d
  69. ", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday);
  70. printf("year: wday:%d yday:%d isdst:%d
  71. ", ptm->tm_wday, ptm->tm_yday, ptm->tm_isdst);
  72. printf("tp.tv_nsec:%ld
  73. ", tp.tv_nsec);
  74. return 0;
  75. }

特定的时间字符相互转换

  1. int str_to_time(void)
  2. {
  3. char pstr[128] = {0};
  4. struct tm t;
  5. strptime("2021-04-23 12:34:56", "%Y-%m-%d %H:%M:%S", &t);
  6. printf("**** tm_isdst: %d, tm_yday:%d, tm_wday%d,
  7. %d-%d-%d
  8. %d:%d:%d
  9. ",
  10. t.tm_isdst, t.tm_yday, t.tm_wday, t.tm_year+1900, t.tm_mon+1, t.tm_mday,
  11. t.tm_hour, t.tm_min, t.tm_sec);
  12. printf("mktime ts:%ld
  13. ", mktime(&t));
  14. printf("asctime:%s
  15. ", asctime(&t));
  16. strftime(pstr, sizeof(pstr), "%Y-%m-%d %H:%M:%S", &t);
  17. printf("pstr:%s
  18. ", pstr);
  19. }
  20. int time_to_str(void)
  21. {
  22. char pstr[128] = {0};
  23. struct tm t = {
  24. .tm_sec = 56, /* Seconds (0-60) */
  25. .tm_min = 34, /* Minutes (0-59) */
  26. .tm_hour = 12, /* Hours (0-23) */
  27. .tm_mday = 23, /* Day of the month (1-31) */
  28. .tm_mon = 4-1, /* Month (0-11) */
  29. .tm_year = 2021-1900, /* Year - 1900 */
  30. .tm_wday = 5, /* Day of the week (0-6, Sunday = 0) */
  31. .tm_yday = 113, /* Day in the year (0-365, 1 Jan = 0) */
  32. .tm_isdst = 0, /* Daylight saving time */
  33. };
  34. strftime(pstr, sizeof(pstr), "%Y-%m-%d %H:%M:%S", &t);
  35. printf("pstr:%s
  36. ", pstr);
  37. }

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

  1. unsigned long long clock_systick_get(void)
  2. {
  3. int ret = -1;
  4. unsigned long long time;
  5. int cnt = 0;
  6. struct timespec now = {0, 0};
  7. while (ret < 0 && cnt < 3)
  8. {
  9. ret = clock_gettime(CLOCK_MONOTONIC, &now); //获取失败重试,最大执行3次
  10. cnt++;
  11. }
  12. time = now.tv_sec * 1000 + now.tv_nsec / (1000000);
  13. return time;
  14. }

以上就是linux是否有计算时间的函数的详细内容,更多关于linux是否有计算时间的函数的资料请关注九品源码其它相关文章!