greg_duration.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef GREG_DURATION_HPP___
  2. #define GREG_DURATION_HPP___
  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/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. explicit date_duration(duration_rep day_count = 0) : base_type(day_count) {}
  29. //! construct from special_values
  30. date_duration(date_time::special_values sv) : base_type(sv) {}
  31. //! Copy constructor
  32. date_duration(const date_duration& other) : base_type(static_cast< base_type const& >(other))
  33. {}
  34. //! Construct from another date_duration
  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. bool operator== (const date_duration& rhs) const
  42. {
  43. return base_type::operator== (rhs);
  44. }
  45. bool operator!= (const date_duration& rhs) const
  46. {
  47. return !operator== (rhs);
  48. }
  49. bool operator< (const date_duration& rhs) const
  50. {
  51. return base_type::operator< (rhs);
  52. }
  53. bool operator> (const date_duration& rhs) const
  54. {
  55. return !(base_type::operator< (rhs) || base_type::operator== (rhs));
  56. }
  57. bool operator<= (const date_duration& rhs) const
  58. {
  59. return (base_type::operator< (rhs) || base_type::operator== (rhs));
  60. }
  61. bool operator>= (const date_duration& rhs) const
  62. {
  63. return !base_type::operator< (rhs);
  64. }
  65. //! Subtract another duration -- result is signed
  66. date_duration& operator-= (const date_duration& rhs)
  67. {
  68. base_type::operator-= (rhs);
  69. return *this;
  70. }
  71. friend date_duration operator- (date_duration rhs, date_duration const& lhs)
  72. {
  73. rhs -= lhs;
  74. return rhs;
  75. }
  76. //! Add a duration -- result is signed
  77. date_duration& operator+= (const date_duration& rhs)
  78. {
  79. base_type::operator+= (rhs);
  80. return *this;
  81. }
  82. friend date_duration operator+ (date_duration rhs, date_duration const& lhs)
  83. {
  84. rhs += lhs;
  85. return rhs;
  86. }
  87. //! unary- Allows for dd = -date_duration(2); -> dd == -2
  88. date_duration operator- ()const
  89. {
  90. return date_duration(get_rep() * (-1));
  91. }
  92. //! Division operations on a duration with an integer.
  93. date_duration& operator/= (int divisor)
  94. {
  95. base_type::operator/= (divisor);
  96. return *this;
  97. }
  98. friend date_duration operator/ (date_duration rhs, int lhs)
  99. {
  100. rhs /= lhs;
  101. return rhs;
  102. }
  103. //! Returns the smallest duration -- used by to calculate 'end'
  104. static date_duration unit()
  105. {
  106. return date_duration(base_type::unit().get_rep());
  107. }
  108. };
  109. //! Shorthand for date_duration
  110. typedef date_duration days;
  111. } } //namespace gregorian
  112. #if defined(BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES)
  113. #include <boost/date_time/date_duration_types.hpp>
  114. #endif
  115. #endif