Linux——时间

写在前面:此系列主要参考自UNIX系统编程手册,将会有大量demo

书籍链接:微云链接

日历时间

无论地理位置如何,Unix内部对时间的表示均是自1970.1.1(Unix大致问世的时间)以来的秒数时间表示。日历时间存储与类型为time_t的变量中

有关时间的API: gettimeofday 和 time

#include <sys/time.h>

int gettimeofdat(struct timeval *tv,struct timezone*tz)
/*
* 作用:向tv指向的结构体去中返回日历时间(更为精确,微秒级)
* 返回值:成功返回0,失败返回-1
* tv:存储日历时间的指针
* tz:现已被废弃,传入null
*/

以下这个常用,重点!!

#include <time.h>

time_t time(time_t* timep);
/*
* 作用:返回自1970.1.1 以来的秒数
* 返回值:成功返回秒数,失败返回-1
* timep:传null
*/

时间转换为固定格式API

图来!!!
在这里插入图片描述
以上图中所示函数频闭了时区、夏令时和地区的影响

time_t 转换为可打印格式:ctime

#include <time.h>

char* ctime(const time_t* timep);
/*
* 作用:将time_t类型转换为固定格式打印
* 返回值:一个26字节的字符串,内含标准格式的时间和日期
* timep:time_t类型的参数
*/

一个 简单的demo:

#include<time.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
	time_t tm = time(nullptr);
	cout<<"current time: "<<ctime(&tm)<<endl;
}

输出:

ik@ik-virtual-machine:~/桌面/test/bin$ ./test1
current time: Thu Apr  1 15:03:23 2021

分解 time_t 时间:gmtime 和 localtime

#include <time.h>
struct tm* gmtime(const time_t* timep);
/*
* 作用:转换为UTC时间
* 返回值:成功返回一个指向tm结构的指针吗,失败返回 null
* timep:time返回值
*/

struct tm* localtime(const time_t *timep);
/*
* 作用:转换日历时间,需要考虑时区
* 返回值:成功返回一个指向tm结构的指针吗,失败返回 null
* timep:time返回值
*/

struct tm
{
	int tm_sec;		//秒 	0-60	(因为闰秒缘故)
	int tm_min;		//分钟 	0-59
	int tm_hour;	//小时 	0-23
	int tm_mday;	//一个月的第几天 	1-31
	int tm_mon;		//月	0-11
	int tm_year;	//年 	自1900年起
	int tm_wday;	//一周的第几天	sunday=0
	int tm_yday;	//一年的第几天	0-365;1 jan=0
	int tm_isdst;	
};

一个简单的demo:

#include <time.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
	time_t ret_time = time(nullptr);
	tm *ret_tm = gmtime(&ret_time);
	cout << "sec: " << ret_tm->tm_sec << endl;
	cout << "minutes: " << ret_tm->tm_min << endl;
	cout << "hour: " << ret_tm->tm_hour << endl;
}

输出:

ik@ik-virtual-machine:~/桌面/test/bin$ ./test1
sec: 13
minutes: 37
hour: 7

分解时间和打印格式之间的转换:asctime

从参数tm中提供一个指向分解时间结构的指针,asctime会返回一指针,指向经由静态分配的字符串,内含时间,格式则与ctime相同。

#include <time.h>

char* asctime(const struct tm *timeptr);
/*
* 返回值:成功返回一个指向静态分配区域的指针,失败返回null
* timepter:指向tm结构的指针
*/
#include <time.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
	time_t ret_time = time(nullptr);
	tm *ret_tm = gmtime(&ret_time);
	cout << asctime(ret_tm) << endl;
}

输出:

ik@ik-virtual-machine:~/桌面/test/bin$ ./test1
Thu Apr  1 07:59:49 2021

进程时间

进程时间是进程创建后使用的CPU时间数量。出于记录的目的,内核吧CPU时间分成以下两部分:

  • 用户CPU时间: 是指在用户模式下执行所花费的时间数量
  • 系统CPU时间: 实在内核模式中执行所花费的时间数量

有时候,进程实际按是之处理过程中所消耗的总CPU时间

系统调用times,检索进程时间信息,并发结果通过buf指向的结构体返回。

#include <sys/times.h>
clock_t times(struct tms* buf);

/*
* 返回值:成功返回时钟计时单元为单位度量实际按的整型值
* buf:用于存储时间信息
*/

struct tms
{
	clock_t tms_utime;		//调用进程目前使用的用户时间
	clock_t tms_stime;		//调用进程目前使用的系统时间
	clock_t tms_cutime;		//父进程执行系统调用wait所有已经终止的子进程使用的CPU时间
	clock_t tms_cstime;
};
#include <time.h>

clock_t clock();
/*
* 返回值:成功返回一个描述系统使用CPU时间的值,失败返回-1
*/

一个简单的demo:

#include <time.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
	cout << clock() << endl;
}

输出:

ik@ik-virtual-machine:~/桌面/test/bin$ ./test1
2002

参考文献

[1] UNIX 系统编程手册(上) 第十章 时间
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shenmingik

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值