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