1 //  i_dont_like_the_default_duration_behavior.cpp  ----------------------------------------------------------//
2 
3 //  Copyright 2008 Howard Hinnant
4 //  Copyright 2008 Beman Dawes
5 //  Copyright 2009 Vicente J. Botet Escriba
6 
7 //  Distributed under the Boost Software License, Version 1.0.
8 //  See http://www.boost.org/LICENSE_1_0.txt
9 
10 /*
11 This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
12 was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
13 Many thanks to Howard for making his code available under the Boost license.
14 The original code was modified to conform to Boost conventions and to section
15 20.9 Time utilities [time] of the C++ committee's working paper N2798.
16 See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
17 
18 time2_demo contained this comment:
19 
20     Much thanks to Andrei Alexandrescu,
21                    Walter Brown,
22                    Peter Dimov,
23                    Jeff Garland,
24                    Terry Golubiewski,
25                    Daniel Krugler,
26                    Anthony Williams.
27 */
28 
29 #include <boost/chrono/chrono.hpp>
30 #include <boost/type_traits.hpp>
31 
32 #include <iostream>
33 
34 namespace I_dont_like_the_default_duration_behavior
35 {
36 
37 // Here's how you override the duration's default constructor to do anything you want (in this case zero)
38 
39 template <class R>
40 class zero_default
41 {
42 public:
43     typedef R rep;
44 
45 private:
46     rep rep_;
47 public:
zero_default(rep i=0)48     zero_default(rep i = 0) : rep_(i) {}
operator rep() const49     operator rep() const {return rep_;}
50 
operator +=(zero_default x)51     zero_default& operator+=(zero_default x) {rep_ += x.rep_; return *this;}
operator -=(zero_default x)52     zero_default& operator-=(zero_default x) {rep_ -= x.rep_; return *this;}
operator *=(zero_default x)53     zero_default& operator*=(zero_default x) {rep_ *= x.rep_; return *this;}
operator /=(zero_default x)54     zero_default& operator/=(zero_default x) {rep_ /= x.rep_; return *this;}
55 
operator +() const56     zero_default  operator+ () const {return *this;}
operator -() const57     zero_default  operator- () const {return zero_default(-rep_);}
operator ++()58     zero_default& operator++()       {++rep_; return *this;}
operator ++(int)59     zero_default  operator++(int)    {return zero_default(rep_++);}
operator --()60     zero_default& operator--()       {--rep_; return *this;}
operator --(int)61     zero_default  operator--(int)    {return zero_default(rep_--);}
62 
operator +(zero_default x,zero_default y)63     friend zero_default operator+(zero_default x, zero_default y) {return x += y;}
operator -(zero_default x,zero_default y)64     friend zero_default operator-(zero_default x, zero_default y) {return x -= y;}
operator *(zero_default x,zero_default y)65     friend zero_default operator*(zero_default x, zero_default y) {return x *= y;}
operator /(zero_default x,zero_default y)66     friend zero_default operator/(zero_default x, zero_default y) {return x /= y;}
67 
operator ==(zero_default x,zero_default y)68     friend bool operator==(zero_default x, zero_default y) {return x.rep_ == y.rep_;}
operator !=(zero_default x,zero_default y)69     friend bool operator!=(zero_default x, zero_default y) {return !(x == y);}
operator <(zero_default x,zero_default y)70     friend bool operator< (zero_default x, zero_default y) {return x.rep_ < y.rep_;}
operator <=(zero_default x,zero_default y)71     friend bool operator<=(zero_default x, zero_default y) {return !(y < x);}
operator >(zero_default x,zero_default y)72     friend bool operator> (zero_default x, zero_default y) {return y < x;}
operator >=(zero_default x,zero_default y)73     friend bool operator>=(zero_default x, zero_default y) {return !(x < y);}
74 };
75 
76 typedef boost::chrono::duration<zero_default<long long>, boost::nano        > nanoseconds;
77 typedef boost::chrono::duration<zero_default<long long>, boost::micro       > microseconds;
78 typedef boost::chrono::duration<zero_default<long long>, boost::milli       > milliseconds;
79 typedef boost::chrono::duration<zero_default<long long>                      > seconds;
80 typedef boost::chrono::duration<zero_default<long long>, boost::ratio<60>   > minutes;
81 typedef boost::chrono::duration<zero_default<long long>, boost::ratio<3600> > hours;
82 
test()83 void test()
84 {
85     milliseconds ms;
86     std::cout << ms.count() << '\n';
87 }
88 
89 }  // I_dont_like_the_default_duration_behavior
90 
91 
main()92 int main()
93 {
94     I_dont_like_the_default_duration_behavior::test();
95     return 0;
96 }
97 
98