ex_find_last_day_of_months.xml 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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-2005 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.end_of_month_day">
  9. <title>Last Day of the Months</title>
  10. <para>
  11. Example that gets a month and a year from the user and finds the last day of each remaining month of that year.
  12. </para>
  13. <programlisting>
  14. <![CDATA[
  15. /* Simple program that finds the last day of the given month,
  16. * then displays the last day of every month left in the given year.
  17. */
  18. #include "boost/date_time/gregorian/gregorian.hpp"
  19. #include <iostream>
  20. int
  21. main()
  22. {
  23. using namespace boost::gregorian;
  24. greg_year year(1400);
  25. greg_month month(1);
  26. // get a month and a year from the user
  27. try {
  28. int y, m;
  29. std::cout << " Enter Year(ex: 2002): ";
  30. std::cin >> y;
  31. year = greg_year(y);
  32. std::cout << " Enter Month(1..12): ";
  33. std::cin >> m;
  34. month = greg_month(m);
  35. }
  36. catch(bad_year by) {
  37. std::cout << "Invalid Year Entered: " << by.what() << '\n'
  38. << "Using minimum values for month and year." << std::endl;
  39. }
  40. catch(bad_month bm) {
  41. std::cout << "Invalid Month Entered" << bm.what() << '\n'
  42. << "Using minimum value for month. " << std::endl;
  43. }
  44. date start_of_next_year(year+1, Jan, 1);
  45. date d(year, month, 1);
  46. // add another month to d until we enter the next year.
  47. while (d < start_of_next_year){
  48. std::cout << to_simple_string(d.end_of_month()) << std::endl;
  49. d += months(1);
  50. }
  51. return 0;
  52. }
  53. ]]>
  54. </programlisting>
  55. </section>