C++ Boost

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.

inherit

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

SyntaxDescriptionExample
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

SyntaxDescriptionExample
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 milliseconds. time_duration td = millisec(3);
microsec(long) Number of microseconds. time_duration td = microsec(3);
nanosec(long) Number of nanoseconds. time_duration td = nanosec(3);

Construction From String

SyntaxDescriptionExample
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

SyntaxDescriptionExample
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
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 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 boost::date_time::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

SyntaxDescriptionExample
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

SyntaxDescriptionExample
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);
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);


Last modified: Sun Aug 10 16:17:58 MST 2003 by Jeff Garland © 2000-2003