date_duration_operators.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef DATE_DURATION_OPERATORS_HPP___
  2. #define DATE_DURATION_OPERATORS_HPP___
  3. /* Copyright (c) 2004 CrystalClear Software, Inc.
  4. * Subject to the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include "boost/date_time/gregorian/greg_duration_types.hpp"
  11. #include "boost/date_time/posix_time/ptime.hpp"
  12. namespace boost {
  13. namespace posix_time {
  14. /*!@file date_duration_operators.hpp Operators for ptime and
  15. * optional gregorian types. Operators use snap-to-end-of-month behavior.
  16. * Further details on this behavior can be found in reference for
  17. * date_time/date_duration_types.hpp and documentation for
  18. * month and year iterators.
  19. */
  20. /*! Adds a months object and a ptime. Result will be same
  21. * day-of-month as ptime unless original day was the last day of month.
  22. * see date_time::months_duration for more details */
  23. inline
  24. ptime
  25. operator+(const ptime& t, const boost::gregorian::months& m)
  26. {
  27. return t + m.get_offset(t.date());
  28. }
  29. /*! Adds a months object to a ptime. Result will be same
  30. * day-of-month as ptime unless original day was the last day of month.
  31. * see date_time::months_duration for more details */
  32. inline
  33. ptime
  34. operator+=(ptime& t, const boost::gregorian::months& m)
  35. {
  36. // get_neg_offset returns a negative duration, so we add
  37. return t += m.get_offset(t.date());
  38. }
  39. /*! Subtracts a months object and a ptime. Result will be same
  40. * day-of-month as ptime unless original day was the last day of month.
  41. * see date_time::months_duration for more details */
  42. inline
  43. ptime
  44. operator-(const ptime& t, const boost::gregorian::months& m)
  45. {
  46. // get_neg_offset returns a negative duration, so we add
  47. return t + m.get_neg_offset(t.date());
  48. }
  49. /*! Subtracts a months object from a ptime. Result will be same
  50. * day-of-month as ptime unless original day was the last day of month.
  51. * see date_time::months_duration for more details */
  52. inline
  53. ptime
  54. operator-=(ptime& t, const boost::gregorian::months& m)
  55. {
  56. return t += m.get_neg_offset(t.date());
  57. }
  58. // ptime & years
  59. /*! Adds a years object and a ptime. Result will be same
  60. * month and day-of-month as ptime unless original day was the
  61. * last day of month. see date_time::years_duration for more details */
  62. inline
  63. ptime
  64. operator+(const ptime& t, const boost::gregorian::years& y)
  65. {
  66. return t + y.get_offset(t.date());
  67. }
  68. /*! Adds a years object to a ptime. Result will be same
  69. * month and day-of-month as ptime unless original day was the
  70. * last day of month. see date_time::years_duration for more details */
  71. inline
  72. ptime
  73. operator+=(ptime& t, const boost::gregorian::years& y)
  74. {
  75. return t += y.get_offset(t.date());
  76. }
  77. /*! Subtracts a years object and a ptime. Result will be same
  78. * month and day-of-month as ptime unless original day was the
  79. * last day of month. see date_time::years_duration for more details */
  80. inline
  81. ptime
  82. operator-(const ptime& t, const boost::gregorian::years& y)
  83. {
  84. // get_neg_offset returns a negative duration, so we add
  85. return t + y.get_neg_offset(t.date());
  86. }
  87. /*! Subtracts a years object from a ptime. Result will be same
  88. * month and day-of-month as ptime unless original day was the
  89. * last day of month. see date_time::years_duration for more details */
  90. inline
  91. ptime
  92. operator-=(ptime& t, const boost::gregorian::years& y)
  93. {
  94. // get_neg_offset returns a negative duration, so we add
  95. return t += y.get_neg_offset(t.date());
  96. }
  97. }} // namespaces
  98. #endif // DATE_DURATION_OPERATORS_HPP___