在C ++中比较日期的方式是什么 [英] Whats the way to compare dates in c++
问题描述
在c ++中比较日期的最好方法是什么?将日期转换成具有日期,月份和年份变量的结构
,即如何检查自事件以来已经过去了30天等等。我个人发现,当比较同一年中的2个日期时,我会使用一个数组来将两个日期都设为天-日期,该数组保存一年中所有月份的天数,然后将日期转换为天并进行我需要的计算。有没有更简单的方法可以比较日期?
Whats the best way to compare dates in c++ which are made into a structure that has a day,month and year variable i.e how to check that 30 days have passed since a event and so on. I personally found that when comparing 2 dates in the same year I make both dates into "days-dates" using a array that holds the day count of all the months of the year and then convert the dates into days and do the calculations I need. Is there a easier way to compare the dates ?
推荐答案
这是一个很好的用于处理日期的C ++ 11/14库 1 。它允许您具有 {年,月,日}
结构(称为 date :: year_month_day
)和 {day-of-days}
结构(称为 sys_days
)。它还允许在这两种结构之间轻松有效地进行转换。当然也有比较运算符。
Here is a very good C++11/14 library for handling dates1. It allows you to have {year, month, day}
structures (called date::year_month_day
), and {count-of-days}
structures (called sys_days
). It also allows easy and efficient conversions between these two structures. And naturally there are comparison operators.
整个库本质上是< chrono>
到日历类型的扩展。
The entire library is essentially an extension of <chrono>
into calendar types.
视频介绍在这里找到:
https://www.youtube.com/watch?v=tzyGjOm8AKo
此处详细说明了在 {年,月,日}
结构和 {count- 结构:
Here is a detailed explanation of the underlying algorithms to convert between the {year, month, day}
structure and the {count-of-days}
structure:
http://howardhinnant.github.io/date_algorithms.html
以下是两个字段的日期创建和比较示例( year_month_day
)和序列号( sys_day s
)数据结构:
Here are some date creation and comparison examples for both field (year_month_day
) and serial (sys_days
) data structures:
#include "date.h"
int
main()
{
using namespace date::literals;
// create and compare {year, month, day} structures
constexpr auto ymd1 = 2017_y/jan/21;
constexpr auto ymd2 = ymd1 + date::months{15};
static_assert(ymd2 > ymd1, "ymd2 is 15 months after ymd1");
static_assert(ymd2 == 2018_y/apr/21, "ymd2 is 2018-04-21");
// create and compare {count_of_days} structures
constexpr date::sys_days sd2 = ymd2;
static_assert(sd2 == ymd2, "sd2 is the same day as ymd2");
static_assert(sd2.time_since_epoch().count() == 17642, "sd2 is day 17642");
constexpr date::sys_days sd1 = sd2 - date::days{465};
static_assert(sd1 < sd2, "sd1 is 465 days before sd2");
static_assert(sd1.time_since_epoch().count() == 17177, "sd1 is day 17177");
static_assert(sd1 == 2017_y/jan/11, "sd1 is 2017-01-11");
}
constexpr
/ static_assert
需要完全符合要求的C ++ 14编译器。对于C ++ 11,删除 constexpr
并将 static_assert
更改为 assert
(并消除 static_assert
消息)。
The constexpr
/ static_assert
requires a fully conforming C++14 compiler. For C++11, remove the constexpr
and change static_assert
to assert
(and eliminate the static_assert
message).
date :: sys_days
是 chrono :: time_point
:
time_point<system_clock, duration<int, ratio<86400>>
上面的示例代码只需要 date.h
,并且没有其他C ++源文件(无需安装)。在同一github位置上也有一个时区库,但这确实需要一些
The above example code requires only "date.h"
, and no other C++ source files (no installation). There is also available a timezone library at this same github location, but that does require some installation.
1 我是该库的主要作者。
1 I am the principal author of this library.
这篇关于在C ++中比较日期的方式是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!