ex_days_between_new_years.xml 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
  3. "../../../tools/boostbook/dtd/boostbook.dtd">
  4. <!-- Copyright (c) 2001-2004 CrystalClear Software, Inc.
  5. Subject to the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <section id="date_time.examples.days_between_new_year">
  9. <title>Days Between New Years</title>
  10. <para>
  11. Calculate the number of days till new years
  12. </para>
  13. <programlisting>
  14. <![CDATA[
  15. /* Provides a simple example of using a date_generator, and simple
  16. * mathematical operatorations, to calculate the days since
  17. * New Years day of this year, and days until next New Years day.
  18. *
  19. * Expected results:
  20. * Adding together both durations will produce 366 (365 in a leap year).
  21. */
  22. #include <iostream>
  23. #include "boost/date_time/gregorian/gregorian.hpp"
  24. int
  25. main()
  26. {
  27. using namespace boost::gregorian;
  28. date today = day_clock::local_day();
  29. partial_date new_years_day(1,Jan);
  30. //Subtract two dates to get a duration
  31. days days_since_year_start = today - new_years_day.get_date(today.year());
  32. std::cout << "Days since Jan 1: " << days_since_year_start.days()
  33. << std::endl;
  34. days days_until_year_start = new_years_day.get_date(today.year()+1) - today;
  35. std::cout << "Days until next Jan 1: " << days_until_year_start.days()
  36. << std::endl;
  37. return 0;
  38. };
  39. ]]>
  40. </programlisting>
  41. </section>