time_period


An os_time_period represents a period of time with microsecond accuracy. Arithmetic operations on time periods are supported, as well as conversions to an integral number of days, hours, minutes, or seconds using one of the to_...() family of functions.

For backwards compatibility, you can construct a time period from a timeval (where available) or time_t . Similarly, you can convert a time period into a timeval or time_t using one of the time period's conversion operators.

The following example demonstrates some time period behaviors.

Example <ospace/time/examples/tperiod1.cpp>
#include <iostream>
#include <ospace/time.h>

void
main()
  {
  os_time_toolkit initialize;

  // 0 days, 1 hour, 0 minutes, 0 seconds, 0 microseconds.
  os_time_period one_hour( 0, 1, 0, 0, 0 );
  cout << "one hour is " << one_hour << endl;
  cout << "one hour as minutes is " << one_hour.to_minutes() << endl;

  os_time_period one_microsecond( 0, 1 );
  cout << "one microsecond is " << one_microsecond << endl;
  cout << "one hour > one microsecond = ";
  cout << (one_hour > one_microsecond) << endl; // Compare.

  os_time_period delta1 = (one_microsecond + one_hour); // Add.
  cout << "delta1 = one_microsecond + one_hour = " << delta1 << endl;

  os_time_period delta2 = (one_microsecond - one_hour); // Subtract.
  cout << "delta2 = one_microsecond - one_hour = " << delta2 << endl;
  cout << "delta1 + delta2 = " << (delta1 + delta2) << endl;
  }

one hour is 3600 seconds
one hour as minutes is 60
one microsecond is 0.000001 seconds
one hour > one microsecond = 1
delta1 = one_microsecond + one_hour = 3600.000001 seconds
delta2 = one_microsecond - one_hour = -3599.999999 seconds
delta1 + delta2 = 0.000002 seconds

To construct a negative time, make every argument to the constructor negative. The following example illustrates a negative time construction.

Example <ospace/time/examples/tperiod2.cpp>
#include <iostream>
#include <ospace/time.h>

void
main()
  {
  os_time_toolkit initialize;

  os_time_period plus( 10, 20 );
  os_time_period neg( -10, -20 );
  cout << plus << " + " << neg << " = " << (plus + neg) << endl;
  }

10.000020 seconds + -10.000020 seconds = 0 seconds

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.