libstdc++
chrono
Go to the documentation of this file.
1 // <chrono> -*- C++ -*-
2 
3 // Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file include/chrono
26  * This is a Standard C++ Library header.
27  */
28 
29 #ifndef _GLIBCXX_CHRONO
30 #define _GLIBCXX_CHRONO 1
31 
32 #pragma GCC system_header
33 
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
37 
38 #include <ratio>
39 #include <type_traits>
40 #include <limits>
41 #include <ctime>
42 
43 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
44 
45 namespace std _GLIBCXX_VISIBILITY(default)
46 {
47  /**
48  * @defgroup chrono Time
49  * @ingroup utilities
50  *
51  * Classes and functions for time.
52  * @{
53  */
54 
55  /** @namespace std::chrono
56  * @brief ISO C++ 0x entities sub namespace for time and date.
57  */
58  namespace chrono
59  {
60  _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 
62  template<typename _Rep, typename _Period = ratio<1>>
63  struct duration;
64 
65  template<typename _Clock, typename _Dur = typename _Clock::duration>
66  struct time_point;
67 
68  _GLIBCXX_END_NAMESPACE_VERSION
69  }
70 
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
72  // 20.8.2.3 specialization of common_type (for duration)
73  template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
74  struct common_type<chrono::duration<_Rep1, _Period1>,
75  chrono::duration<_Rep2, _Period2>>
76  {
77  private:
78  typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num;
79  typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den;
80  typedef typename common_type<_Rep1, _Rep2>::type __cr;
81  typedef ratio<__gcd_num::value,
82  (_Period1::den / __gcd_den::value) * _Period2::den> __r;
83 
84  public:
85  typedef chrono::duration<__cr, __r> type;
86  };
87 
88  // 20.8.2.3 specialization of common_type (for time_point)
89  template<typename _Clock, typename _Dur1, typename _Dur2>
90  struct common_type<chrono::time_point<_Clock, _Dur1>,
91  chrono::time_point<_Clock, _Dur2>>
92  {
93  private:
94  typedef typename common_type<_Dur1, _Dur2>::type __ct;
95 
96  public:
97  typedef chrono::time_point<_Clock, __ct> type;
98  };
99 _GLIBCXX_END_NAMESPACE_VERSION
100 
101  namespace chrono
102  {
103  _GLIBCXX_BEGIN_NAMESPACE_VERSION
104 
105  // Primary template for duration_cast impl.
106  template<typename _ToDur, typename _CF, typename _CR,
107  bool _NumIsOne = false, bool _DenIsOne = false>
108  struct __duration_cast_impl
109  {
110  template<typename _Rep, typename _Period>
111  static constexpr _ToDur
112  __cast(const duration<_Rep, _Period>& __d)
113  {
114  typedef typename _ToDur::rep __to_rep;
115  return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
116  * static_cast<_CR>(_CF::num)
117  / static_cast<_CR>(_CF::den)));
118  }
119  };
120 
121  template<typename _ToDur, typename _CF, typename _CR>
122  struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
123  {
124  template<typename _Rep, typename _Period>
125  static constexpr _ToDur
126  __cast(const duration<_Rep, _Period>& __d)
127  {
128  typedef typename _ToDur::rep __to_rep;
129  return _ToDur(static_cast<__to_rep>(__d.count()));
130  }
131  };
132 
133  template<typename _ToDur, typename _CF, typename _CR>
134  struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
135  {
136  template<typename _Rep, typename _Period>
137  static constexpr _ToDur
138  __cast(const duration<_Rep, _Period>& __d)
139  {
140  typedef typename _ToDur::rep __to_rep;
141  return _ToDur(static_cast<__to_rep>(
142  static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
143  }
144  };
145 
146  template<typename _ToDur, typename _CF, typename _CR>
147  struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
148  {
149  template<typename _Rep, typename _Period>
150  static constexpr _ToDur
151  __cast(const duration<_Rep, _Period>& __d)
152  {
153  typedef typename _ToDur::rep __to_rep;
154  return _ToDur(static_cast<__to_rep>(
155  static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
156  }
157  };
158 
159  template<typename _Tp>
160  struct __is_duration
162  { };
163 
164  template<typename _Rep, typename _Period>
165  struct __is_duration<duration<_Rep, _Period>>
167  { };
168 
169  /// duration_cast
170  template<typename _ToDur, typename _Rep, typename _Period>
171  constexpr typename enable_if<__is_duration<_ToDur>::value,
172  _ToDur>::type
174  {
175  typedef typename _ToDur::period __to_period;
176  typedef typename _ToDur::rep __to_rep;
177  typedef ratio_divide<_Period, __to_period> __r_div;
178  typedef typename __r_div::type __cf;
179  typedef typename common_type<__to_rep, _Rep, intmax_t>::type
180  __cr;
181  typedef __duration_cast_impl<_ToDur, __cf, __cr,
182  __cf::num == 1, __cf::den == 1> __dc;
183  return __dc::__cast(__d);
184  }
185 
186  /// treat_as_floating_point
187  template<typename _Rep>
189  : is_floating_point<_Rep>
190  { };
191 
192  /// duration_values
193  template<typename _Rep>
195  {
196  static constexpr _Rep
197  zero()
198  { return _Rep(0); }
199 
200  static constexpr _Rep
201  max()
202  { return numeric_limits<_Rep>::max(); }
203 
204  static constexpr _Rep
205  min()
206  { return numeric_limits<_Rep>::lowest(); }
207  };
208 
209  template<typename T>
210  struct __is_ratio
212  { };
213 
214  template<intmax_t _Num, intmax_t _Den>
215  struct __is_ratio<ratio<_Num, _Den>>
217  { };
218 
219  /// duration
220  template<typename _Rep, typename _Period>
221  struct duration
222  {
223  typedef _Rep rep;
224  typedef _Period period;
225 
226  static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
227  static_assert(__is_ratio<_Period>::value,
228  "period must be a specialization of ratio");
229  static_assert(_Period::num > 0, "period must be positive");
230 
231  // 20.8.3.1 construction / copy / destroy
232  constexpr duration() : __r() { }
233 
234  constexpr duration(const duration&) = default;
235 
236  template<typename _Rep2, typename = typename
240  constexpr explicit duration(const _Rep2& __rep)
241  : __r(static_cast<rep>(__rep)) { }
242 
243  template<typename _Rep2, typename _Period2, typename = typename
247  constexpr duration(const duration<_Rep2, _Period2>& __d)
248  : __r(duration_cast<duration>(__d).count()) { }
249 
250  ~duration() = default;
251  duration& operator=(const duration&) = default;
252 
253  // 20.8.3.2 observer
254  constexpr rep
255  count() const
256  { return __r; }
257 
258  // 20.8.3.3 arithmetic
259  constexpr duration
260  operator+() const
261  { return *this; }
262 
263  constexpr duration
264  operator-() const
265  { return duration(-__r); }
266 
267  duration&
268  operator++()
269  {
270  ++__r;
271  return *this;
272  }
273 
274  duration
275  operator++(int)
276  { return duration(__r++); }
277 
278  duration&
279  operator--()
280  {
281  --__r;
282  return *this;
283  }
284 
285  duration
286  operator--(int)
287  { return duration(__r--); }
288 
289  duration&
290  operator+=(const duration& __d)
291  {
292  __r += __d.count();
293  return *this;
294  }
295 
296  duration&
297  operator-=(const duration& __d)
298  {
299  __r -= __d.count();
300  return *this;
301  }
302 
303  duration&
304  operator*=(const rep& __rhs)
305  {
306  __r *= __rhs;
307  return *this;
308  }
309 
310  duration&
311  operator/=(const rep& __rhs)
312  {
313  __r /= __rhs;
314  return *this;
315  }
316 
317  // DR 934.
318  template<typename _Rep2 = rep>
320  duration&>::type
321  operator%=(const rep& __rhs)
322  {
323  __r %= __rhs;
324  return *this;
325  }
326 
327  template<typename _Rep2 = rep>
329  duration&>::type
330  operator%=(const duration& __d)
331  {
332  __r %= __d.count();
333  return *this;
334  }
335 
336  // 20.8.3.4 special values
337  static constexpr duration
338  zero()
339  { return duration(duration_values<rep>::zero()); }
340 
341  static constexpr duration
342  min()
343  { return duration(duration_values<rep>::min()); }
344 
345  static constexpr duration
346  max()
347  { return duration(duration_values<rep>::max()); }
348 
349  private:
350  rep __r;
351  };
352 
353  template<typename _Rep1, typename _Period1,
354  typename _Rep2, typename _Period2>
355  constexpr typename common_type<duration<_Rep1, _Period1>,
357  operator+(const duration<_Rep1, _Period1>& __lhs,
358  const duration<_Rep2, _Period2>& __rhs)
359  {
360  typedef duration<_Rep1, _Period1> __dur1;
361  typedef duration<_Rep2, _Period2> __dur2;
362  typedef typename common_type<__dur1,__dur2>::type __cd;
363  return __cd(__cd(__lhs).count() + __cd(__rhs).count());
364  }
365 
366  template<typename _Rep1, typename _Period1,
367  typename _Rep2, typename _Period2>
368  constexpr typename common_type<duration<_Rep1, _Period1>,
369  duration<_Rep2, _Period2>>::type
370  operator-(const duration<_Rep1, _Period1>& __lhs,
371  const duration<_Rep2, _Period2>& __rhs)
372  {
373  typedef duration<_Rep1, _Period1> __dur1;
374  typedef duration<_Rep2, _Period2> __dur2;
375  typedef typename common_type<__dur1,__dur2>::type __cd;
376  return __cd(__cd(__lhs).count() - __cd(__rhs).count());
377  }
378 
379  template<typename _Rep1, typename _Rep2, bool =
380  is_convertible<_Rep2,
381  typename common_type<_Rep1, _Rep2>::type>::value>
382  struct __common_rep_type { };
383 
384  template<typename _Rep1, typename _Rep2>
385  struct __common_rep_type<_Rep1, _Rep2, true>
386  { typedef typename common_type<_Rep1, _Rep2>::type type; };
387 
388  template<typename _Rep1, typename _Period, typename _Rep2>
389  constexpr
390  duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
391  operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
392  {
393  typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
394  __cd;
395  return __cd(__cd(__d).count() * __s);
396  }
397 
398  template<typename _Rep1, typename _Rep2, typename _Period>
399  constexpr
400  duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
401  operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
402  { return __d * __s; }
403 
404  template<typename _Rep1, typename _Period, typename _Rep2>
405  constexpr duration<typename __common_rep_type<_Rep1, typename
406  enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
407  operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
408  {
409  typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
410  __cd;
411  return __cd(__cd(__d).count() / __s);
412  }
413 
414  template<typename _Rep1, typename _Period1,
415  typename _Rep2, typename _Period2>
416  constexpr typename common_type<_Rep1, _Rep2>::type
417  operator/(const duration<_Rep1, _Period1>& __lhs,
418  const duration<_Rep2, _Period2>& __rhs)
419  {
420  typedef duration<_Rep1, _Period1> __dur1;
421  typedef duration<_Rep2, _Period2> __dur2;
422  typedef typename common_type<__dur1,__dur2>::type __cd;
423  return __cd(__lhs).count() / __cd(__rhs).count();
424  }
425 
426  // DR 934.
427  template<typename _Rep1, typename _Period, typename _Rep2>
428  constexpr duration<typename __common_rep_type<_Rep1, typename
429  enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
430  operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
431  {
432  typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
433  __cd;
434  return __cd(__cd(__d).count() % __s);
435  }
436 
437  template<typename _Rep1, typename _Period1,
438  typename _Rep2, typename _Period2>
439  constexpr typename common_type<duration<_Rep1, _Period1>,
440  duration<_Rep2, _Period2>>::type
441  operator%(const duration<_Rep1, _Period1>& __lhs,
442  const duration<_Rep2, _Period2>& __rhs)
443  {
444  typedef duration<_Rep1, _Period1> __dur1;
445  typedef duration<_Rep2, _Period2> __dur2;
446  typedef typename common_type<__dur1,__dur2>::type __cd;
447  return __cd(__cd(__lhs).count() % __cd(__rhs).count());
448  }
449 
450  // comparisons
451  template<typename _Rep1, typename _Period1,
452  typename _Rep2, typename _Period2>
453  constexpr bool
454  operator==(const duration<_Rep1, _Period1>& __lhs,
455  const duration<_Rep2, _Period2>& __rhs)
456  {
457  typedef duration<_Rep1, _Period1> __dur1;
458  typedef duration<_Rep2, _Period2> __dur2;
459  typedef typename common_type<__dur1,__dur2>::type __ct;
460  return __ct(__lhs).count() == __ct(__rhs).count();
461  }
462 
463  template<typename _Rep1, typename _Period1,
464  typename _Rep2, typename _Period2>
465  constexpr bool
466  operator<(const duration<_Rep1, _Period1>& __lhs,
467  const duration<_Rep2, _Period2>& __rhs)
468  {
469  typedef duration<_Rep1, _Period1> __dur1;
470  typedef duration<_Rep2, _Period2> __dur2;
471  typedef typename common_type<__dur1,__dur2>::type __ct;
472  return __ct(__lhs).count() < __ct(__rhs).count();
473  }
474 
475  template<typename _Rep1, typename _Period1,
476  typename _Rep2, typename _Period2>
477  constexpr bool
478  operator!=(const duration<_Rep1, _Period1>& __lhs,
479  const duration<_Rep2, _Period2>& __rhs)
480  { return !(__lhs == __rhs); }
481 
482  template<typename _Rep1, typename _Period1,
483  typename _Rep2, typename _Period2>
484  constexpr bool
485  operator<=(const duration<_Rep1, _Period1>& __lhs,
486  const duration<_Rep2, _Period2>& __rhs)
487  { return !(__rhs < __lhs); }
488 
489  template<typename _Rep1, typename _Period1,
490  typename _Rep2, typename _Period2>
491  constexpr bool
492  operator>(const duration<_Rep1, _Period1>& __lhs,
493  const duration<_Rep2, _Period2>& __rhs)
494  { return __rhs < __lhs; }
495 
496  template<typename _Rep1, typename _Period1,
497  typename _Rep2, typename _Period2>
498  constexpr bool
499  operator>=(const duration<_Rep1, _Period1>& __lhs,
500  const duration<_Rep2, _Period2>& __rhs)
501  { return !(__lhs < __rhs); }
502 
503  /// nanoseconds
505 
506  /// microseconds
508 
509  /// milliseconds
511 
512  /// seconds
514 
515  /// minutes
517 
518  /// hours
520 
521  /// time_point
522  template<typename _Clock, typename _Dur>
523  struct time_point
524  {
525  typedef _Clock clock;
526  typedef _Dur duration;
527  typedef typename duration::rep rep;
528  typedef typename duration::period period;
529 
530  constexpr time_point() : __d(duration::zero())
531  { }
532 
533  constexpr explicit time_point(const duration& __dur)
534  : __d(__dur)
535  { }
536 
537  // conversions
538  template<typename _Dur2>
539  constexpr time_point(const time_point<clock, _Dur2>& __t)
540  : __d(__t.time_since_epoch())
541  { }
542 
543  // observer
544  constexpr duration
545  time_since_epoch() const
546  { return __d; }
547 
548  // arithmetic
549  time_point&
550  operator+=(const duration& __dur)
551  {
552  __d += __dur;
553  return *this;
554  }
555 
556  time_point&
557  operator-=(const duration& __dur)
558  {
559  __d -= __dur;
560  return *this;
561  }
562 
563  // special values
564  static constexpr time_point
565  min()
566  { return time_point(duration::min()); }
567 
568  static constexpr time_point
569  max()
570  { return time_point(duration::max()); }
571 
572  private:
573  duration __d;
574  };
575 
576  /// time_point_cast
577  template<typename _ToDur, typename _Clock, typename _Dur>
578  constexpr typename enable_if<__is_duration<_ToDur>::value,
581  {
582  typedef time_point<_Clock, _ToDur> __time_point;
583  return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
584  }
585 
586  template<typename _Clock, typename _Dur1,
587  typename _Rep2, typename _Period2>
588  constexpr time_point<_Clock,
589  typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
590  operator+(const time_point<_Clock, _Dur1>& __lhs,
591  const duration<_Rep2, _Period2>& __rhs)
592  {
593  typedef duration<_Rep2, _Period2> __dur2;
594  typedef typename common_type<_Dur1,__dur2>::type __ct;
595  typedef time_point<_Clock, __ct> __time_point;
596  return __time_point(__lhs.time_since_epoch() + __rhs);
597  }
598 
599  template<typename _Rep1, typename _Period1,
600  typename _Clock, typename _Dur2>
601  constexpr time_point<_Clock,
602  typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
603  operator+(const duration<_Rep1, _Period1>& __lhs,
604  const time_point<_Clock, _Dur2>& __rhs)
605  {
606  typedef duration<_Rep1, _Period1> __dur1;
607  typedef typename common_type<__dur1,_Dur2>::type __ct;
608  typedef time_point<_Clock, __ct> __time_point;
609  return __time_point(__rhs.time_since_epoch() + __lhs);
610  }
611 
612  template<typename _Clock, typename _Dur1,
613  typename _Rep2, typename _Period2>
614  constexpr time_point<_Clock,
615  typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
616  operator-(const time_point<_Clock, _Dur1>& __lhs,
617  const duration<_Rep2, _Period2>& __rhs)
618  {
619  typedef duration<_Rep2, _Period2> __dur2;
620  typedef typename common_type<_Dur1,__dur2>::type __ct;
621  typedef time_point<_Clock, __ct> __time_point;
622  return __time_point(__lhs.time_since_epoch() -__rhs);
623  }
624 
625  template<typename _Clock, typename _Dur1, typename _Dur2>
626  constexpr typename common_type<_Dur1, _Dur2>::type
627  operator-(const time_point<_Clock, _Dur1>& __lhs,
628  const time_point<_Clock, _Dur2>& __rhs)
629  { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
630 
631  template<typename _Clock, typename _Dur1, typename _Dur2>
632  constexpr bool
633  operator==(const time_point<_Clock, _Dur1>& __lhs,
634  const time_point<_Clock, _Dur2>& __rhs)
635  { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
636 
637  template<typename _Clock, typename _Dur1, typename _Dur2>
638  constexpr bool
639  operator!=(const time_point<_Clock, _Dur1>& __lhs,
640  const time_point<_Clock, _Dur2>& __rhs)
641  { return !(__lhs == __rhs); }
642 
643  template<typename _Clock, typename _Dur1, typename _Dur2>
644  constexpr bool
645  operator<(const time_point<_Clock, _Dur1>& __lhs,
646  const time_point<_Clock, _Dur2>& __rhs)
647  { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
648 
649  template<typename _Clock, typename _Dur1, typename _Dur2>
650  constexpr bool
651  operator<=(const time_point<_Clock, _Dur1>& __lhs,
652  const time_point<_Clock, _Dur2>& __rhs)
653  { return !(__rhs < __lhs); }
654 
655  template<typename _Clock, typename _Dur1, typename _Dur2>
656  constexpr bool
657  operator>(const time_point<_Clock, _Dur1>& __lhs,
658  const time_point<_Clock, _Dur2>& __rhs)
659  { return __rhs < __lhs; }
660 
661  template<typename _Clock, typename _Dur1, typename _Dur2>
662  constexpr bool
663  operator>=(const time_point<_Clock, _Dur1>& __lhs,
664  const time_point<_Clock, _Dur2>& __rhs)
665  { return !(__lhs < __rhs); }
666 
667  /// system_clock
669  {
670 #ifdef _GLIBCXX_USE_CLOCK_REALTIME
672 #elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
674 #else
675  typedef chrono::seconds duration;
676 #endif
677 
678  typedef duration::rep rep;
679  typedef duration::period period;
681 
682  static_assert(system_clock::duration::min()
683  < system_clock::duration::zero(),
684  "a clock's minimum duration cannot be less than its epoch");
685 
686  static constexpr bool is_steady = false;
687 
688  static time_point
689  now() noexcept;
690 
691  // Map to C API
692  static std::time_t
693  to_time_t(const time_point& __t) noexcept
694  {
695  return std::time_t(duration_cast<chrono::seconds>
696  (__t.time_since_epoch()).count());
697  }
698 
699  static time_point
700  from_time_t(std::time_t __t) noexcept
701  {
704  (__from(chrono::seconds(__t)));
705  }
706  };
707 
708 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
709  /// steady_clock
710  struct steady_clock
711  {
713  typedef duration::rep rep;
714  typedef duration::period period;
716 
717  static constexpr bool is_steady = true;
718 
719  static time_point
720  now() noexcept;
721  };
722 #else
723  typedef system_clock steady_clock;
724 #endif
725 
727 
728  _GLIBCXX_END_NAMESPACE_VERSION
729  } // namespace chrono
730 
731  // @} group chrono
732 } // namespace
733 
734 #endif //_GLIBCXX_USE_C99_STDINT_TR1
735 
736 #endif //__GXX_EXPERIMENTAL_CXX0X__
737 
738 #endif //_GLIBCXX_CHRONO