Linux C/C++时间操作

  • C++11提供了操作时间的库chrono库,从语言级别提供了支持
  • chrono库屏蔽了时间操作的很多细节,简化了时间操作

Unix操作系统根据计算机产生的年代把1970年1月1日作为UNIX的纪元时间,1970年1月1日是时间的中间点,将从1970年1月1日起经过的秒数用一个整数存放

time_t

time_t用于表示时间类型,它是一个 long 类型的别名,在<time.h>文件中定义,表示从 1970 年1月1日0时0分0秒到现在的秒数

复制代码
long tt;
time_t tt;

typedef long time_t;

time()库函数

time0)库函数用于获取操作系统的当前时间

包含头文件:

复制代码
#include<time.h>

声明:

复制代码
time_t time(time t *tloc);

有两种调用方法:

复制代码
time_t now=time(0);// 将空地址传递给time()函数,并将 time()返回值赋给变量 now。
或
time_t now; time(&now);// 将变量 now 的地址作为参数传递给 time()函数。

tm结构体

time_t是一个长整数,需转换为tm结构体

复制代码
struct tm
{
  int tm_sec;			/* Seconds.	[0-60] (1 leap second) */
  int tm_min;			/* Minutes.	[0-59] */
  int tm_hour;			/* Hours.	[0-23] */
  int tm_mday;			/* Day.		[1-31] */
  int tm_mon;			/* Month.	[0-11] */
  int tm_year;			/* Year	- 1900.  */
  int tm_wday;			/* Day of week.	[0-6] */
  int tm_yday;			/* Days in year.[0-365]	*/
  int tm_isdst;			/* DST.		[-1/0/1]*/

# ifdef	__USE_BSD
  long int tm_gmtoff;		/* Seconds east of UTC.  */
  const char *tm_zone;		/* Timezone abbreviation.  */
# else
  long int __tm_gmtoff;		/* Seconds east of UTC.  */
  const char *__tm_zone;	/* Timezone abbreviation.  */
# endif
};

localtime()库函数

用于把time_t转换为tm结构体表示时间

localtime()函数不是线程安全的,licaltime_r()是线程安全的

包含头文件

复制代码
#include<time.h>

函数声明:

复制代码
struct tm *localtime(conmst time_t *timep);
struct tm *localtime_r(const time_t *timep,struct tm * result);

示例

复制代码
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
    long now;
    time(&now);

    cout<<"now="<<now<<endl;

    tm time;
    localtime_r(&now,&time);

    string str_time = to_string(time.tm_year+1900) +"-"+
                    to_string(time.tm_mon+1) +"-"+
                    to_string(time.tm_mday) +" "+
                    to_string(time.tm_hour) +":"+
                    to_string(time.tm_min) +":"+
                    to_string(time.tm_sec);
    cout<<"time="<<str_time<<endl;             

    return 0;
}

[root@localhost 05demotime]# ./demo_time 
now=1717311833
time=2024-6-2 15:3:53

mktime()库函数

mktime函数与localtime()函数相反,用于把tm结构体时间转换为time_t时间

包含头文件

复制代码
#include <time.h>

函数声明:

复制代码
time_t mktime(struct tm *tm);

该函数主要用于时间的运算,例如 把2022-10-01 15:30:25 加30分钟

思路:

  • 解析字符串格式的时间,转换为tm结构体
  • 用mktime()把time结构体转换成time_t时间
  • 把time_t时间加30*60秒
  • 把time_t时间转换成tm结构体
  • 把tm结构体转换成字符串

gettimeofday()库函数

用于获取1970年1月1日到现在的秒和当前秒中已逝去的微秒数,可以用于程序计时

包含头文件

复制代码
#include <sys/time.h>

函数声明

复制代码
int gettimeofday(struct timeval *tv,struct timezone *tz);

struct timeval
  {
     time_t tv_sec;		/* Seconds. 1970-1-1到现在秒数 */
     suseconds_t tv_usec;	/* Microseconds. 当前秒中已逝去的微秒数 */
  };

程序睡眠

如果把程序挂起一段时间,可以用sleep()和usleep()两个库函数

包含头文件

复制代码
#include<unistd.h>

函数声明

复制代码
unsigned int sleep(unsigned int seconds);
int usleep(useconds_t usec);

推荐一个零声学院项目课,个人觉得老师讲得不错,分享给大家:
零声白金学习卡(含基础架构/高性能存储/golang云原生/音视频/Linux内核)
https://xxetb.xet.tech/s/3Zqhgt

相关推荐
Lenyiin7 分钟前
《LeetCode 顺序刷题》61 - 70
java·c++·python·算法·leetcode·lenyiin
想唱rap16 分钟前
应用层HTTPS协议
服务器·网络·c++·网络协议·http·https
前端之虎陈随易21 分钟前
为什么今天还会有新语言?MoonBit 想解决什么问题?
大数据·linux·javascript·人工智能·算法·microsoft·typescript
G.晴天21 分钟前
Linux常用命令练习流程
java·linux·运维·服务器·tomcat
_F_y36 分钟前
仿RabbitMQ实现消息队列-客户端模块实现
c++·算法·rabbitmq
嵌入式×边缘AI:打怪升级日志37 分钟前
Linux 驱动开发核心自测题库(面试官问答版)
linux·运维·驱动开发
想唱rap38 分钟前
传输层协议之UDP
java·linux·网络·c++·网络协议·mysql·udp
春蕾夏荷_72829772541 分钟前
2、c++ acl tcp服务器客户端简单实例-客户端(2)
服务器·c++·tcp/ip
网安薯条1 小时前
Kali Linux 虚拟机安装与基础配置保姆级图文教程
linux·运维·网络·安全·web安全·网络安全
良木生香1 小时前
【Linux系统编程】Linux基础指令(1)---一文带你了解Linux的基础指令
linux·运维·服务器·ubuntu·机器学习·系统架构·centos