C++11中的便利工具--chrono库(处理日期和时间)

chrono is the name of a header, but also of a sub-namespace: All the elements in this header (except for the common_type specializations) are not defined directly under the std namespace (like most of the standard library) but under the std::chrono namespace.

The elements in this header deal with time. This is done mainly by means of three concepts:

Durations
They measure time spans, like: one minute, two hours, or ten milliseconds.
In this library, they are represented with objects of the duration class template, that couples a count representation and a period precision (e.g., ten milliseconds has ten as count representation and milliseconds as period precision).

Time points
A reference to a specific point in time, like one’s birthday, today’s dawn, or when the next train passes.
In this library, objects of the time_point class template express this by using a duration relative to an epoch (which is a fixed point in time common to all time_point objects using the same clock).

Clocks
A framework that relates a time point to real physical time.
The library provides at least three clocks that provide means to express the current time as a time_point: system_clock, steady_clock and high_resolution_clock.

记录时长的duration
duration表示一段时间间隔,用来记录时间长度,可以表示几秒、几分钟或者几个小时的时间间隔。
原型:

template <class Rep, class Period = ratio<1> >
class duration;
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
可以使用 C++11 的 `<chrono>` 头文件和 `<iomanip>` 头文件的 `std::put_time` 函数来实现。 以下是一个获取两个日期间所有日期的示例代码: ```c++ #include <iostream> #include <chrono> #include <iomanip> #include <vector> using namespace std; // 获取两个日期之间的所有日期 vector<string> getDatesBetween(const string& start, const string& end) { // 将起始日期和结束日期转换为时间点 auto startTime = chrono::system_clock::from_time_t(chrono::system_clock::to_time_t(chrono::system_clock::now())); auto endTime = chrono::system_clock::from_time_t(chrono::system_clock::to_time_t(chrono::system_clock::now())); stringstream ss(start); ss >> get_time(&tm(), "%Y-%m-%d"); startTime = chrono::system_clock::from_time_t(mktime(&tm())); ss.clear(); ss.str(end); ss >> get_time(&tm(), "%Y-%m-%d"); endTime = chrono::system_clock::from_time_t(mktime(&tm())); // 计算日期差 auto days = chrono::duration_cast<chrono::duration<int, ratio<86400>>>(endTime - startTime).count(); // 构造日期列表 vector<string> dates; for (int i = 0; i <= days; i++) { auto tp = startTime + chrono::hours(i * 24); // 逐天增加 auto t = chrono::system_clock::to_time_t(tp); // 转换为 time_t auto tm = *localtime(&t); // 转换为 struct tm stringstream ss; ss << put_time(&tm, "%Y-%m-%d"); // 格式化日期 dates.push_back(ss.str()); } return dates; } int main() { vector<string> dates = getDatesBetween("2022-01-01", "2022-01-07"); for (auto date : dates) { cout << date << endl; } return 0; } ``` 该示例代码的 `getDatesBetween` 函数接受两个字符串类型的日期参数,返回一个字符串类型的日期列表,其包含了起始日期和结束日期之间的所有日期。 在函数,首先将起始日期和结束日期转换为时间点,然后计算日期差,并逐天增加时间点,最后将时间点转换回日期并格式化为字符串存入列表
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一苇渡江694

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

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

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

打赏作者

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

抵扣说明:

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

余额充值