• c++11 时间相关操作练习


    博客和书籍资料 来自该地址 https://www.cnblogs.com/qicosmos/category/490693.html

    自行编写相应代码进行学习

      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
     "
     27         << "6000 us duration has " << us.count() << " ticks
    ";
     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") << "
    ";
     73 
     74     localtime_s(&t, &next);
     75     std::cout << "Next day , the time was "
     76         << std::put_time(&t, "%F %T") << "
    ";
     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") << "
    ";
     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") << "
    ";
    106     
    107     return 0;
    108 }
    View Code
     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 };
    View Code
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    SOUI更新到2.0
    第三十一篇:SOUI布局之相对于特定兄弟窗口
    SOUI与WTL
    在SOUI中非半透明窗口如何实现圆角窗口?
    拥抱ARM妹纸第二季 之 第一次 点亮太阳
    拥抱ARM妹子第二季 之 序:我和春天有个约会
    解决自定义控件窗体缩放时闪烁
    The 50 Most Essential Pieces of Classical Music
    嵌入式开发目录
    C中浮点数转字符串
  • 原文地址:https://www.cnblogs.com/itdef/p/7927351.html
Copyright © 2020-2023  润新知