c++11 时间相关操作练习
博客和书籍资料 来自该地址 https://www.cnblogs.com/qicosmos/category/490693.html
自行编写相应代码进行学习
// TimeTest.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <chrono>
#include <typeinfo>
#include <iostream>
#include <thread>
#include <iomanip>
#include <ctime> #include "Timer.h" using namespace std; void func1()
{
std::this_thread::sleep_for(std::chrono::milliseconds());
std::this_thread::sleep_for(std::chrono::seconds());
//std::this_thread::sleep_for(std::chrono::minutes(1)); std::chrono::milliseconds ms{ }; // 3毫秒
std::chrono::microseconds us = * ms; //6000微秒
std::chrono::duration<double, std::ratio<, >> hz30{ 3.5 }; std::cout << "3 ms duration has " << ms.count() << " ticks\n "
<< "6000 us duration has " << us.count() << " ticks\n";
} void func2()
{
std::chrono::minutes t1();
std::chrono::seconds t2(); std::chrono::seconds t3 = t1 - t2;
std::cout << t3.count() << "second" << std::endl;
} void TestChrono()
{
std::chrono::duration<double, std::ratio<, >> d1();
std::chrono::duration<double, std::ratio<, >> d2(); auto d3 = d1 - d2;
std::cout << typeid(d3).name() << std::endl; std::cout << d3.count() << std::endl;
} void func3()
{
using namespace std::chrono; typedef duration<int, std::ratio< * * >> days_type;
time_point<system_clock, days_type> today = time_point_cast<days_type>(system_clock::now()); std::cout << today.time_since_epoch().count() << " days since epoch " << std::endl;
return;
} void func4()
{
//用于写入数据库mysql
using namespace std::chrono;
system_clock::time_point now = system_clock::now();
std::time_t last = system_clock::to_time_t(now - hours());
std::time_t next = system_clock::to_time_t(now + hours()); struct tm t;
localtime_s(&t, &last);
std::cout << "One day ago, the time was "
<< std::put_time(&t, "%F %T") << "\n"; localtime_s(&t, &next);
std::cout << "Next day , the time was "
<< std::put_time(&t, "%F %T") << "\n";
} int main()
{
Timer timer;
system_clock::time_point now = system_clock::now();
std::time_t nowtime = system_clock::to_time_t(now); struct tm t;
localtime_s(&t, &nowtime);
std::cout << "before "
<< std::put_time(&t, "%F %T") << "\n"; while () {
if (timer.elapsed_minutes() > ) {
std::cout <<" break"<<std::endl;
break;
}
std::this_thread::sleep_for(std::chrono::seconds());
} now = system_clock::now();
nowtime = system_clock::to_time_t(now); localtime_s(&t, &nowtime);
std::cout << "after "
<< std::put_time(&t, "%F %T") << "\n"; return ;
}
#pragma once
#include <chrono> using namespace std;
using namespace std::chrono; class Timer
{
public:
Timer() :m_begin(high_resolution_clock::now()) {};
void reset() { m_begin = high_resolution_clock::now(); } //默认输出毫秒
template<typename Duration = milliseconds>
int64_t elapsed()const {
return duration_cast<Duration>(high_resolution_clock::now() - m_begin).count();
} //微秒
int64_t elapsed_micro()const {
return elapsed<microseconds>();
} //纳秒
int64_t elapsed_nano()const
{
return elapsed<nanoseconds>();
}
//秒
int64_t elapsed_seconds()const
{
return elapsed<seconds>();
}
//分
int64_t elapsed_minutes()const
{
return elapsed<minutes>();
}
//时
int64_t elapsed_hours()const
{
return elapsed<hours>();
}
private:
time_point<high_resolution_clock> m_begin;
};
c++11 时间相关操作练习的更多相关文章
- Linux之时间相关操作20170607
		一.Linux常用时间相关函数 -asctime,ctime,getttimeofday,gmtime,localtime,mktime,settimeofday,time asctime ... 
- C++11时间操作
		C++11提供了chrono库,这个库可以处理和时间相关的一些事情.这个库里面主要有3个类:时间间隔Duration.时钟Clocks和时间点Time point. Duration template ... 
- centos7下安装docker(11容器操作总结)
		这段时间主要是学习了对容器的操作,包括:容器的状态:start,stop,restart,rename,pause,unpause,rm,attach,exec,kill,logs:还学习了对容器的资 ... 
- MySQL时间相关操作
		本文主要介绍MySQL使用过程中有关时间处理的相关操作: MySQL时间更新(加上或减去一段时间) MySQL毫秒数和日期之间的转换 一.MySQL时间更新(加上或减去一段时间) 1.1 MySQL时 ... 
- Flask(11)- 操作 Cookie
		前言 Cookie 详解:https://www.cnblogs.com/poloyy/p/12513247.html 这一节来瞧一瞧如何用 Flask 操作 Cookie 接下来就是 实战栗子!!! ... 
- Js获取当前日期时间及时间相关操作
		Js获取当前日期时间及时间格式 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ... 
- 11)django-ORM(操作增删改查)
		ORM从增删改查等方面说明 一:创建数据 #创建数据两种方式1,推荐方式1 UserInfo.objects.create(username=") #方式1变种 user_dict={&qu ... 
- PHP 时间相关操作
		使用函式 date() 实现 <?php echo $showtime=date("Y-m-d H:i:s");?> 显示的格式: 年-月-日 小时:分钟:秒 获得当天 ... 
- odoo开发笔记-日期时间相关操作
		日期格式化字符串:DATE_FORMAT = "%Y-%m-%d" 日期时间格式字符串:DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" ... 
随机推荐
- spring boot springmvc视图
			pring boot 在springmvc的视图解析器方面就默认集成了ContentNegotiatingViewResolver和BeanNameViewResolver,在视图引擎上就已经集成自动 ... 
- CSS同时选择器
			[CSS同时选择器] 同一个div拥有多个class时,我们可以作多个class作为组合来选择对象.方法就是将多个.className直接连接在一起(中间不能有空格). <p class=&qu ... 
- 去掉Android新建项目的顶部标题
			[ 去掉Android新建项目的顶部标题] 使用NoActionBar的Theme即可. 参考:http://blog.csdn.net/u012246458/article/details/5299 ... 
- AdminLTE 动画时间
			app.js AdminLTE.css 
- SQL%ROWCOUNT作用
			SQL%ROWCOUNT是一个游标属性,而SQL中的DML操作实际上是一种隐式的游标操作,在做insert,update,delete,merge以及select into操作时,Oracle会打开一 ... 
- 第十章 优先级队列 (b4)完全二叉堆:批量建堆
- 第四章 栈与队列(a)栈接口与实现
- 第二章 向量(d3)有序向量:Fibonacci查找
- 22. Generate Parentheses (backTracking)
			Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ... 
- 【linux】常见问题&常用命令笔记
			1.重启以及关机命令: Linux centos重启命令: (1)reboot 普通重启 (2)shutdown -r now 立刻重启(root用户使用) (3)shutdown -r 10 过 ... 
