Files
date_time/doc/exp-print_holidays.html
2003-02-10 13:23:54 +00:00

111 lines
4.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Example Documentation</title><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#ffffff"> <!-- Generated by Doxygen 1.2.17 -->
<hr>
<h1>print_holidays.cpp</h1>
This is an example of using functors to define a holiday schedule
<p></p>
<div class="fragment" id="DIV1"><pre><font class="comment">/*generate a set of dates using a collections of date generators</font>
<font class="comment">Output looks like:</font>
<font class="comment">Enter Year: 2002</font>
<font class="comment">2002-Jan-01 [Tue]</font>
<font class="comment">2002-Jan-21 [Mon]</font>
<font class="comment">2002-Feb-12 [Tue]</font>
<font class="comment">2002-Jul-04 [Thu]</font>
<font class="comment">2002-Sep-02 [Mon]</font>
<font class="comment">2002-Nov-28 [Thu]</font>
<font class="comment">2002-Dec-25 [Wed]</font>
<font class="comment">Number Holidays: 7</font>
<font class="comment">*/</font>
<font class="preprocessor">#include "boost/date_time/gregorian/gregorian.hpp"</font>
<font class="preprocessor">#include &lt;algorithm&gt;</font>
<font class="preprocessor">#include &lt;functional&gt;</font>
<font class="preprocessor">#include &lt;vector&gt;</font>
<font class="preprocessor">#include &lt;iostream&gt;</font>
<font class="preprocessor">#include &lt;set&gt;</font>
<font class="keywordtype">void</font>
print_date(boost::gregorian::date d)
{
std::cout &lt;&lt; d &lt;&lt; <font class="stringliteral">" ["</font> &lt;&lt; d.day_of_week() &lt;&lt; <font class="stringliteral">"]\n"</font>;
}
<font class="keywordtype">int</font>
main() {
std::cout &lt;&lt; <font class="stringliteral">"Enter Year: "</font>;
<font class="keywordtype">int</font> year;
std::cin &gt;&gt; year;
<font class="keyword">using</font> <font class="keyword">namespace </font>boost::gregorian;
<font class="comment">//define a collection of holidays fixed by month and day</font>
std::vector&lt;partial_date&gt; holidays;
holidays.push_back(partial_date(1, Jan)); <font class="comment">//Western New Year</font>
holidays.push_back(partial_date(4, Jul)); <font class="comment">//US Independence Day</font>
holidays.push_back(partial_date(25, Dec));<font class="comment">//Christmas day</font>
<font class="comment">//define a shorthand for the nkday function object</font>
typedef boost::date_time::nth_kday_of_month&lt;date&gt; nkday;
<font class="comment">//define a collection of nth kday holidays</font>
std::vector&lt;nkday&gt; more_holidays;
more_holidays.push_back(nkday(nkday::first, Monday, Sep)); <font class="comment">//US labor day</font>
more_holidays.push_back(nkday(nkday::third, Monday, Jan)); <font class="comment">//MLK Day</font>
more_holidays.push_back(nkday(nkday::second, Tuesday, Feb)); <font class="comment">//Pres day</font>
more_holidays.push_back(nkday(nkday::fourth, Thursday, Nov)); <font class="comment">//Thanksgiving</font>
typedef std::set&lt;date&gt; date_set;
date_set all_holidays;
#if (defined(BOOST_MSVC) &amp;&amp; (_MSC_VER &lt;= 1200)) <font class="comment">// 1200 == VC++ 6.0</font>
std::cout &lt;&lt; "Sorry, this example temporarily disabled on VC 6.\n"
&lt;&lt; "The std::transform isn't accepted by the compiler\n"
&lt;&lt; "So if you hack up the example without std::transform\n"
&lt;&lt; "it should work\n"
&lt;&lt; std::endl;
#else
<font class="comment">//generate a set of concrete dates from the 'partial_date' holidays</font>
<font class="comment">//by calling the get_date functions</font>
std::transform(holidays.begin(), holidays.end(),
std::insert_iterator&lt;date_set&gt;(all_holidays, all_holidays.begin()),
std::bind2nd(std::mem_fun_ref(&amp;partial_date::get_date),
year));
<font class="comment">//Now add in the 'floating' holidays </font>
std::transform(more_holidays.begin(), more_holidays.end(),
std::insert_iterator&lt;date_set&gt;(all_holidays, all_holidays.begin()),
std::bind2nd(std::mem_fun_ref(&amp;nkday::get_date),
year));
<font class="comment">//print the holidays to the screen</font>
std::for_each(all_holidays.begin(), all_holidays.end(), print_date);
std::cout &lt;&lt; "Number Holidays: " &lt;&lt; all_holidays.size() &lt;&lt; std::endl;
#endif
return 0;
}
</pre>
</div>
<hr>
<address><small> Generated Wed Aug 21 16:54:33 2002 by Doxygen for CrystalClear
Software © 2000-2002</small></address>
</body>
</html>