网站首页 > 基础教程 正文
1 两种时间
linux提供了两种时间:
(1)日历时间。该值是自 协调世界时(Coordinated Universal Time, UTC)1970年1月1日00::00::00这个特定时间以来所经过的秒数累计值。(早期称UTC为格林尼治标准时间)
系统基本数据类型time_t用于保存这种时间值。
(2)进程时间。也被成为CPU时间,用以度量进程使用的中央处理器资源。进程时间以时钟滴答计算。调用sysconf函数可以获得每秒得时间滴答数。
系统基本数据类型clock_t用于保存这种时间值。
为了度量一个进程的执行时间,linux系统维护了3个进程时间值:
(1)时钟时间; //进程运行的时间总量
(2)用户CPU时间; //执行用户指令所用时间(用户态时间)
(3)系统CPU时间; //执行内核程序所用时间(内核态时间)
用户cpu时间和系统cpu时间总和称为cpu时间。
执行time命令,可以取得任一进程的时钟时间、用户时间和系统时间。
2 获取时间相关函数
2.1 获取秒级时间函数
#include <time.h>
time_t time(time_t *timer);//通过函数返回值或者timer 变量均可以获取到当前时间
time_t实际上是一个长整型,表示UTC时间(1970年1月1日0时0分0秒,Linux系统的Epoch时间)到当前系统时间的秒数级时间差
2.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);
2.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);
当时钟类型设置为CLOCK_REALTIME时,clock_gettime函数提供了与time函数类似的功能,不过在系统支持高精度值得情况下,clock_gettime可能比time函数得到更高精度的时间值。
举例:若想获取从系统启动开始计时,即使用户更改时间也没有影响的时间,单位微秒,如下:
int64_t get_time_point_monotonic() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000L + ts.tv_nsec / 1000;
}
3 转换时间相关函数
3.1 将time_t转换为结构体struct tm
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()转换的结果是UTC对应的信息,而localtime() 函数转换的结果是当前所在时区的信息。
3.2 将time_t转换成我们习惯性使用的时间和日期字符串
对应转换函数如下:
#include <time.h>
char* ctime(time_t* timep);
3.3 将struct tm 转换成 time_t
对应函数如下:
#include <time.h>
time_t mktime(struct tm *p_tm);
3.4 将struct tm转换成我们习惯性使用的时间和日期字符串
对应函数如下:
#include <time.h>
char *asctime(const struct tm *p_tm); //习惯性字符串 Thu Dec 9 07:13:35 2021
3.5 将时间字符串转换成 struct tm格式
/**************************************
** description: 将struct tm 按照指定的format格式转化成字符串
** parameter:
** *s : 需要被转换的时间字符串
** *format:时间字符串的格式
** *tm:转换后的tm时间
**************************************/
char *strptime(const char *s, const char *format, struct tm *tm);
3.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);
猜你喜欢
- 2024-10-19 Python3 datetime模块指南:日期时间操作、时区管理与实战案例
- 2024-10-19 C++及数据结构复习笔记,类和对象很简单?为啥好多程序员还不会
- 2024-10-19 C/C++的8种时间度量方式以及代码片段
- 2024-10-19 Python实战:使用 datetime模块处理时间日期的全方位指南
- 2024-10-19 C++编程的 42 条建议(四) c++编程100例
- 2024-10-19 mount with noatime - 合理关闭atime提高服务器性能
- 2024-10-19 linux下连续三次fork()——深度理解进程创建函数
- 2024-10-19 C函数time和clock的计时区别 c计时器函数
- 2024-10-19 win进程弹出Microsoft Visual C++ Runtime Library的解决办法
- 2024-10-19 库函数——ctime()和ctime不安全函数
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)