gregorian_calendar.ipp 7.8 KB

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