ex_print_month.xml 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.print_month">
  9. <title>Print Month</title>
  10. <para>
  11. Simple utility to print out days of the month with the days of a month. Demontstrates date iteration (date_time::date_itr).
  12. </para>
  13. <programlisting>
  14. <![CDATA[
  15. /* This example prints all the dates in a month. It demonstrates
  16. * the use of iterators as well as functions of the gregorian_calendar
  17. *
  18. * Output:
  19. * Enter Year: 2002
  20. * Enter Month(1..12): 2
  21. * 2002-Feb-01 [Fri]
  22. * 2002-Feb-02 [Sat]
  23. * 2002-Feb-03 [Sun]
  24. * 2002-Feb-04 [Mon]
  25. * 2002-Feb-05 [Tue]
  26. * 2002-Feb-06 [Wed]
  27. * 2002-Feb-07 [Thu]
  28. */
  29. #include "boost/date_time/gregorian/gregorian.hpp"
  30. #include <iostream>
  31. int
  32. main()
  33. {
  34. std::cout << "Enter Year: ";
  35. int year, month;
  36. std::cin >> year;
  37. std::cout << "Enter Month(1..12): ";
  38. std::cin >> month;
  39. using namespace boost::gregorian;
  40. try {
  41. //Use the calendar to get the last day of the month
  42. int eom_day = gregorian_calendar::end_of_month_day(year,month);
  43. date endOfMonth(year,month,eom_day);
  44. //construct an iterator starting with firt day of the month
  45. day_iterator ditr(date(year,month,1));
  46. //loop thru the days and print each one
  47. for (; ditr <= endOfMonth; ++ditr) {
  48. #if defined(BOOST_DATE_TIME_NO_LOCALE)
  49. std::cout << to_simple_string(*ditr) << " ["
  50. #else
  51. std::cout << *ditr << " ["
  52. #endif
  53. << ditr->day_of_week() << "]"
  54. << std::endl;
  55. }
  56. }
  57. catch(std::exception& e) {
  58. std::cout << "Error bad date, check your entry: \n"
  59. << " Details: " << e.what() << std::endl;
  60. }
  61. return 0;
  62. }
  63. ]]>
  64. </programlisting>
  65. </section>