# 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 responsible for representing a length of time. A duration can be either positive or negative. The general time_duration class provides a constructor that takes a count of the number of hours, minutes, seconds, and fractional seconds count as shown in the code fragment below. The resolution of the time_duration is configureable at compile time. See Build-Compiler Information for more information.

      using namespace boost::posix_time;

      time_duration td(1,2,3,4); //01:02:03.000000004 when resolution is nano seconds
      time_duration td(1,2,3,4); //01:02:03.000004 when resolution is micro seconds

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::posix_time;

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

Note that the existence of the higher resolution classes (eg: nanosec) depends on the installation of the library. See Build-Compiler Information for more information.

## 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 a duration from the counts | time_duration td(1,2,3,9); //1 hr 2 min 3 sec 9 nanoseconds
time_duration(special_values) | Construct a time duration with a special value such as not_a_date_time, pos_infin, or neg_infin. | time_duration td(not_a_date_time); //invalid value

## 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);
milliseconds(long) | Number of milliseconds. | time_duration td = milliseconds(3);
microseconds(long) | Number of microseconds. | time_duration td = microseconds(3);
nanoseconds(long) | Number of nanoseconds. | time_duration td = nanoseconds(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 normalized hours. | time_duration td(1,2,3); td.hours() --> 1
long minutes() const | Get the number of minutes normalized (0..59). | time_duration td(1,2,3); td.minutes() --> 2
long seconds() const | Get the normalized number of second (0..59). | time_duration td(1,2,3); td.seconds() --> 3
bool is_special() const | Returns true if the value of the duration is a special value, otherwise false. | time_duration td(neg_infin); td.is_special() --> true
bool is_neg_infinity() const | Returns true if the value of the duration is negative infinity, otherwise false. | time_duration td(neg_inifin); td.is_neg_infinity() --> true
bool is_pos_infinity() const | Returns true if the value of the duration is positive infinity, otherwise false. | hours h(5); h.is_pos_infinity() --> false
long total_seconds() const | Get the total number of seconds truncating any fractional seconds. | time_duration td(1,2,3,10); td.total_seconds() --> (1*3600) + (2*60) + 3 == 3723
long total_microseconds() const | Return the total number of microseconds in the duration truncating any fractional lower resolution (eg: nano second counts). | seconds(1).total_microseconds() == 1000000
long total_milliseconds() const | Return the total number of milliseconds in the duration truncating any fractional lower resolution (eg: micro or nano second counts). | seconds(1).total_milliseconds() == 1000
long total_nanoseconds() const | Return the total number of nanoseconds in the duration. | seconds(1).total_nanoseconds() == 1000000000
long fractional_seconds() const | Get the fractional seconds count. Note that the results of this method vary based on the resolution of template instantiation for the time duration (Normally either a nano-second or millisecond count). | 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
boost::int64_t ticks() | Return the raw count of the duration type. | time_duration td(0,0,0, 1000); td.ticks() --> 1000
static boost::int64_t ticks_per_second() | Return the number of ticks held internally to represent a second, which is driven by the resolution settings. For nano second resolution the value 1000000000. | time_duration::ticks_per_second() == 1000000000 //nano second resolution
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 | td1 == td2, 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);
time_duration operator-() const | Invert sign. | time_duration td1(hours(1)); -td1; //equal -1 hour
time_duration operator/(int) const | Divide the length of a duration by an integer value. Discards any remainder. | hours(3)/2 == time_duration(1,30,0); nanosec(3)/2 == nanosec(1)
time_duration operator*(int) const | Multiply the length of a duration by an integer value. | hours(3)*2 == hours(6);