C++11中的便利工具--chrono库(处理日期和时间)

页面导航:首页 > 软件编程 > C 语言 > C++11中的便利工具--chrono库(处理日期和时间)

C++11中的便利工具--chrono库(处理日期和时间)

来源: 作者: 时间:2016-01-15 14:31 【

chrono is the name of a header, but also of a sub-namespace: All the elements in this header (except for the common_type specializations) are not defined directly under

chrono is the name of a header, but also of a sub-namespace: All the elements in this header (except 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
duration表示一段时间间隔,用来记录时间长度,可以表示几秒、几分钟或者几个小时的时间间隔。
原型:

template  >
class duration;

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

参数:
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
time point表示一个时间点,用来获取从它的clock的纪元开始所经过的duration和当前时间。
原型:

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;
}

获取时钟的clock
clocks表示当前的系统时钟。

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;
}
 
Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<