# posix_time::time_duration

Overall Index -- Gregorian Index -- Posix Time Index

Time Duration Documentation

Header -- Construction -- Count Based Construction -- Construct from String -- Accessors -- Conversion To String -- Operators

## Introduction

The class boost::posix_time::time_duration the base type reponsible for representing a length of time. A duration can be either positive or negative.

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.

As an example:

      using namespace boost::gregorian;
      using namespace boost::posix_time;

      time_duration td = hours(1) + seconds(10); //01:00:01
      td = hours(1) + nanosec(5); //01:00:00.000000005

## Header

    #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

## Construction

Syntax | Description | Example
time_duration(hours,minutes,seconds,fractional_seconds) | Construct ad duration from the counts | time_duration td(1,2,3,9); //1 hr 2 min 3 sec 9 nanoseconds

## Construction By Count

Syntax | Description | Example
hours(long) | Number of hours | time_duration td = hours(3);
minutes(long) | Number of minutes | time_duration td = minutes(3);
seconds(long) | Number of seconds | time_duration td = seconds(3);
millisec(long) | Number of millisec | time_duration td = millisec(3);
nanosec(long) | Number of nanosec | time_duration td = nanosec(3);

## Construction From String

Syntax | Description | Example
time_duration duration_from_string(const std::string&) | From delimited string. | std::string ts("23:59:59.000"); time_duraton td(duration_from_string(ts))

## Accessors

Syntax | Description | Example
long hours() const | Get the number of hours. | time_duration td(1,2,3); td.hours() --> 1
long minutes() const | Get the number of minutes normalized. | time_duration td(1,2,3); td.minutes() --> 2
long seconds() const | Get the number of seconds. | time_duration td(1,2,3); td.hours() --> 3
long fractional_seconds() const | Get the number of fractional seconds. | time_duration td(1,2,3, 1000); td.fractional_seconds() --> 1000
bool is_negative() const | True if duration is negative. | time_duration td(-1,0,0); td.is_negative() --> true
time_duration invert_sign() const | Generate a new duration with the sign inverted/ | time_duration td(-1,0,0); td.invert_sign() --> 01:00:00
static gdtl::time_resolutions resolution() | Describes the resolution capability of the time_duration class. | time_duration::resolution() --> nano
boost::int64_t ticks() | Return the raw count of the duration type. | time_duration td(0,0,0, 1000); td.ticks() --> 1000
static time_duration unit() | Return smallest possible unit of duration type (1 nanosecond). | time_duration::unit() --> time_duration(0,0,0,1)

## Conversion To String

Syntax | Description | Example
std::string to_simple_string(time_duration) | To HH:MM:SS.fffffffff were fff is fractional seconds that are only included if non-zero. | 10:00:01.123456789
std::string to_iso_string(time_duration) | Convert to form HHMMSS,fffffffff. | 100001,123456789

## Operators

Syntax | Description | Example
operator==, operator!=, operator>, operator< operator>=, operator<= | A full complement of comparison operators | dd1 == dd2, etc
time_duration operator+(time_duration) const | Add durations. | time_duration td1(hours(1)+minutes(2)); time_duration td2(seconds(10)); time_duration td3 = td1 + td2;
time_duration operator-(time_duration) const | Subtract durations. | time_duration td1(hours(1)+nanosec(2)); time_duration td2 = td1 - minutes(1);