前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++中时间相关函数用法详解

C++中时间相关函数用法详解

作者头像
Linux兵工厂
发布2024-04-01 18:12:10
770
发布2024-04-01 18:12:10
举报
文章被收录于专栏:Linux兵工厂Linux兵工厂

START

Hi,大家好!在平时编程中我们往往有需要计算时间等相关需求,本节我们详细介绍下C++标准中时间相关的chrono库。

C++标准库中的 <chrono> 头文件提供了一套用于处理时间的工具,包括时钟、时间点和持续时间等。下面是 <chrono> 库的一些主要组件及其使用示例:

时钟(Clocks):时钟是一种用于度量时间的设备,C++标准库提供了几种不同的时钟类型,包括系统时钟、高精度时钟和稳定时钟。

示例:

代码语言:javascript
复制
#include <chrono>
#include <iostream>

int main() {
    // 获取当前系统时钟时间点
    auto now = std::chrono::system_clock::now();

    // 将时间点转换为时间戳
    auto timestamp = std::chrono::system_clock::to_time_t(now);

    // 打印时间戳
    std::cout << "Timestamp: " << timestamp << std::endl;

    return 0;
}

时间点(Time points):时间点是时钟的特定实例化,代表特定时钟上的一个瞬时点。

示例:

代码语言:javascript
复制
#include <chrono>
#include <iostream>

int main() {
    // 使用系统时钟获取当前时间点
    auto now = std::chrono::system_clock::now();

    // 使用 steady_clock 获取时间点
    auto start = std::chrono::steady_clock::now();

    // 一些操作...

    // 使用 steady_clock 获取结束时间点
    auto end = std::chrono::steady_clock::now();

    // 计算持续时间
    auto duration = end - start;

    // 打印持续时间
    std::cout << "Duration: " << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << " milliseconds" << std::endl;

    return 0;
}

持续时间(Durations):持续时间表示两个时间点之间的时间段。

示例:

代码语言:javascript
复制
#include <chrono>
#include <iostream>

int main() {
    // 创建一个持续时间为1秒的时间段
    auto duration = std::chrono::seconds(1);

    // 打印持续时间的秒数
    std::cout << "Duration in seconds: " << duration.count() << std::endl;

    return 0;
}

时钟精度(Clock precision):每种时钟可能具有不同的精度,可以使用 std::chrono::high_resolution_clock 来获取高精度时钟。

示例:

代码语言:javascript
复制
#include <chrono>
#include <iostream>

int main() {
    // 获取高精度时钟的当前时间点
    auto high_res_now = std::chrono::high_resolution_clock::now();

    // 打印高精度时钟的当前时间点
    std::cout << "High resolution clock time: " << high_res_now.time_since_epoch().count() << std::endl;

    return 0;
}

时间单位转换(Time unit conversion):可以使用 std::chrono::duration_cast 函数将持续时间从一个时间单位转换为另一个时间单位。

示例:

代码语言:javascript
复制
#include <chrono>
#include <iostream>

int main() {
    // 创建一个持续时间为1小时的时间段
    auto duration_hours = std::chrono::hours(1);

    // 将持续时间从小时转换为分钟
    auto duration_minutes = std::chrono::duration_cast<std::chrono::minutes>(duration_hours);

    // 打印转换后的持续时间
    std::cout << "Duration in minutes: " << duration_minutes.count() << std::endl;

    return 0;
}

日期和时间格式化(Date and time formatting):C++ 标准库不提供直接的日期和时间格式化功能,但可以与其他库(如 strftime)结合使用来实现格式化。

示例:

代码语言:javascript
复制
#include <chrono>
#include <iostream>
#include <ctime>

int main() {
    // 获取当前系统时钟时间点
    auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

    // 使用 strftime 将时间点格式化为字符串
    std::string time_str(30, '\0');
    std::strftime(&time_str[0], time_str.size(), "%Y-%m-%d %H:%M:%S", std::localtime(&now));

    // 打印格式化后的时间字符串
    std::cout << "Formatted time: " << time_str << std::endl;

    return 0;
}

时钟类型的特性(Clock traits):每种时钟类型都有一些特性,如是否稳定、是否单调递增等。可以使用 std::chrono::is_steadystd::chrono::is_clock 等类型特性查询特定时钟类型的属性。

示例:

代码语言:javascript
复制
#include <chrono>
#include <iostream>

int main() {
    // 查询系统时钟是否稳定
    std::cout << "System clock is steady: " << std::chrono::is_steady<std::chrono::system_clock>::value << std::endl;

    // 查询高精度时钟是否为时钟类型
    std::cout << "High resolution clock is a clock type: " << std::chrono::is_clock<std::chrono::high_resolution_clock>::value << std::endl;

    return 0;
}

时钟转换(Clock conversion):可以使用 std::chrono::time_point_cast 函数将时间点从一个时钟类型转换为另一个时钟类型。

示例:

代码语言:javascript
复制
#include <chrono>
#include <iostream>

int main() {
    // 获取系统时钟的当前时间点
    auto sys_now = std::chrono::system_clock::now();

    // 将系统时钟的时间点转换为高精度时钟的时间点
    auto high_res_now = std::chrono::time_point_cast<std::chrono::high_resolution_clock::duration>(sys_now);

    // 打印高精度时钟的时间点
    std::cout << "High resolution clock time: " << high_res_now.time_since_epoch().count() << std::endl;

    return 0;
}

自定义时钟(Custom clocks):可以根据需要自定义时钟类型,并提供时钟类型的必要特性和实现。

示例:

代码语言:javascript
复制
#include <chrono>
#include <iostream>

// 自定义时钟类型
struct MyClock {
    using duration = std::chrono::nanoseconds; // 持续时间类型
    using rep = duration::rep; // 表示时间的基本类型
    using period = duration::period; // 时间单位
    using time_point = std::chrono::time_point<MyClock>; // 时间点类型
    static constexpr bool is_steady = false; // 是否稳定
    static time_point now() noexcept { return time_point(std::chrono::duration_cast<duration>(std::chrono::steady_clock::now().time_since_epoch())); }
};

int main() {
    // 获取自定义时钟的当前时间点
    auto my_clock_now = MyClock::now();

    // 打印自定义时钟的时间点
    std::cout << "Custom clock time: " << my_clock_now.time_since_epoch().count() << std::endl;

    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2024-03-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linux兵工厂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档