c语言中point用法,time_point 的基本用法举例

time_point,是C++11引入的表示特定时间点的工具,它工作时需要clock的帮助,可为system_clock, monotonic_clock, 或high_resolution_clock。

time_point在头文件中定义,并且使用时要引用 std::chrono命名空间。

其定义形式为

template<

class Clock,

class Duration = typename Clock::duration

> class time_point;

下面是一个使用time_point的小例子,在time_point与C语言中的time相互转换。

#include

#include

#include

#include

using namespace std;

using namespace std::chrono;

int main()

{

/* 由time_point得到时间点。*/

time_point now_time = system_clock::now(); //当前时间

time_t now_c = system_clock::to_time_t(now_time);

tm* now_tm = localtime(&now_c);

cout << "now : " << put_time(now_tm, "%F %T") << endl;

// %F - equivalent to "%Y-%m-%d" (the ISO 8601 date format); tm_mon, tm_mday, tm_year

// %T - equivalent to "%H:%M:%S" (the ISO 8601 time format); tm_hour, tm_min, tm_sec

/* 如何得到特定时间点的 time_point. */

tm then_tm;

then_tm.tm_year = 2012-1900;

then_tm.tm_mon = 7; //8月

then_tm.tm_mday = 15;

then_tm.tm_hour = 19;

then_tm.tm_min = 19;

then_tm.tm_sec = 19;

time_t timetThen = mktime(&then_tm); //得到时间的整数值

time_point then_tp = system_clock::from_time_t(timetThen);

//后面就可以使用then_tp了

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值