博客和书籍资料 来自该地址 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;
};