greg_duration.hpp 3.8 KB

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