gregorian_calendar.ipp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
  2. * Use, modification and distribution is subject to the
  3. * Boost Software License, Version 1.0. (See accompanying
  4. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  5. * Author: Jeff Garland, Bart Garst
  6. * $Date$
  7. */
  8. namespace boost {
  9. namespace date_time {
  10. //! Return the day of the week (0==Sunday, 1==Monday, etc)
  11. /*! Converts a year-month-day into a day of the week number
  12. */
  13. template<typename ymd_type_, typename date_int_type_>
  14. BOOST_CXX14_CONSTEXPR
  15. inline
  16. unsigned short
  17. gregorian_calendar_base<ymd_type_,date_int_type_>::day_of_week(const ymd_type& ymd) {
  18. unsigned short a = static_cast<unsigned short>((14-ymd.month)/12);
  19. unsigned short y = static_cast<unsigned short>(ymd.year - a);
  20. unsigned short m = static_cast<unsigned short>(ymd.month + 12*a - 2);
  21. unsigned short d = static_cast<unsigned short>((ymd.day + y + (y/4) - (y/100) + (y/400) + (31*m)/12) % 7);
  22. //std::cout << year << "-" << month << "-" << day << " is day: " << d << "\n";
  23. return d;
  24. }
  25. //!Return the ISO 8601 week number for the date
  26. /*!Implements the rules associated with the ISO 8601 week number.
  27. Basically the rule is that Week 1 of the year is the week that contains
  28. January 4th or the week that contains the first Thursday in January.
  29. Reference for this algorithm is the Calendar FAQ by Claus Tondering, April 2000.
  30. */
  31. template<typename ymd_type_, typename date_int_type_>
  32. BOOST_CXX14_CONSTEXPR
  33. inline
  34. int
  35. gregorian_calendar_base<ymd_type_,date_int_type_>::week_number(const ymd_type& ymd) {
  36. unsigned long julianbegin = julian_day_number(ymd_type(ymd.year,1,1));
  37. unsigned long juliantoday = julian_day_number(ymd);
  38. unsigned long day = (julianbegin + 3) % 7;
  39. unsigned long week = (juliantoday + day - julianbegin + 4)/7;
  40. if ((week >= 1) && (week <= 52)) {
  41. return static_cast<int>(week);
  42. }
  43. if (week == 53) {
  44. if((day==6) ||(day == 5 && is_leap_year(ymd.year))) {
  45. return static_cast<int>(week); //under these circumstances week == 53.
  46. } else {
  47. return 1; //monday - wednesday is in week 1 of next year
  48. }
  49. }
  50. //if the week is not in current year recalculate using the previous year as the beginning year
  51. else if (week == 0) {
  52. julianbegin = julian_day_number(ymd_type(static_cast<unsigned short>(ymd.year-1),1,1));
  53. juliantoday = julian_day_number(ymd);
  54. day = (julianbegin + 3) % 7;
  55. week = (juliantoday + day - julianbegin + 4)/7;
  56. return static_cast<int>(week);
  57. }
  58. return static_cast<int>(week); //not reachable -- well except if day == 5 and is_leap_year != true
  59. }
  60. //! Convert a ymd_type into a day number
  61. /*! The day number is an absolute number of days since the start of count
  62. */
  63. template<typename ymd_type_, typename date_int_type_>
  64. BOOST_CXX14_CONSTEXPR
  65. inline
  66. date_int_type_
  67. gregorian_calendar_base<ymd_type_,date_int_type_>::day_number(const ymd_type& ymd)
  68. {
  69. unsigned short a = static_cast<unsigned short>((14-ymd.month)/12);
  70. unsigned short y = static_cast<unsigned short>(ymd.year + 4800 - a);
  71. unsigned short m = static_cast<unsigned short>(ymd.month + 12*a - 3);
  72. unsigned long d = static_cast<unsigned long>(ymd.day) + ((153*m + 2)/5) + 365*y + (y/4) - (y/100) + (y/400) - 32045;
  73. return static_cast<date_int_type>(d);
  74. }
  75. //! Convert a year-month-day into the julian day number
  76. /*! Since this implementation uses julian day internally, this is the same as the day_number.
  77. */
  78. template<typename ymd_type_, typename date_int_type_>
  79. BOOST_CXX14_CONSTEXPR
  80. inline
  81. date_int_type_
  82. gregorian_calendar_base<ymd_type_,date_int_type_>::julian_day_number(const ymd_type& ymd)
  83. {
  84. return day_number(ymd);
  85. }
  86. //! Convert year-month-day into a modified julian day number
  87. /*! The day number is an absolute number of days.
  88. * MJD 0 thus started on 17 Nov 1858(Gregorian) at 00:00:00 UTC
  89. */
  90. template<typename ymd_type_, typename date_int_type_>
  91. BOOST_CXX14_CONSTEXPR
  92. inline
  93. date_int_type_
  94. gregorian_calendar_base<ymd_type_,date_int_type_>::modjulian_day_number(const ymd_type& ymd)
  95. {
  96. return julian_day_number(ymd)-2400001; //prerounded
  97. }
  98. //! Change a day number into a year-month-day
  99. template<typename ymd_type_, typename date_int_type_>
  100. BOOST_CXX14_CONSTEXPR
  101. inline
  102. ymd_type_
  103. gregorian_calendar_base<ymd_type_,date_int_type_>::from_day_number(date_int_type dayNumber)
  104. {
  105. date_int_type a = dayNumber + 32044;
  106. date_int_type b = (4*a + 3)/146097;
  107. date_int_type c = a-((146097*b)/4);
  108. date_int_type d = (4*c + 3)/1461;
  109. date_int_type e = c - (1461*d)/4;
  110. date_int_type m = (5*e + 2)/153;
  111. unsigned short day = static_cast<unsigned short>(e - ((153*m + 2)/5) + 1);
  112. unsigned short month = static_cast<unsigned short>(m + 3 - 12 * (m/10));
  113. year_type year = static_cast<unsigned short>(100*b + d - 4800 + (m/10));
  114. //std::cout << year << "-" << month << "-" << day << "\n";
  115. return ymd_type(static_cast<unsigned short>(year),month,day);
  116. }
  117. //! Change a day number into a year-month-day
  118. template<typename ymd_type_, typename date_int_type_>
  119. BOOST_CXX14_CONSTEXPR
  120. inline
  121. ymd_type_
  122. gregorian_calendar_base<ymd_type_,date_int_type_>::from_julian_day_number(date_int_type dayNumber)
  123. {
  124. date_int_type a = dayNumber + 32044;
  125. date_int_type b = (4*a+3)/146097;
  126. date_int_type c = a - ((146097*b)/4);
  127. date_int_type d = (4*c + 3)/1461;
  128. date_int_type e = c - ((1461*d)/4);
  129. date_int_type m = (5*e + 2)/153;
  130. unsigned short day = static_cast<unsigned short>(e - ((153*m + 2)/5) + 1);
  131. unsigned short month = static_cast<unsigned short>(m + 3 - 12 * (m/10));
  132. year_type year = static_cast<year_type>(100*b + d - 4800 + (m/10));
  133. //std::cout << year << "-" << month << "-" << day << "\n";
  134. return ymd_type(year,month,day);
  135. }
  136. //! Change a modified julian day number into a year-month-day
  137. template<typename ymd_type_, typename date_int_type_>
  138. BOOST_CXX14_CONSTEXPR
  139. inline
  140. ymd_type_
  141. gregorian_calendar_base<ymd_type_,date_int_type_>::from_modjulian_day_number(date_int_type dayNumber) {
  142. date_int_type jd = dayNumber + 2400001; //is 2400000.5 prerounded
  143. return from_julian_day_number(jd);
  144. }
  145. //! Determine if the provided year is a leap year
  146. /*!
  147. *@return true if year is a leap year, false otherwise
  148. */
  149. template<typename ymd_type_, typename date_int_type_>
  150. BOOST_CXX14_CONSTEXPR
  151. inline
  152. bool
  153. gregorian_calendar_base<ymd_type_,date_int_type_>::is_leap_year(year_type year)
  154. {
  155. //divisible by 4, not if divisible by 100, but true if divisible by 400
  156. return (!(year % 4)) && ((year % 100) || (!(year % 400)));
  157. }
  158. //! Calculate the last day of the month
  159. /*! Find the day which is the end of the month given year and month
  160. * No error checking is performed.
  161. */
  162. template<typename ymd_type_, typename date_int_type_>
  163. BOOST_CXX14_CONSTEXPR
  164. inline
  165. unsigned short
  166. gregorian_calendar_base<ymd_type_,date_int_type_>::end_of_month_day(year_type year,
  167. month_type month)
  168. {
  169. switch (month) {
  170. case 2:
  171. if (is_leap_year(year)) {
  172. return 29;
  173. } else {
  174. return 28;
  175. }
  176. case 4:
  177. case 6:
  178. case 9:
  179. case 11:
  180. return 30;
  181. default:
  182. return 31;
  183. }
  184. }
  185. //! Provide the ymd_type specification for the calendar start
  186. template<typename ymd_type_, typename date_int_type_>
  187. BOOST_CXX14_CONSTEXPR
  188. inline
  189. ymd_type_
  190. gregorian_calendar_base<ymd_type_,date_int_type_>::epoch()
  191. {
  192. return ymd_type(1400,1,1);
  193. }
  194. //! Defines length of a week for week calculations
  195. template<typename ymd_type_, typename date_int_type_>
  196. BOOST_CXX14_CONSTEXPR
  197. inline
  198. unsigned short
  199. gregorian_calendar_base<ymd_type_,date_int_type_>::days_in_week()
  200. {
  201. return 7;
  202. }
  203. } } //namespace gregorian