C++时间库和一些应用

Cpp时间库

在这里插入图片描述

时间计算、日期转换和时间点操作

ctime

  • 标准库
  • 提供了与时间相关的类型和函数,包括对time_t类型的操作,以及转换时间和日期的函数

类型

  • time_t:表示时间的类型,通常是一个长整型。
  • tm:一个结构体,用于表示时间的各个部分,如年、月、日、小时等。
    struct tm {  
    int tm_sec; /* seconds after the minute - [0,59] */  
    int tm_min; /* minutes after the hour - [0,59] */  
    int tm_hour; /* hours since midnight - [0,23] */  
    int tm_mday; /* day of the month - [1,31] */  
    int tm_mon; /* months since January - [0,11] */  
    int tm_year; /* years since 1900 */  
    int tm_wday; /* days since Sunday - [0,6] */  
    int tm_yday; /* days since January 1 - [0,365] */  
    int tm_isdst; /* daylight savings time flag */  
    };
    

函数

  • 返回系统的当前日历时间,自 1970 年 1 月 1 日以来经过的秒数。如果系统没有时间,则返回 .1。
    time_t time(time_t *time);
    
  • 返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year/n/0
    char *ctime(const time_t *time);
    
  • 函数返回一个指向表示本地时间的 tm 结构的指针。
    struct tm *localtime(const time_t *time);
    
  • 返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 .1。
    clock_t clock(void);
    
  • 返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year/n/0
    char * asctime ( const struct tm * time ); 该函数
    
  • 返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
    struct tm *gmtime(const time_t *time); 
    
  • 返回日历时间,相当于 time 所指向结构中存储的时间。
    time_t mktime(struct tm *time);
    
  • 返回 time1 和 time2 之间相差的秒数。
    double difftime ( time_t time2, time_t time1 );
    
  • 可用于格式化日期和时间为指定的格式。
    size_t strftime();
    

语法

以下是 <ctime> 库中一些常用函数的基本语法:

  • 获取当前时间(以秒为单位,从1970年1月1日开始计算):
    time_t t = time(NULL);
    
  • time_t 类型的时间转换为 tm 结构体:
    struct tm *tm = localtime(&t);
    
  • time_t 类型的时间转换为协调世界时(UTC)的 tm 结构体:
    struct tm *tm_utc = gmtime(&t);
    
  • 格式化时间:
    char buffer[80];
    strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", tm);
    

chrono

时间颗粒度

//chrono重载了各种运算符
    std::chrono::hours             c1(1);                //1小时
    std::chrono::minutes           c2(60);               //60分钟
    std::chrono::seconds           c3(60*60);            //60*60s
    std::chrono::milliseconds      c4(60*60*1000);       //60*60*1000毫秒
    std::chrono::microseconds      c5(60*60*1000*1000);  //微秒 溢出
    std::chrono::nanoseconds       c6(60*1000*1000*1000);//纳秒 溢出

clock类型的时钟

	std::chrono::system_clock:  类似Windows系统右下角那个时钟(可调整)
	std::chrono::steady_clock:  以统一的速率运行(类似于unix时间或者北京时间)
	std::chrono::high_resolution_clock: 高分辨率时钟(但引入本身也会消耗算力的)

这3个时钟类都提供了一个静态成员函数now()用于获取当前时间,该函数的返回值是一个time_point类型。
high_resolution_clock和system_clock除了①now()函数外,还提供了②to_time_t()静态成员函数。用于将系统时间转换成熟悉的std::time_t类型,得到了time_t类型的值,再使用③ctime()函数将时间转换成字符串格式,就可以很方便地打印当前时间了;

duration

duration_cast用于将duration转换成另一个类型的duration,可回看刚开始介绍的chrono组件中时间的颗粒度!

duration还有一个成员函数count(),用来表示这一段时间的长度

 auto tNow=high_resolution_clock::to_time_t(sta);
 std::cout<<"start time="<<tNow<<std::endl;
 auto end = std::chrono::system_clock::now();

 auto dur=std::chrono::duration_cast<microseconds>(end-sta);
 cout<<"程序花费的时间="<<dur.count()<<"nano"<<endl;

其中std::chrono::duration_cast<>中的模板参数,可以是时间的颗粒度中的任意一种类型,但返回会是整数,如果单位放的比较大,比如是seconds但是实际运行不到1second,那么返回的参数会是0,也就是说返回的是整数,小数全被舍掉了

应用

以毫秒单位为时间戳

源码

#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>

...

// 毫秒为单位的时间戳(可从外部传入)
uint64_t millis = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();

// 秒为单位
auto seconds = millis / 1000;
std::time_t t = seconds;
struct tm *time = std::localtime(&t);

// 使用put_time将tm格式化为字符串,组合毫秒形式的字符串
std::cout << std::put_time(time, "%Y-%m-%d %H:%M:%S")<< '.' << std::setfill('0') << std::setw(3) << (millis % 1000) << std::endl;

结果输出

2024-10-09 14:59:27.509

计时器

源码

#include <chrono>
#include <iostream>
#include <thread>

...

auto start = std::chrono::high_resolution_clock::now();

// 模拟一些操作
...
std::this_thread::sleep_for(std::chrono::seconds(2));

auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "花费了" << duration.count() << "毫秒" << std::endl;

输出结果

花费了2000毫秒
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值