mirror of
https://github.com/boostorg/date_time.git
synced 2026-01-29 19:32:20 +00:00
111 lines
4.6 KiB
HTML
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 <algorithm></font>
|
|
<font class="preprocessor">#include <functional></font>
|
|
<font class="preprocessor">#include <vector></font>
|
|
<font class="preprocessor">#include <iostream></font>
|
|
<font class="preprocessor">#include <set></font>
|
|
|
|
|
|
<font class="keywordtype">void</font>
|
|
print_date(boost::gregorian::date d)
|
|
{
|
|
std::cout << d << <font class="stringliteral">" ["</font> << d.day_of_week() << <font class="stringliteral">"]\n"</font>;
|
|
}
|
|
|
|
|
|
<font class="keywordtype">int</font>
|
|
main() {
|
|
|
|
std::cout << <font class="stringliteral">"Enter Year: "</font>;
|
|
<font class="keywordtype">int</font> year;
|
|
std::cin >> 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<partial_date> 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<date> nkday;
|
|
<font class="comment">//define a collection of nth kday holidays</font>
|
|
std::vector<nkday> 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<date> date_set;
|
|
date_set all_holidays;
|
|
|
|
#if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) <font class="comment">// 1200 == VC++ 6.0</font>
|
|
std::cout << "Sorry, this example temporarily disabled on VC 6.\n"
|
|
<< "The std::transform isn't accepted by the compiler\n"
|
|
<< "So if you hack up the example without std::transform\n"
|
|
<< "it should work\n"
|
|
<< 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<date_set>(all_holidays, all_holidays.begin()),
|
|
std::bind2nd(std::mem_fun_ref(&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<date_set>(all_holidays, all_holidays.begin()),
|
|
std::bind2nd(std::mem_fun_ref(&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 << "Number Holidays: " << all_holidays.size() << 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>
|
|
|