博客和书籍资料 来自该地址 https://www.cnblogs.com/qicosmos/category/490693.html
自行编写相应代码进行学习
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 // TimeTest.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <chrono> 6 #include <typeinfo> 7 #include <iostream> 8 #include <thread> 9 #include <iomanip> 10 #include <ctime> 11 12 #include "Timer.h" 13 14 using namespace std; 15 16 void func1() 17 { 18 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 19 std::this_thread::sleep_for(std::chrono::seconds(1)); 20 //std::this_thread::sleep_for(std::chrono::minutes(1)); 21 22 std::chrono::milliseconds ms{ 3 }; // 3毫秒 23 std::chrono::microseconds us = 2 * ms; //6000微秒 24 std::chrono::duration<double, std::ratio<1, 30>> hz30{ 3.5 }; 25 26 std::cout << "3 ms duration has " << ms.count() << " ticks\n " 27 << "6000 us duration has " << us.count() << " ticks\n"; 28 } 29 30 void func2() 31 { 32 std::chrono::minutes t1(10); 33 std::chrono::seconds t2(60); 34 35 std::chrono::seconds t3 = t1 - t2; 36 std::cout << t3.count() << "second" << std::endl; 37 } 38 39 void TestChrono() 40 { 41 std::chrono::duration<double, std::ratio<9, 7>> d1(3); 42 std::chrono::duration<double, std::ratio<6, 5>> d2(1); 43 44 auto d3 = d1 - d2; 45 std::cout << typeid(d3).name() << std::endl; 46 47 std::cout << d3.count() << std::endl; 48 } 49 50 void func3() 51 { 52 using namespace std::chrono; 53 54 typedef duration<int, std::ratio<60 * 60 * 24>> days_type; 55 time_point<system_clock, days_type> today = time_point_cast<days_type>(system_clock::now()); 56 57 std::cout << today.time_since_epoch().count() << " days since epoch " << std::endl; 58 return; 59 } 60 61 void func4() 62 { 63 //用于写入数据库mysql 64 using namespace std::chrono; 65 system_clock::time_point now = system_clock::now(); 66 std::time_t last = system_clock::to_time_t(now - hours(24)); 67 std::time_t next = system_clock::to_time_t(now + hours(24)); 68 69 struct tm t; 70 localtime_s(&t, &last); 71 std::cout << "One day ago, the time was " 72 << std::put_time(&t, "%F %T") << "\n"; 73 74 localtime_s(&t, &next); 75 std::cout << "Next day , the time was " 76 << std::put_time(&t, "%F %T") << "\n"; 77 } 78 79 80 int main() 81 { 82 Timer timer; 83 system_clock::time_point now = system_clock::now(); 84 std::time_t nowtime = system_clock::to_time_t(now); 85 86 struct tm t; 87 localtime_s(&t, &nowtime); 88 std::cout << "before " 89 << std::put_time(&t, "%F %T") << "\n"; 90 91 92 while (1) { 93 if (timer.elapsed_minutes() > 3) { 94 std::cout <<" break"<<std::endl; 95 break; 96 } 97 std::this_thread::sleep_for(std::chrono::seconds(1)); 98 } 99 100 now = system_clock::now(); 101 nowtime = system_clock::to_time_t(now); 102 103 localtime_s(&t, &nowtime); 104 std::cout << "after " 105 << std::put_time(&t, "%F %T") << "\n"; 106 107 return 0; 108 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #pragma once 2 #include <chrono> 3 4 using namespace std; 5 using namespace std::chrono; 6 7 class Timer 8 { 9 public: 10 Timer() :m_begin(high_resolution_clock::now()) {}; 11 void reset() { m_begin = high_resolution_clock::now(); } 12 13 //默认输出毫秒 14 template<typename Duration = milliseconds> 15 int64_t elapsed()const { 16 return duration_cast<Duration>(high_resolution_clock::now() - m_begin).count(); 17 } 18 19 //微秒 20 int64_t elapsed_micro()const { 21 return elapsed<microseconds>(); 22 } 23 24 //纳秒 25 int64_t elapsed_nano()const 26 { 27 return elapsed<nanoseconds>(); 28 } 29 //秒 30 int64_t elapsed_seconds()const 31 { 32 return elapsed<seconds>(); 33 } 34 //分 35 int64_t elapsed_minutes()const 36 { 37 return elapsed<minutes>(); 38 } 39 //时 40 int64_t elapsed_hours()const 41 { 42 return elapsed<hours>(); 43 } 44 private: 45 time_point<high_resolution_clock> m_begin; 46 };