annotate DEPENDENCIES/generic/include/boost/date_time/date_generators.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 #ifndef DATE_TIME_DATE_GENERATORS_HPP__
Chris@16 2 #define DATE_TIME_DATE_GENERATORS_HPP__
Chris@16 3
Chris@16 4 /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
Chris@16 5 * Use, modification and distribution is subject to the
Chris@16 6 * Boost Software License, Version 1.0. (See accompanying
Chris@16 7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 * Author: Jeff Garland, Bart Garst
Chris@101 9 * $Date$
Chris@16 10 */
Chris@16 11
Chris@16 12 /*! @file date_generators.hpp
Chris@16 13 Definition and implementation of date algorithm templates
Chris@16 14 */
Chris@16 15
Chris@16 16 #include <stdexcept>
Chris@16 17 #include <sstream>
Chris@16 18 #include <boost/throw_exception.hpp>
Chris@16 19 #include <boost/date_time/date.hpp>
Chris@16 20 #include <boost/date_time/compiler_config.hpp>
Chris@16 21
Chris@16 22 namespace boost {
Chris@16 23 namespace date_time {
Chris@16 24
Chris@16 25 //! Base class for all generators that take a year and produce a date.
Chris@16 26 /*! This class is a base class for polymorphic function objects that take
Chris@16 27 a year and produce a concrete date.
Chris@16 28 @param date_type The type representing a date. This type must
Chris@16 29 export a calender_type which defines a year_type.
Chris@16 30 */
Chris@16 31 template<class date_type>
Chris@16 32 class year_based_generator
Chris@16 33 {
Chris@16 34 public:
Chris@16 35 typedef typename date_type::calendar_type calendar_type;
Chris@16 36 typedef typename calendar_type::year_type year_type;
Chris@16 37 year_based_generator() {}
Chris@16 38 virtual ~year_based_generator() {}
Chris@16 39 virtual date_type get_date(year_type y) const = 0;
Chris@16 40 //! Returns a string for use in a POSIX time_zone string
Chris@16 41 virtual std::string to_string() const =0;
Chris@16 42 };
Chris@16 43
Chris@16 44 //! Generates a date by applying the year to the given month and day.
Chris@16 45 /*!
Chris@16 46 Example usage:
Chris@16 47 @code
Chris@16 48 partial_date pd(1, Jan);
Chris@16 49 partial_date pd2(70);
Chris@16 50 date d = pd.get_date(2002); //2002-Jan-01
Chris@16 51 date d2 = pd2.get_date(2002); //2002-Mar-10
Chris@16 52 @endcode
Chris@16 53 \ingroup date_alg
Chris@16 54 */
Chris@16 55 template<class date_type>
Chris@16 56 class partial_date : public year_based_generator<date_type>
Chris@16 57 {
Chris@16 58 public:
Chris@16 59 typedef typename date_type::calendar_type calendar_type;
Chris@16 60 typedef typename calendar_type::day_type day_type;
Chris@16 61 typedef typename calendar_type::month_type month_type;
Chris@16 62 typedef typename calendar_type::year_type year_type;
Chris@16 63 typedef typename date_type::duration_type duration_type;
Chris@16 64 typedef typename duration_type::duration_rep duration_rep;
Chris@16 65 partial_date(day_type d, month_type m) :
Chris@16 66 day_(d),
Chris@16 67 month_(m)
Chris@16 68 {}
Chris@16 69 //! Partial date created from number of days into year. Range 1-366
Chris@16 70 /*! Allowable values range from 1 to 366. 1=Jan1, 366=Dec31. If argument
Chris@16 71 * exceeds range, partial_date will be created with closest in-range value.
Chris@16 72 * 60 will always be Feb29, if get_date() is called with a non-leap year
Chris@16 73 * an exception will be thrown */
Chris@16 74 partial_date(duration_rep days) :
Chris@16 75 day_(1), // default values
Chris@16 76 month_(1)
Chris@16 77 {
Chris@16 78 date_type d1(2000,1,1);
Chris@16 79 if(days > 1) {
Chris@16 80 if(days > 366) // prevents wrapping
Chris@16 81 {
Chris@16 82 days = 366;
Chris@16 83 }
Chris@16 84 days = days - 1;
Chris@16 85 duration_type dd(days);
Chris@16 86 d1 = d1 + dd;
Chris@16 87 }
Chris@16 88 day_ = d1.day();
Chris@16 89 month_ = d1.month();
Chris@16 90 }
Chris@16 91 //! Return a concrete date when provided with a year specific year.
Chris@16 92 /*! Will throw an 'invalid_argument' exception if a partial_date object,
Chris@16 93 * instantiated with Feb-29, has get_date called with a non-leap year.
Chris@16 94 * Example:
Chris@16 95 * @code
Chris@16 96 * partial_date pd(29, Feb);
Chris@16 97 * pd.get_date(2003); // throws invalid_argument exception
Chris@16 98 * pg.get_date(2000); // returns 2000-2-29
Chris@16 99 * @endcode
Chris@16 100 */
Chris@16 101 date_type get_date(year_type y) const
Chris@16 102 {
Chris@16 103 if((day_ == 29) && (month_ == 2) && !(calendar_type::is_leap_year(y))) {
Chris@16 104 std::ostringstream ss;
Chris@16 105 ss << "No Feb 29th in given year of " << y << ".";
Chris@16 106 boost::throw_exception(std::invalid_argument(ss.str()));
Chris@16 107 }
Chris@16 108 return date_type(y, month_, day_);
Chris@16 109 }
Chris@16 110 date_type operator()(year_type y) const
Chris@16 111 {
Chris@16 112 return get_date(y);
Chris@16 113 //return date_type(y, month_, day_);
Chris@16 114 }
Chris@16 115 bool operator==(const partial_date& rhs) const
Chris@16 116 {
Chris@16 117 return (month_ == rhs.month_) && (day_ == rhs.day_);
Chris@16 118 }
Chris@16 119 bool operator<(const partial_date& rhs) const
Chris@16 120 {
Chris@16 121 if (month_ < rhs.month_) return true;
Chris@16 122 if (month_ > rhs.month_) return false;
Chris@16 123 //months are equal
Chris@16 124 return (day_ < rhs.day_);
Chris@16 125 }
Chris@16 126
Chris@16 127 // added for streaming purposes
Chris@16 128 month_type month() const
Chris@16 129 {
Chris@16 130 return month_;
Chris@16 131 }
Chris@16 132 day_type day() const
Chris@16 133 {
Chris@16 134 return day_;
Chris@16 135 }
Chris@16 136
Chris@16 137 //! Returns string suitable for use in POSIX time zone string
Chris@16 138 /*! Returns string formatted with up to 3 digits:
Chris@16 139 * Jan-01 == "0"
Chris@16 140 * Feb-29 == "58"
Chris@16 141 * Dec-31 == "365" */
Chris@16 142 virtual std::string to_string() const
Chris@16 143 {
Chris@16 144 std::ostringstream ss;
Chris@16 145 date_type d(2004, month_, day_);
Chris@16 146 unsigned short c = d.day_of_year();
Chris@16 147 c--; // numbered 0-365 while day_of_year is 1 based...
Chris@16 148 ss << c;
Chris@16 149 return ss.str();
Chris@16 150 }
Chris@16 151 private:
Chris@16 152 day_type day_;
Chris@16 153 month_type month_;
Chris@16 154 };
Chris@16 155
Chris@16 156
Chris@16 157 //! Returns nth arg as string. 1 -> "first", 2 -> "second", max is 5.
Chris@16 158 BOOST_DATE_TIME_DECL const char* nth_as_str(int n);
Chris@16 159
Chris@16 160 //! Useful generator functor for finding holidays
Chris@16 161 /*! Based on the idea in Cal. Calc. for finding holidays that are
Chris@16 162 * the 'first Monday of September'. When instantiated with
Chris@16 163 * 'fifth' kday of month, the result will be the last kday of month
Chris@16 164 * which can be the fourth or fifth depending on the structure of
Chris@16 165 * the month.
Chris@16 166 *
Chris@16 167 * The algorithm here basically guesses for the first
Chris@16 168 * day of the month. Then finds the first day of the correct
Chris@16 169 * type. That is, if the first of the month is a Tuesday
Chris@16 170 * and it needs Wenesday then we simply increment by a day
Chris@16 171 * and then we can add the length of a week until we get
Chris@16 172 * to the 'nth kday'. There are probably more efficient
Chris@16 173 * algorithms based on using a mod 7, but this one works
Chris@16 174 * reasonably well for basic applications.
Chris@16 175 * \ingroup date_alg
Chris@16 176 */
Chris@16 177 template<class date_type>
Chris@16 178 class nth_kday_of_month : public year_based_generator<date_type>
Chris@16 179 {
Chris@16 180 public:
Chris@16 181 typedef typename date_type::calendar_type calendar_type;
Chris@16 182 typedef typename calendar_type::day_of_week_type day_of_week_type;
Chris@16 183 typedef typename calendar_type::month_type month_type;
Chris@16 184 typedef typename calendar_type::year_type year_type;
Chris@16 185 typedef typename date_type::duration_type duration_type;
Chris@16 186 enum week_num {first=1, second, third, fourth, fifth};
Chris@16 187 nth_kday_of_month(week_num week_no,
Chris@16 188 day_of_week_type dow,
Chris@16 189 month_type m) :
Chris@16 190 month_(m),
Chris@16 191 wn_(week_no),
Chris@16 192 dow_(dow)
Chris@16 193 {}
Chris@16 194 //! Return a concrete date when provided with a year specific year.
Chris@16 195 date_type get_date(year_type y) const
Chris@16 196 {
Chris@16 197 date_type d(y, month_, 1); //first day of month
Chris@16 198 duration_type one_day(1);
Chris@16 199 duration_type one_week(7);
Chris@16 200 while (dow_ != d.day_of_week()) {
Chris@16 201 d = d + one_day;
Chris@16 202 }
Chris@16 203 int week = 1;
Chris@16 204 while (week < wn_) {
Chris@16 205 d = d + one_week;
Chris@16 206 week++;
Chris@16 207 }
Chris@16 208 // remove wrapping to next month behavior
Chris@16 209 if(d.month() != month_) {
Chris@16 210 d = d - one_week;
Chris@16 211 }
Chris@16 212 return d;
Chris@16 213 }
Chris@16 214 // added for streaming
Chris@16 215 month_type month() const
Chris@16 216 {
Chris@16 217 return month_;
Chris@16 218 }
Chris@16 219 week_num nth_week() const
Chris@16 220 {
Chris@16 221 return wn_;
Chris@16 222 }
Chris@16 223 day_of_week_type day_of_week() const
Chris@16 224 {
Chris@16 225 return dow_;
Chris@16 226 }
Chris@16 227 const char* nth_week_as_str() const
Chris@16 228 {
Chris@16 229 return nth_as_str(wn_);
Chris@16 230 }
Chris@16 231 //! Returns string suitable for use in POSIX time zone string
Chris@16 232 /*! Returns a string formatted as "M4.3.0" ==> 3rd Sunday in April. */
Chris@16 233 virtual std::string to_string() const
Chris@16 234 {
Chris@16 235 std::ostringstream ss;
Chris@16 236 ss << 'M'
Chris@16 237 << static_cast<int>(month_) << '.'
Chris@16 238 << static_cast<int>(wn_) << '.'
Chris@16 239 << static_cast<int>(dow_);
Chris@16 240 return ss.str();
Chris@16 241 }
Chris@16 242 private:
Chris@16 243 month_type month_;
Chris@16 244 week_num wn_;
Chris@16 245 day_of_week_type dow_;
Chris@16 246 };
Chris@16 247
Chris@16 248 //! Useful generator functor for finding holidays and daylight savings
Chris@16 249 /*! Similar to nth_kday_of_month, but requires less paramters
Chris@16 250 * \ingroup date_alg
Chris@16 251 */
Chris@16 252 template<class date_type>
Chris@16 253 class first_kday_of_month : public year_based_generator<date_type>
Chris@16 254 {
Chris@16 255 public:
Chris@16 256 typedef typename date_type::calendar_type calendar_type;
Chris@16 257 typedef typename calendar_type::day_of_week_type day_of_week_type;
Chris@16 258 typedef typename calendar_type::month_type month_type;
Chris@16 259 typedef typename calendar_type::year_type year_type;
Chris@16 260 typedef typename date_type::duration_type duration_type;
Chris@16 261 //!Specify the first 'Sunday' in 'April' spec
Chris@16 262 /*!@param dow The day of week, eg: Sunday, Monday, etc
Chris@16 263 * @param m The month of the year, eg: Jan, Feb, Mar, etc
Chris@16 264 */
Chris@16 265 first_kday_of_month(day_of_week_type dow, month_type m) :
Chris@16 266 month_(m),
Chris@16 267 dow_(dow)
Chris@16 268 {}
Chris@16 269 //! Return a concrete date when provided with a year specific year.
Chris@16 270 date_type get_date(year_type year) const
Chris@16 271 {
Chris@16 272 date_type d(year, month_,1);
Chris@16 273 duration_type one_day(1);
Chris@16 274 while (dow_ != d.day_of_week()) {
Chris@16 275 d = d + one_day;
Chris@16 276 }
Chris@16 277 return d;
Chris@16 278 }
Chris@16 279 // added for streaming
Chris@16 280 month_type month() const
Chris@16 281 {
Chris@16 282 return month_;
Chris@16 283 }
Chris@16 284 day_of_week_type day_of_week() const
Chris@16 285 {
Chris@16 286 return dow_;
Chris@16 287 }
Chris@16 288 //! Returns string suitable for use in POSIX time zone string
Chris@16 289 /*! Returns a string formatted as "M4.1.0" ==> 1st Sunday in April. */
Chris@16 290 virtual std::string to_string() const
Chris@16 291 {
Chris@16 292 std::ostringstream ss;
Chris@16 293 ss << 'M'
Chris@16 294 << static_cast<int>(month_) << '.'
Chris@16 295 << 1 << '.'
Chris@16 296 << static_cast<int>(dow_);
Chris@16 297 return ss.str();
Chris@16 298 }
Chris@16 299 private:
Chris@16 300 month_type month_;
Chris@16 301 day_of_week_type dow_;
Chris@16 302 };
Chris@16 303
Chris@16 304
Chris@16 305
Chris@16 306 //! Calculate something like Last Sunday of January
Chris@16 307 /*! Useful generator functor for finding holidays and daylight savings
Chris@16 308 * Get the last day of the month and then calculate the difference
Chris@16 309 * to the last previous day.
Chris@16 310 * @param date_type A date class that exports day_of_week, month_type, etc.
Chris@16 311 * \ingroup date_alg
Chris@16 312 */
Chris@16 313 template<class date_type>
Chris@16 314 class last_kday_of_month : public year_based_generator<date_type>
Chris@16 315 {
Chris@16 316 public:
Chris@16 317 typedef typename date_type::calendar_type calendar_type;
Chris@16 318 typedef typename calendar_type::day_of_week_type day_of_week_type;
Chris@16 319 typedef typename calendar_type::month_type month_type;
Chris@16 320 typedef typename calendar_type::year_type year_type;
Chris@16 321 typedef typename date_type::duration_type duration_type;
Chris@16 322 //!Specify the date spec like last 'Sunday' in 'April' spec
Chris@16 323 /*!@param dow The day of week, eg: Sunday, Monday, etc
Chris@16 324 * @param m The month of the year, eg: Jan, Feb, Mar, etc
Chris@16 325 */
Chris@16 326 last_kday_of_month(day_of_week_type dow, month_type m) :
Chris@16 327 month_(m),
Chris@16 328 dow_(dow)
Chris@16 329 {}
Chris@16 330 //! Return a concrete date when provided with a year specific year.
Chris@16 331 date_type get_date(year_type year) const
Chris@16 332 {
Chris@16 333 date_type d(year, month_, calendar_type::end_of_month_day(year,month_));
Chris@16 334 duration_type one_day(1);
Chris@16 335 while (dow_ != d.day_of_week()) {
Chris@16 336 d = d - one_day;
Chris@16 337 }
Chris@16 338 return d;
Chris@16 339 }
Chris@16 340 // added for streaming
Chris@16 341 month_type month() const
Chris@16 342 {
Chris@16 343 return month_;
Chris@16 344 }
Chris@16 345 day_of_week_type day_of_week() const
Chris@16 346 {
Chris@16 347 return dow_;
Chris@16 348 }
Chris@16 349 //! Returns string suitable for use in POSIX time zone string
Chris@16 350 /*! Returns a string formatted as "M4.5.0" ==> last Sunday in April. */
Chris@16 351 virtual std::string to_string() const
Chris@16 352 {
Chris@16 353 std::ostringstream ss;
Chris@16 354 ss << 'M'
Chris@16 355 << static_cast<int>(month_) << '.'
Chris@16 356 << 5 << '.'
Chris@16 357 << static_cast<int>(dow_);
Chris@16 358 return ss.str();
Chris@16 359 }
Chris@16 360 private:
Chris@16 361 month_type month_;
Chris@16 362 day_of_week_type dow_;
Chris@16 363 };
Chris@16 364
Chris@16 365
Chris@16 366 //! Calculate something like "First Sunday after Jan 1,2002
Chris@16 367 /*! Date generator that takes a date and finds kday after
Chris@16 368 *@code
Chris@16 369 typedef boost::date_time::first_kday_after<date> firstkdayafter;
Chris@16 370 firstkdayafter fkaf(Monday);
Chris@16 371 fkaf.get_date(date(2002,Feb,1));
Chris@16 372 @endcode
Chris@16 373 * \ingroup date_alg
Chris@16 374 */
Chris@16 375 template<class date_type>
Chris@16 376 class first_kday_after
Chris@16 377 {
Chris@16 378 public:
Chris@16 379 typedef typename date_type::calendar_type calendar_type;
Chris@16 380 typedef typename calendar_type::day_of_week_type day_of_week_type;
Chris@16 381 typedef typename date_type::duration_type duration_type;
Chris@16 382 first_kday_after(day_of_week_type dow) :
Chris@16 383 dow_(dow)
Chris@16 384 {}
Chris@16 385 //! Return next kday given.
Chris@16 386 date_type get_date(date_type start_day) const
Chris@16 387 {
Chris@16 388 duration_type one_day(1);
Chris@16 389 date_type d = start_day + one_day;
Chris@16 390 while (dow_ != d.day_of_week()) {
Chris@16 391 d = d + one_day;
Chris@16 392 }
Chris@16 393 return d;
Chris@16 394 }
Chris@16 395 // added for streaming
Chris@16 396 day_of_week_type day_of_week() const
Chris@16 397 {
Chris@16 398 return dow_;
Chris@16 399 }
Chris@16 400 private:
Chris@16 401 day_of_week_type dow_;
Chris@16 402 };
Chris@16 403
Chris@16 404 //! Calculate something like "First Sunday before Jan 1,2002
Chris@16 405 /*! Date generator that takes a date and finds kday after
Chris@16 406 *@code
Chris@16 407 typedef boost::date_time::first_kday_before<date> firstkdaybefore;
Chris@16 408 firstkdaybefore fkbf(Monday);
Chris@16 409 fkbf.get_date(date(2002,Feb,1));
Chris@16 410 @endcode
Chris@16 411 * \ingroup date_alg
Chris@16 412 */
Chris@16 413 template<class date_type>
Chris@16 414 class first_kday_before
Chris@16 415 {
Chris@16 416 public:
Chris@16 417 typedef typename date_type::calendar_type calendar_type;
Chris@16 418 typedef typename calendar_type::day_of_week_type day_of_week_type;
Chris@16 419 typedef typename date_type::duration_type duration_type;
Chris@16 420 first_kday_before(day_of_week_type dow) :
Chris@16 421 dow_(dow)
Chris@16 422 {}
Chris@16 423 //! Return next kday given.
Chris@16 424 date_type get_date(date_type start_day) const
Chris@16 425 {
Chris@16 426 duration_type one_day(1);
Chris@16 427 date_type d = start_day - one_day;
Chris@16 428 while (dow_ != d.day_of_week()) {
Chris@16 429 d = d - one_day;
Chris@16 430 }
Chris@16 431 return d;
Chris@16 432 }
Chris@16 433 // added for streaming
Chris@16 434 day_of_week_type day_of_week() const
Chris@16 435 {
Chris@16 436 return dow_;
Chris@16 437 }
Chris@16 438 private:
Chris@16 439 day_of_week_type dow_;
Chris@16 440 };
Chris@16 441
Chris@16 442 //! Calculates the number of days until the next weekday
Chris@16 443 /*! Calculates the number of days until the next weekday.
Chris@16 444 * If the date given falls on a Sunday and the given weekday
Chris@16 445 * is Tuesday the result will be 2 days */
Chris@16 446 template<typename date_type, class weekday_type>
Chris@16 447 inline
Chris@16 448 typename date_type::duration_type days_until_weekday(const date_type& d, const weekday_type& wd)
Chris@16 449 {
Chris@16 450 typedef typename date_type::duration_type duration_type;
Chris@16 451 duration_type wks(0);
Chris@16 452 duration_type dd(wd.as_number() - d.day_of_week().as_number());
Chris@16 453 if(dd.is_negative()){
Chris@16 454 wks = duration_type(7);
Chris@16 455 }
Chris@16 456 return dd + wks;
Chris@16 457 }
Chris@16 458
Chris@16 459 //! Calculates the number of days since the previous weekday
Chris@16 460 /*! Calculates the number of days since the previous weekday
Chris@16 461 * If the date given falls on a Sunday and the given weekday
Chris@16 462 * is Tuesday the result will be 5 days. The answer will be a positive
Chris@16 463 * number because Tuesday is 5 days before Sunday, not -5 days before. */
Chris@16 464 template<typename date_type, class weekday_type>
Chris@16 465 inline
Chris@16 466 typename date_type::duration_type days_before_weekday(const date_type& d, const weekday_type& wd)
Chris@16 467 {
Chris@16 468 typedef typename date_type::duration_type duration_type;
Chris@16 469 duration_type wks(0);
Chris@16 470 duration_type dd(wd.as_number() - d.day_of_week().as_number());
Chris@16 471 if(dd.days() > 0){
Chris@16 472 wks = duration_type(7);
Chris@16 473 }
Chris@16 474 // we want a number of days, not an offset. The value returned must
Chris@16 475 // be zero or larger.
Chris@16 476 return (-dd + wks);
Chris@16 477 }
Chris@16 478
Chris@16 479 //! Generates a date object representing the date of the following weekday from the given date
Chris@16 480 /*! Generates a date object representing the date of the following
Chris@16 481 * weekday from the given date. If the date given is 2004-May-9
Chris@16 482 * (a Sunday) and the given weekday is Tuesday then the resulting date
Chris@16 483 * will be 2004-May-11. */
Chris@16 484 template<class date_type, class weekday_type>
Chris@16 485 inline
Chris@16 486 date_type next_weekday(const date_type& d, const weekday_type& wd)
Chris@16 487 {
Chris@16 488 return d + days_until_weekday(d, wd);
Chris@16 489 }
Chris@16 490
Chris@16 491 //! Generates a date object representing the date of the previous weekday from the given date
Chris@16 492 /*! Generates a date object representing the date of the previous
Chris@16 493 * weekday from the given date. If the date given is 2004-May-9
Chris@16 494 * (a Sunday) and the given weekday is Tuesday then the resulting date
Chris@16 495 * will be 2004-May-4. */
Chris@16 496 template<class date_type, class weekday_type>
Chris@16 497 inline
Chris@16 498 date_type previous_weekday(const date_type& d, const weekday_type& wd)
Chris@16 499 {
Chris@16 500 return d - days_before_weekday(d, wd);
Chris@16 501 }
Chris@16 502
Chris@16 503 } } //namespace date_time
Chris@16 504
Chris@16 505
Chris@16 506
Chris@16 507
Chris@16 508 #endif
Chris@16 509