date_duration.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. namespace boost {
  14. namespace date_time {
  15. //! Duration type with date level resolution
  16. template<class duration_rep_traits>
  17. class BOOST_SYMBOL_VISIBLE date_duration : private
  18. boost::less_than_comparable1< date_duration< duration_rep_traits >
  19. , boost::equality_comparable1< date_duration< duration_rep_traits >
  20. , boost::addable1< date_duration< duration_rep_traits >
  21. , boost::subtractable1< date_duration< duration_rep_traits >
  22. , boost::dividable2< date_duration< duration_rep_traits >, int
  23. > > > > >
  24. {
  25. public:
  26. typedef typename duration_rep_traits::int_type duration_rep_type;
  27. typedef typename duration_rep_traits::impl_type duration_rep;
  28. //! Construct from a day count
  29. explicit date_duration(duration_rep day_count) : days_(day_count) {}
  30. /*! construct from special_values - only works when
  31. * instantiated with duration_traits_adapted */
  32. date_duration(special_values sv) :
  33. days_(duration_rep::from_special(sv))
  34. {}
  35. // copy constructor required for addable<> & subtractable<>
  36. //! Construct from another date_duration (Copy Constructor)
  37. date_duration(const date_duration<duration_rep_traits>& other) :
  38. days_(other.days_)
  39. {}
  40. //! returns days_ as it's instantiated type - used for streaming
  41. duration_rep get_rep()const
  42. {
  43. return days_;
  44. }
  45. special_values as_special() const
  46. {
  47. return days_.as_special();
  48. }
  49. bool is_special()const
  50. {
  51. return days_.is_special();
  52. }
  53. //! returns days as value, not object.
  54. duration_rep_type days() const
  55. {
  56. return duration_rep_traits::as_number(days_);
  57. }
  58. //! Returns the smallest duration -- used by to calculate 'end'
  59. static date_duration unit()
  60. {
  61. return date_duration<duration_rep_traits>(1);
  62. }
  63. //! Equality
  64. bool operator==(const date_duration& rhs) const
  65. {
  66. return days_ == rhs.days_;
  67. }
  68. //! Less
  69. bool operator<(const date_duration& rhs) const
  70. {
  71. return days_ < rhs.days_;
  72. }
  73. /* For shortcut operators (+=, -=, etc) simply using
  74. * "days_ += days_" may not work. If instantiated with
  75. * an int_adapter, shortcut operators are not present,
  76. * so this will not compile */
  77. //! Subtract another duration -- result is signed
  78. date_duration& operator-=(const date_duration& rhs)
  79. {
  80. //days_ -= rhs.days_;
  81. days_ = days_ - rhs.days_;
  82. return *this;
  83. }
  84. //! Add a duration -- result is signed
  85. date_duration& operator+=(const date_duration& rhs)
  86. {
  87. days_ = days_ + rhs.days_;
  88. return *this;
  89. }
  90. //! unary- Allows for dd = -date_duration(2); -> dd == -2
  91. date_duration operator-() const
  92. {
  93. return date_duration<duration_rep_traits>(get_rep() * (-1));
  94. }
  95. //! Division operations on a duration with an integer.
  96. date_duration& operator/=(int divisor)
  97. {
  98. days_ = days_ / divisor;
  99. return *this;
  100. }
  101. //! return sign information
  102. bool is_negative() const
  103. {
  104. return days_ < 0;
  105. }
  106. private:
  107. duration_rep days_;
  108. };
  109. /*! Struct for instantiating date_duration with <b>NO</b> special values
  110. * functionality. Allows for transparent implementation of either
  111. * date_duration<long> or date_duration<int_adapter<long> > */
  112. struct BOOST_SYMBOL_VISIBLE duration_traits_long
  113. {
  114. typedef long int_type;
  115. typedef long impl_type;
  116. static int_type as_number(impl_type i) { return i; }
  117. };
  118. /*! Struct for instantiating date_duration <b>WITH</b> special values
  119. * functionality. Allows for transparent implementation of either
  120. * date_duration<long> or date_duration<int_adapter<long> > */
  121. struct BOOST_SYMBOL_VISIBLE duration_traits_adapted
  122. {
  123. typedef long int_type;
  124. typedef boost::date_time::int_adapter<long> impl_type;
  125. static int_type as_number(impl_type i) { return i.as_number(); }
  126. };
  127. } } //namspace date_time
  128. #endif