greg_duration.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef GREG_DURATION_HPP___
  2. #define GREG_DURATION_HPP___
  3. /* Copyright (c) 2002,2003, 2020 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/date_time/compiler_config.hpp>
  11. #include <boost/date_time/date_duration.hpp>
  12. #include <boost/date_time/int_adapter.hpp>
  13. #include <boost/date_time/special_defs.hpp>
  14. namespace boost {
  15. namespace gregorian {
  16. //!An internal date representation that includes infinities, not a date
  17. typedef boost::date_time::duration_traits_adapted date_duration_rep;
  18. //! Durations in days for gregorian system
  19. /*! \ingroup date_basics
  20. */
  21. class BOOST_SYMBOL_VISIBLE date_duration :
  22. public boost::date_time::date_duration< date_duration_rep >
  23. {
  24. typedef boost::date_time::date_duration< date_duration_rep > base_type;
  25. public:
  26. typedef base_type::duration_rep duration_rep;
  27. //! Construct from a day count
  28. BOOST_CXX14_CONSTEXPR explicit
  29. date_duration(duration_rep day_count = 0) : base_type(day_count) {}
  30. //! construct from special_values
  31. BOOST_CXX14_CONSTEXPR
  32. date_duration(date_time::special_values sv) : base_type(sv) {}
  33. //! Construct from another date_duration
  34. BOOST_CXX14_CONSTEXPR
  35. date_duration(const base_type& other) : base_type(other)
  36. {}
  37. // Relational operators
  38. // NOTE: Because of date_time::date_duration< T > design choice we don't use Boost.Operators here,
  39. // because we need the class to be a direct base. Either lose EBO, or define operators by hand.
  40. // The latter is more effecient.
  41. BOOST_CXX14_CONSTEXPR bool operator== (const date_duration& rhs) const
  42. {
  43. return base_type::operator== (rhs);
  44. }
  45. BOOST_CXX14_CONSTEXPR bool operator!= (const date_duration& rhs) const
  46. {
  47. return !operator== (rhs);
  48. }
  49. BOOST_CXX14_CONSTEXPR bool operator< (const date_duration& rhs) const
  50. {
  51. return base_type::operator< (rhs);
  52. }
  53. BOOST_CXX14_CONSTEXPR bool operator> (const date_duration& rhs) const
  54. {
  55. return !(base_type::operator< (rhs) || base_type::operator== (rhs));
  56. }
  57. BOOST_CXX14_CONSTEXPR bool operator<= (const date_duration& rhs) const
  58. {
  59. return (base_type::operator< (rhs) || base_type::operator== (rhs));
  60. }
  61. BOOST_CXX14_CONSTEXPR bool operator>= (const date_duration& rhs) const
  62. {
  63. return !base_type::operator< (rhs);
  64. }
  65. //! Subtract another duration -- result is signed
  66. BOOST_CXX14_CONSTEXPR date_duration& operator-= (const date_duration& rhs)
  67. {
  68. base_type::operator-= (rhs);
  69. return *this;
  70. }
  71. BOOST_CXX14_CONSTEXPR friend
  72. date_duration operator- (date_duration rhs, date_duration const& lhs);
  73. //! Add a duration -- result is signed
  74. BOOST_CXX14_CONSTEXPR date_duration& operator+= (const date_duration& rhs)
  75. {
  76. base_type::operator+= (rhs);
  77. return *this;
  78. }
  79. BOOST_CXX14_CONSTEXPR friend
  80. date_duration operator+ (date_duration rhs, date_duration const& lhs);
  81. //! unary- Allows for dd = -date_duration(2); -> dd == -2
  82. BOOST_CXX14_CONSTEXPR date_duration operator- ()const
  83. {
  84. return date_duration(get_rep() * (-1));
  85. }
  86. //! Division operations on a duration with an integer.
  87. BOOST_CXX14_CONSTEXPR date_duration& operator/= (int divisor)
  88. {
  89. base_type::operator/= (divisor);
  90. return *this;
  91. }
  92. BOOST_CXX14_CONSTEXPR friend date_duration operator/ (date_duration rhs, int lhs);
  93. //! Returns the smallest duration -- used by to calculate 'end'
  94. static BOOST_CXX14_CONSTEXPR date_duration unit()
  95. {
  96. return date_duration(base_type::unit().get_rep());
  97. }
  98. };
  99. inline BOOST_CXX14_CONSTEXPR
  100. date_duration operator- (date_duration rhs, date_duration const& lhs)
  101. {
  102. rhs -= lhs;
  103. return rhs;
  104. }
  105. inline BOOST_CXX14_CONSTEXPR
  106. date_duration operator+ (date_duration rhs, date_duration const& lhs)
  107. {
  108. rhs += lhs;
  109. return rhs;
  110. }
  111. inline BOOST_CXX14_CONSTEXPR date_duration operator/ (date_duration rhs, int lhs)
  112. {
  113. rhs /= lhs;
  114. return rhs;
  115. }
  116. //! Shorthand for date_duration
  117. typedef date_duration days;
  118. } } //namespace gregorian
  119. #if defined(BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES)
  120. #include <boost/date_time/date_duration_types.hpp>
  121. #endif
  122. #endif