greg_date.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef GREG_DATE_HPP___
  2. #define GREG_DATE_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
  8. * $Date$
  9. */
  10. #include <boost/throw_exception.hpp>
  11. #include <boost/date_time/compiler_config.hpp>
  12. #include <boost/date_time/date.hpp>
  13. #include <boost/date_time/special_defs.hpp>
  14. #include <boost/date_time/gregorian/greg_calendar.hpp>
  15. #include <boost/date_time/gregorian/greg_duration.hpp>
  16. namespace boost {
  17. namespace gregorian {
  18. //bring special enum values into the namespace
  19. using date_time::special_values;
  20. using date_time::not_special;
  21. using date_time::neg_infin;
  22. using date_time::pos_infin;
  23. using date_time::not_a_date_time;
  24. using date_time::max_date_time;
  25. using date_time::min_date_time;
  26. //! A date type based on gregorian_calendar
  27. /*! This class is the primary interface for programming with
  28. greogorian dates. The is a lightweight type that can be
  29. freely passed by value. All comparison operators are
  30. supported.
  31. \ingroup date_basics
  32. */
  33. class BOOST_SYMBOL_VISIBLE date : public date_time::date<date, gregorian_calendar, date_duration>
  34. {
  35. public:
  36. typedef gregorian_calendar::year_type year_type;
  37. typedef gregorian_calendar::month_type month_type;
  38. typedef gregorian_calendar::day_type day_type;
  39. typedef gregorian_calendar::day_of_year_type day_of_year_type;
  40. typedef gregorian_calendar::ymd_type ymd_type;
  41. typedef gregorian_calendar::date_rep_type date_rep_type;
  42. typedef gregorian_calendar::date_int_type date_int_type;
  43. typedef date_duration duration_type;
  44. #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
  45. //! Default constructor constructs with not_a_date_time
  46. BOOST_CXX14_CONSTEXPR date():
  47. date_time::date<date, gregorian_calendar, date_duration>(date_rep_type::from_special(not_a_date_time))
  48. {}
  49. #endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR
  50. //! Main constructor with year, month, day
  51. BOOST_CXX14_CONSTEXPR date(year_type y, month_type m, day_type d)
  52. : date_time::date<date, gregorian_calendar, date_duration>(y, m, d)
  53. {
  54. if (gregorian_calendar::end_of_month_day(y, m) < d) {
  55. boost::throw_exception(bad_day_of_month(std::string("Day of month is not valid for year")));
  56. }
  57. }
  58. //! Constructor from a ymd_type structure
  59. BOOST_CXX14_CONSTEXPR explicit date(const ymd_type& ymd)
  60. : date_time::date<date, gregorian_calendar, date_duration>(ymd)
  61. {}
  62. //! Needed copy constructor
  63. BOOST_CXX14_CONSTEXPR explicit date(const date_int_type& rhs):
  64. date_time::date<date,gregorian_calendar, date_duration>(rhs)
  65. {}
  66. //! Needed copy constructor
  67. BOOST_CXX14_CONSTEXPR explicit date(date_rep_type rhs):
  68. date_time::date<date,gregorian_calendar, date_duration>(rhs)
  69. {}
  70. //! Constructor for infinities, not a date, max and min date
  71. BOOST_CXX14_CONSTEXPR explicit date(special_values sv):
  72. date_time::date<date, gregorian_calendar, date_duration>(from_special_adjusted(sv))
  73. {}
  74. //!Return the Julian Day number for the date.
  75. BOOST_CXX14_CONSTEXPR date_int_type julian_day() const
  76. {
  77. ymd_type ymd = year_month_day();
  78. return gregorian_calendar::julian_day_number(ymd);
  79. }
  80. //!Return the day of year 1..365 or 1..366 (for leap year)
  81. BOOST_CXX14_CONSTEXPR day_of_year_type day_of_year() const
  82. {
  83. date start_of_year(year(), 1, 1);
  84. unsigned short doy = static_cast<unsigned short>((*this-start_of_year).days() + 1);
  85. return day_of_year_type(doy);
  86. }
  87. //!Return the Modified Julian Day number for the date.
  88. BOOST_CXX14_CONSTEXPR date_int_type modjulian_day() const
  89. {
  90. ymd_type ymd = year_month_day();
  91. return gregorian_calendar::modjulian_day_number(ymd);
  92. }
  93. //!Return the ISO 8601 week number 1..53
  94. BOOST_CXX14_CONSTEXPR int week_number() const
  95. {
  96. ymd_type ymd = year_month_day();
  97. return gregorian_calendar::week_number(ymd);
  98. }
  99. //! Return the day number from the calendar
  100. BOOST_CXX14_CONSTEXPR date_int_type day_number() const
  101. {
  102. return days_;
  103. }
  104. //! Return the last day of the current month
  105. BOOST_CXX14_CONSTEXPR date end_of_month() const
  106. {
  107. ymd_type ymd = year_month_day();
  108. unsigned short eom_day = gregorian_calendar::end_of_month_day(ymd.year, ymd.month);
  109. return date(ymd.year, ymd.month, eom_day);
  110. }
  111. friend BOOST_CXX14_CONSTEXPR
  112. bool operator==(const date& lhs, const date& rhs);
  113. private:
  114. BOOST_CXX14_CONSTEXPR date_rep_type from_special_adjusted(special_values sv)
  115. {
  116. switch (sv)
  117. {
  118. case min_date_time: return gregorian_calendar::day_number(ymd_type(1400, 1, 1));
  119. case max_date_time: return gregorian_calendar::day_number(ymd_type(9999, 12, 31));
  120. default: return date_rep_type::from_special(sv);
  121. }
  122. }
  123. };
  124. inline BOOST_CXX14_CONSTEXPR
  125. bool operator==(const date& lhs, const date& rhs)
  126. {
  127. return lhs.days_ == rhs.days_;
  128. }
  129. } } //namespace gregorian
  130. #endif