date_duration.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #ifndef DATE_TIME_DATE_DURATION__
  2. #define DATE_TIME_DATE_DURATION__
  3. /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include <boost/operators.hpp>
  11. #include <boost/date_time/special_defs.hpp>
  12. #include <boost/date_time/compiler_config.hpp>
  13. #include <boost/date_time/int_adapter.hpp>
  14. namespace boost {
  15. namespace date_time {
  16. //! Duration type with date level resolution
  17. template<class duration_rep_traits>
  18. class BOOST_SYMBOL_VISIBLE date_duration : private
  19. boost::less_than_comparable1< date_duration< duration_rep_traits >
  20. , boost::equality_comparable1< date_duration< duration_rep_traits >
  21. , boost::addable1< date_duration< duration_rep_traits >
  22. , boost::subtractable1< date_duration< duration_rep_traits >
  23. , boost::dividable2< date_duration< duration_rep_traits >, int
  24. > > > > >
  25. {
  26. public:
  27. typedef typename duration_rep_traits::int_type duration_rep_type;
  28. typedef typename duration_rep_traits::impl_type duration_rep;
  29. //! Construct from a day count
  30. BOOST_CXX14_CONSTEXPR explicit date_duration(duration_rep day_count) : days_(day_count) {}
  31. /*! construct from special_values - only works when
  32. * instantiated with duration_traits_adapted */
  33. BOOST_CXX14_CONSTEXPR date_duration(special_values sv) :
  34. days_(duration_rep::from_special(sv))
  35. {}
  36. //! returns days_ as it's instantiated type - used for streaming
  37. BOOST_CXX14_CONSTEXPR duration_rep get_rep()const
  38. {
  39. return days_;
  40. }
  41. BOOST_CXX14_CONSTEXPR special_values as_special() const
  42. {
  43. return days_.as_special();
  44. }
  45. BOOST_CXX14_CONSTEXPR bool is_special()const
  46. {
  47. return days_.is_special();
  48. }
  49. //! returns days as value, not object.
  50. BOOST_CXX14_CONSTEXPR duration_rep_type days() const
  51. {
  52. return duration_rep_traits::as_number(days_);
  53. }
  54. //! Returns the smallest duration -- used by to calculate 'end'
  55. static BOOST_CXX14_CONSTEXPR date_duration unit()
  56. {
  57. return date_duration<duration_rep_traits>(1);
  58. }
  59. //! Equality
  60. BOOST_CXX14_CONSTEXPR bool operator==(const date_duration& rhs) const
  61. {
  62. return days_ == rhs.days_;
  63. }
  64. //! Less
  65. BOOST_CXX14_CONSTEXPR bool operator<(const date_duration& rhs) const
  66. {
  67. return days_ < rhs.days_;
  68. }
  69. /* For shortcut operators (+=, -=, etc) simply using
  70. * "days_ += days_" may not work. If instantiated with
  71. * an int_adapter, shortcut operators are not present,
  72. * so this will not compile */
  73. //! Subtract another duration -- result is signed
  74. BOOST_CXX14_CONSTEXPR date_duration& operator-=(const date_duration& rhs)
  75. {
  76. //days_ -= rhs.days_;
  77. days_ = days_ - rhs.days_;
  78. return *this;
  79. }
  80. //! Add a duration -- result is signed
  81. BOOST_CXX14_CONSTEXPR date_duration& operator+=(const date_duration& rhs)
  82. {
  83. days_ = days_ + rhs.days_;
  84. return *this;
  85. }
  86. //! unary- Allows for dd = -date_duration(2); -> dd == -2
  87. BOOST_CXX14_CONSTEXPR date_duration operator-() const
  88. {
  89. return date_duration<duration_rep_traits>(get_rep() * (-1));
  90. }
  91. //! Division operations on a duration with an integer.
  92. BOOST_CXX14_CONSTEXPR date_duration& operator/=(int divisor)
  93. {
  94. days_ = days_ / divisor;
  95. return *this;
  96. }
  97. //! return sign information
  98. BOOST_CXX14_CONSTEXPR bool is_negative() const
  99. {
  100. return days_ < 0;
  101. }
  102. private:
  103. duration_rep days_;
  104. };
  105. /*! Struct for instantiating date_duration with <b>NO</b> special values
  106. * functionality. Allows for transparent implementation of either
  107. * date_duration<long> or date_duration<int_adapter<long> > */
  108. struct BOOST_SYMBOL_VISIBLE duration_traits_long
  109. {
  110. typedef long int_type;
  111. typedef long impl_type;
  112. static BOOST_CXX14_CONSTEXPR int_type as_number(impl_type i) { return i; }
  113. };
  114. /*! Struct for instantiating date_duration <b>WITH</b> special values
  115. * functionality. Allows for transparent implementation of either
  116. * date_duration<long> or date_duration<int_adapter<long> > */
  117. struct BOOST_SYMBOL_VISIBLE duration_traits_adapted
  118. {
  119. typedef long int_type;
  120. typedef boost::date_time::int_adapter<long> impl_type;
  121. static BOOST_CXX14_CONSTEXPR int_type as_number(impl_type i) { return i.as_number(); }
  122. };
  123. } } //namspace date_time
  124. #endif