mirror of
https://github.com/boostorg/date_time.git
synced 2026-02-22 15:22:15 +00:00
53 lines
1.6 KiB
XML
53 lines
1.6 KiB
XML
<section id="date_time.examples.general_usage_examples">
|
|
<title>General Usage Examples</title>
|
|
|
|
<para>
|
|
The following provides some sample usage of dates.
|
|
See <link linkend="date_time.gregorian">Date Programming</link>
|
|
for more details.
|
|
|
|
<programlisting>
|
|
using namespace boost::gregorian;
|
|
date weekstart(2002,Feb,1);
|
|
date weekend (2002,Feb,7);
|
|
date_duration fiveDays(5);
|
|
date d3 = d1 + fiveDays;
|
|
date today = day_clock::local_day();
|
|
if (d3 >= today) {} //date comparison operators
|
|
|
|
date_period thisWeek(d1,d2);
|
|
if (thisWeek.contains(today)) {}//do something
|
|
|
|
//iterate and print the week
|
|
day_iterator itr(weekstart);
|
|
for (; itr <= weekend; ++itr) {
|
|
std::cout << (*itr) << std::endl;
|
|
}
|
|
</programlisting>
|
|
|
|
The following provides some example code using times.
|
|
See <link linkend="date_time.posix_time">Time Programming</link>
|
|
for more details.
|
|
|
|
<programlisting>
|
|
use namespace boost::posix_time;
|
|
date d(2002,Feb,1); //an arbitrary date
|
|
ptime t1(d, hours(5)+nanosec(100)); //date + time of day offset
|
|
ptime t2 = t1 - minutes(4)+seconds(2);
|
|
ptime now = second_clock::local_time(); //use the clock
|
|
date today = now.date(); //Get the date part out of the time
|
|
date tommorrow = today + date_duration(1);
|
|
ptime tommorrow_start(tommorrow); //midnight
|
|
|
|
//starting at current time iterator adds by one hour
|
|
time_iterator titr(now,hours(1));
|
|
for (; titr < tommorrow_start; ++titr) {
|
|
std::cout << to_simple_string(*titr) << std::endl;
|
|
}
|
|
|
|
</programlisting>
|
|
</para>
|
|
|
|
<link linkend="top">top</link>
|
|
</section>
|