使用chrono获取当天(obtain current day using chrono)

C ++ 11提供了返回当前时间的函数。 但是,我找不到返回当天的功能。 我使用boost来做到这一点。

boost::gregorian::date TODAY = boost::gregorian::day_clock::local_day();

有没有办法用chrono实现相同的结果?

编辑:我想要的是使用time_point来表示当天。 这意味着小时,分钟和秒均为零2013-07-31 00:00:00

C++11 provide a function to return current time. However, I cannot find there is a function to return current day. And I use boost to do so.

boost::gregorian::date TODAY = boost::gregorian::day_clock::local_day();

Is there any way to achieve the same result using chrono?

EDIT: What I want is to use time_point to represent current day. That means the hour,minute and second are zero 2013-07-31 00:00:00

最满意答案

Chronos不延长至日期问题; 这真的不是它的目的。 日期问题和“时间”问题之间的分界线是一天。 而Chrono没有定义日期类型。

但你可以把时间用小时数除以24.或者甚至更好,定义你自己的持续时间typedef并使用它:

typedef std::duration<std::uint32_t, std::ratio<3600 * 24>> day;

然后只使用该类型的time_point :

std::chrono::time_point<std::chrono::system_clock, day> day_getter; day_getter.time_since_epoch();

Chronos doesn't extend to date issues; that's really not it's purpose. The dividing line between date issues and "time" issues is the day. And Chrono doesn't define a day type.

But you could just divide the time in hours by 24. Or even better, define your own duration typedef and use that:

typedef std::duration<std::uint32_t, std::ratio<3600 * 24>> day;

Then just use a time_point with that type:

std::chrono::time_point<std::chrono::system_clock, day> day_getter; day_getter.time_since_epoch();

更多推荐