find_last_day_of_months.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Simple program that finds the last day of the given month,
  2. * then displays the last day of every month left in the given year.
  3. */
  4. #include "boost/date_time/gregorian/gregorian.hpp"
  5. #include <iostream>
  6. int
  7. main()
  8. {
  9. using namespace boost::gregorian;
  10. greg_year year(1400);
  11. greg_month month(1);
  12. // get a month and a year from the user
  13. try {
  14. greg_year::value_type y;
  15. greg_month::value_type m;
  16. std::cout << " Enter Year(ex: 2002): ";
  17. std::cin >> y;
  18. year = greg_year(y);
  19. std::cout << " Enter Month(1..12): ";
  20. std::cin >> m;
  21. month = greg_month(m);
  22. }
  23. catch(const bad_year& by) {
  24. std::cout << "Invalid Year Entered: " << by.what() << '\n'
  25. << "Using minimum values for month and year." << std::endl;
  26. }
  27. catch(const bad_month& bm) {
  28. std::cout << "Invalid Month Entered" << bm.what() << '\n'
  29. << "Using minimum value for month. " << std::endl;
  30. }
  31. date start_of_next_year(year+1, Jan, 1);
  32. date d(year, month, 1);
  33. // add another month to d until we enter the next year.
  34. while (d < start_of_next_year){
  35. std::cout << to_simple_string(d.end_of_month()) << std::endl;
  36. d += months(1);
  37. }
  38. return 0;
  39. }
  40. /* Copyright 2001-2005: CrystalClear Software, Inc
  41. * http://www.crystalclearsoftware.com
  42. *
  43. * Subject to the Boost Software License, Version 1.0.
  44. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  45. */