mirror of
https://github.com/boostorg/date_time.git
synced 2026-02-23 15:42:20 +00:00
76 lines
2.5 KiB
XML
76 lines
2.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
|
|
"../../../tools/boostbook/dtd/boostbook.dtd">
|
|
|
|
<!-- Copyright (c) 2001-2005 CrystalClear Software, Inc.
|
|
Subject to the Boost Software License, Version 1.0.
|
|
(See accompanying file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
|
-->
|
|
|
|
<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 = weekstart + week(1);
|
|
date d2 = d1 + days(5);
|
|
date today = day_clock::local_day();
|
|
if (d2 >= today) {} //date comparison operators
|
|
|
|
date_period thisWeek(d1,d2);
|
|
if (thisWeek.contains(today)) {}//do something
|
|
|
|
//iterate and print the week
|
|
day_iterator itr(weekstart);
|
|
while (itr <= weekend) {
|
|
std::cout << (*itr) << std::endl;
|
|
++itr;
|
|
}
|
|
//input streaming
|
|
std::stringstream ss("2004-Jan-1");
|
|
ss >> d3;
|
|
|
|
//date generator functions
|
|
date d5 = next_weekday(d4, Sunday); //calculate sunday following d4
|
|
|
|
//define a shorthand for the
|
|
// nth_day_of_the_week_in_month function object
|
|
typedef nth_day_of_the_week_in_month nth_dow;
|
|
//US labor day is first Monday in Sept
|
|
nth_dow labor_day(nth_dow::first,Monday, Sep);
|
|
//calculate a specific date from functor
|
|
date d6 = labor_day.get_date(2004);
|
|
</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
|
|
|
|
//input streaming
|
|
std::stringstream ss("2004-Jan-1 05:21:33.20");
|
|
ss >> t2;
|
|
|
|
//starting at current time iterator adds by one hour
|
|
time_iterator titr(now,hours(1));
|
|
for (; titr < tommorrow_start; ++titr) {
|
|
std::cout << (*titr) << std::endl;
|
|
}
|
|
</programlisting>
|
|
</para>
|
|
|
|
</section>
|