Files
date_time/doc/class_time_duration.html
Jeff Garland 9d6fedb8e5 fix spelling
[SVN r19496]
2003-08-09 18:22:31 +00:00

205 lines
7.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>posix_time::time_duration Documentation</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head>
<body>
<a href="../../../index.htm">
<img align="left" src="../../../c++boost.gif" alt="C++ Boost">
</a>
<h1>posix_time::time_duration</h1>
<p>&nbsp;<p>
<hr>
<p>
<p>
<a href="index.html">Overall Index</a> --
<a href="gregorian.html">Gregorian Index</a> --
<a href="posix_time.html">Posix Time Index</a>
<p>
<font class="bold">Time Duration Documentation</font>
<p>
<a href="class_time_duration.html#header">Header</a> --
<a href="class_time_duration.html#construct">Construction</a> --
<a href="class_time_duration.html#countbasedconstruct">Count Based Construction</a> --
<a href="class_time_duration.html#constructfromstring">Construct from String</a> --
<a href="class_time_duration.html#accessors">Accessors</a> --
<a href="class_time_duration.html#conversiontostring">Conversion To String</a> --
<a href="class_time_duration.html#operators">Operators</a>
<p>
<h2><a name="intro">Introduction</a></h2>
<p>
The class boost::posix_time::time_duration the base type responsible for representing a length of time. A duration can be either positive or negative.
<p>
Several small helper classes that derive from a base time_duration, as
shown below, to adjust for different resolutions. These classes can
shorten code and make the intent clearer.
<p>
<img align="middle" src="time_duration_inherit.png" alt="inherit">
<p>
As an example:
<div class="fragment"><pre>
<font class="keyword">using namespace</font> boost::gregorian;
<font class="keyword">using namespace</font> boost::posix_time;
time_duration td = hours(1) + seconds(10); <font class="comment">//01:00:01</font>
td = hours(1) + nanosec(5); <font class="comment">//01:00:00.000000005</font>
</pre></div>
<p>
Note that the existence of the higher resolution classes depends on
the installation of the library. See
<a href="BuildInfo.html">Build-Compiler Information</a> for more
information.
<p>
<h2><a name="header">Header</a></h2>
<pre>
#include "boost/date_time/posix_time/posix_time.hpp" //include all types plus i/o
or
#include "boost/date_time/posix_time/posix_time/posix_time_types.hpp" //no i/o just types
</pre>
<p>
<h2><a name="construct">Construction</a></h2>
<p>
<table border=1 cellspacing=3 cellpadding=3>
<tr><td><b>Syntax</b></td><td><b>Description</b></td><td><b>Example</b></td></tr>
<tr><td>time_duration(hours,minutes,seconds,fractional_seconds)</td>
<td>Construct ad duration from the counts</td>
<td>time_duration td(1,2,3,9); //1 hr 2 min 3 sec 9 nanoseconds</td></tr>
</table>
<p>
<h2><a name="countbasedconstruct">Construction By Count</a></h2>
<table border=1 cellspacing=3 cellpadding=3>
<tr><td><b>Syntax</b></td><td><b>Description</b></td><td><b>Example</b></td></tr>
<tr><td>hours(long)</td>
<td>Number of hours</td>
<td>time_duration td = hours(3);</td></tr>
<tr><td>minutes(long)</td>
<td>Number of minutes</td>
<td>time_duration td = minutes(3);</td></tr>
<tr><td>seconds(long)</td>
<td>Number of seconds</td>
<td>time_duration td = seconds(3);</td></tr>
<tr><td>millisec(long)</td>
<td>Number of millisec</td>
<td>time_duration td = millisec(3);</td></tr>
<tr><td>nanosec(long)</td>
<td>Number of nanosec</td>
<td>time_duration td = nanosec(3);</td></tr>
</table>
<p>
<h2><a name="constructfromstring">Construction From String</a></h2>
<p>
<table border=1 cellspacing=3 cellpadding=3>
<tr><td><b>Syntax</b></td><td><b>Description</b></td><td><b>Example</b></td></tr>
<tr><td>time_duration <font class="func">duration_from_string</font>(const std::string&)</td>
<td>From delimited string.</td>
<td>std::string ts("23:59:59.000");
<br>
time_duraton td(duration_from_string(ts))</td></tr>
</table>
<p>
<h2><a name="accessors">Accessors</a></h2>
<p>
<table border=1 cellspacing=3 cellpadding=3>
<tr><td><b>Syntax</b></td><td><b>Description</b></td><td><b>Example</b></td></tr>
<tr><td>long <font class="func">hours</font>() const</td>
<td>Get the number of hours.</td>
<td>time_duration td(1,2,3); td.hours() --> 1</td></tr>
<tr><td>long <font class="func">minutes</font>() const</td>
<td>Get the number of minutes normalized.</td>
<td>time_duration td(1,2,3); td.minutes() --> 2</td></tr>
<tr><td>long <font class="func">seconds</font>() const</td>
<td>Get the number of seconds.</td>
<td>time_duration td(1,2,3); td.seconds() --> 3</td></tr>
<tr><td>long <font class="func">fractional_seconds</font>() const</td>
<td>Get the number of fractional seconds.</td>
<td>time_duration td(1,2,3, 1000); td.fractional_seconds() --> 1000</td></tr>
<tr><td>bool <font class="func">is_negative</font>() const</td>
<td>True if duration is negative.</td>
<td>time_duration td(-1,0,0); td.is_negative() --> true</td></tr>
<tr><td>time_duration <font class="func">invert_sign</font>() const</td>
<td>Generate a new duration with the sign inverted/</td>
<td>time_duration td(-1,0,0); td.invert_sign() --> 01:00:00</td></tr>
<tr><td>static boost::date_time::time_resolutions <font class="func">resolution</font>()</td>
<td>Describes the resolution capability of the time_duration class.</td>
<td>time_duration::resolution() --> nano</td></tr>
<tr><td>boost::int64_t <font class="func">ticks</font>()</td>
<td>Return the raw count of the duration type.</td>
<td>time_duration td(0,0,0, 1000); td.ticks() --> 1000</td></tr>
<tr><td>static time_duration <font class="func">unit</font>()</td>
<td>Return smallest possible unit of duration type (1 nanosecond).</td>
<td>time_duration::unit() --> time_duration(0,0,0,1)</td></tr>
</table>
<p>
<p>
<h2><a name="conversiontostring">Conversion To String</a></h2>
<p>
<table border=1 cellspacing=3 cellpadding=3>
<tr><td><b>Syntax</b></td><td><b>Description</b></td><td><b>Example</b></td></tr>
<tr><td>std::string <font class="func">to_simple_string</font>(time_duration)</td>
<td>To HH:MM:SS.fffffffff were fff is fractional seconds that are only included if non-zero.</td>
<td>10:00:01.123456789</td></tr>
<tr><td>std::string <font class="func">to_iso_string</font>(time_duration)</td>
<td>Convert to form HHMMSS,fffffffff.</td>
<td>100001,123456789</td></tr>
</table>
<p>
<h2><a name="operators">Operators</a></h2>
<p>
<table border=1 cellspacing=3 cellpadding=3>
<tr><td><b>Syntax</b></td><td><b>Description</b></td><td><b>Example</b></td></tr>
<tr><td>operator==, operator!=,<br>
operator&gt;, operator&lt; <br>
operator&gt;=, operator&lt;=</td>
<td>A full complement of comparison operators</td>
<td>dd1 == dd2, etc</td></tr>
<tr><td>time_duration operator+(time_duration) const</td>
<td>Add durations.</td>
<td>time_duration td1(hours(1)+minutes(2));
<br>time_duration td2(seconds(10));
<br>time_duration td3 = td1 + td2;</td></tr>
<tr><td>time_duration operator-(time_duration) const</td>
<td>Subtract durations.</td>
<td>time_duration td1(hours(1)+nanosec(2)); <br>
time_duration td2 = td1 - minutes(1);</td></tr>
</table>
<p>
<hr>
<address><small>
<!-- hhmts start -->
Last modified: Thu Feb 13 09:19:15 MST 2003
<!-- hhmts end -->
by <a href="mailto:jeff&#64;crystalclearsoftware.com">Jeff Garland</a> &copy; 2000-2002
</small></address>
</body>
</html>