Convenient tool in C ++ 11-chrono Library (processing date and time)

Source: Internet
Author: User

Convenient tool in C ++ 11-chrono Library (processing date and time)

Chrono is the name of a header, but also of a sub-namespace: All the elements in this header (partition t for the common_type specializations) are not defined directly under the std namespace (like most of the standard library) but under the std: chrono namespace.

The elements in this header deal with time. This is done mainly by means of three concepts:

Durations
They measure time spans, like: one minute, two hours, or ten milliseconds.
In this library, they are represented with objects of the duration class template, that couples a count representation and a period precision (e.g ., ten milliseconds has ten as count representation and milliseconds as period precision ).

Time points
A reference to a specific point in time, like one's birthday, today's dawn, or when the next train passes.
In this library, objects of the time_point class template express this by using a duration relative to an epoch (which is a fixed point in time common to all time_point objects using the same clock ).

Clocks
A framework that relates a time point to real physical time.
The library provides at least three clocks that provide means to express the current time as a time_point: system_clock, steady_clock and high_resolution_clock.

Duration of record duration
Duration indicates a period of time, which is used to record the length of time. It can represent the interval of seconds, minutes, or hours.
Prototype:

template 
  
    >class duration;
  

Purpose:
A duration object expresses a time span by means of a count and a period.

Parameters:
Rep
An arithmetic type, or a class emulating an arithmetic type, to be used as the type for the internal count.
Period
A ratio type that represents the period in seconds.

#include 
  
   
    #include 
    
     #include 
     
      int main (){  typedef std::chrono::duration
      
        seconds_type; typedef std::chrono::duration
       
         milliseconds_type; typedef std::chrono::duration
        
         > hours_type; hours_type h_oneday (24); // 24h seconds_type s_oneday (60*60*24); // 86400s milliseconds_type ms_oneday (s_oneday); // 86400000ms seconds_type s_onehour (60*60); // 3600s//hours_type h_onehour (s_onehour); // NOT VALID (type truncates), use: hours_type h_onehour (std::chrono::duration_cast
         
          (s_onehour)); milliseconds_type ms_onehour (s_onehour); // 3600000ms (ok, no type truncation) std::cout << ms_onehour.count() << "ms in 1h" << std::endl; return 0;}
         
        
       
      
     
    
   
  

Time point of the time point
A time point is a time point used to obtain the duration and current time that has elapsed since the era of its clock.
Prototype:

template 
  
   
    
     
      
       
        
         
          
            class time_point;
          
         
        
       
      
     
    
   
  
// time_point constructors#include 
  
   
    
     
      
       
        
         
          
           
            #include 
            
             #include 
             
              int main (){ using namespace std::chrono; system_clock::time_point tp_epoch; // epoch value time_point 
              
               > tp_seconds (duration
               
                (1)); system_clock::time_point tp (tp_seconds); std::cout << "1 second since system_clock epoch = "; std::cout << tp.time_since_epoch().count(); std::cout << " system_clock periods." << std::endl; // display time_point: std::time_t tt = system_clock::to_time_t(tp); std::cout << "time_point tp is: " << ctime(&tt); return 0;}
               
              
             
            
           
          
         
        
       
      
     
    
   
  

Obtain the clock of the system clock.
Clocks indicates the current system clock.

System_clock
Clock classes provide access to the current time_point.
Specifically, system_clock is a system-wide realtime clock.

Steady_clock
Clock classes provide access to the current time_point.
Steady_clock is specifically designed to calculate time intervals.

High_resolution_clock
The members of clock classes provide access to the current time_point.
High_resolution_clock is the clock with the shortest tick period. It may be a synonym for system_clock or steady_clock.

#include 
  
   
    
     
      
       
        
         
          
           
            
             
              
               
                
                 #include 
                 
                  #include 
                  
                   #include 
                   
                    int main (){ using namespace std::chrono; high_resolution_clock::time_point t1 = high_resolution_clock::now(); std::cout << "printing out 1000 stars...\n"; for (int i=0; i<1000; ++i) std::cout << "*"; std::cout << std::endl; high_resolution_clock::time_point t2 = high_resolution_clock::now(); duration
                    
                      time_span = duration_cast
                     
                      >(t2 - t1); std::cout << "It took me " << time_span.count() << " seconds."; std::cout << std::endl; return 0;}
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.