print_month.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* This example prints all the dates in a month. It demonstrates
  2. * the use of iterators as well as functions of the gregorian_calendar
  3. *
  4. * Output:
  5. * Enter Year: 2002
  6. * Enter Month(1..12): 2
  7. * 2002-Feb-01 [Fri]
  8. * 2002-Feb-02 [Sat]
  9. * 2002-Feb-03 [Sun]
  10. * 2002-Feb-04 [Mon]
  11. * 2002-Feb-05 [Tue]
  12. * 2002-Feb-06 [Wed]
  13. * 2002-Feb-07 [Thu]
  14. */
  15. #include "boost/date_time/gregorian/gregorian.hpp"
  16. #include <iostream>
  17. int
  18. main()
  19. {
  20. using namespace boost::gregorian;
  21. std::cout << "Enter Year: ";
  22. greg_year::value_type year;
  23. greg_month::value_type month;
  24. std::cin >> year;
  25. std::cout << "Enter Month(1..12): ";
  26. std::cin >> month;
  27. try {
  28. //Use the calendar to get the last day of the month
  29. greg_day::value_type eom_day = gregorian_calendar::end_of_month_day(year,month);
  30. date endOfMonth(year,month,eom_day);
  31. //construct an iterator starting with firt day of the month
  32. day_iterator ditr(date(year,month,1));
  33. //loop thru the days and print each one
  34. for (; ditr <= endOfMonth; ++ditr) {
  35. #if defined(BOOST_DATE_TIME_NO_LOCALE)
  36. std::cout << to_simple_string(*ditr) << " ["
  37. #else
  38. std::cout << *ditr << " ["
  39. #endif
  40. << ditr->day_of_week() << " week: "
  41. << ditr->week_number() << "]"
  42. << std::endl;
  43. }
  44. }
  45. catch(std::exception& e) {
  46. std::cout << "Error bad date, check your entry: \n"
  47. << " Details: " << e.what() << std::endl;
  48. }
  49. return 0;
  50. }
  51. /* Copyright 2001-2004: CrystalClear Software, Inc
  52. * http://www.crystalclearsoftware.com
  53. *
  54. * Subject to the Boost Software License, Version 1.0.
  55. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  56. */