Posix Time SystemIntroduction -- Usage Examples IntroductionDefines a non-adjusted time system with nano-second/micro-second resolution and stable calculation properties. The nano-second resolution option uses 96 bits of underlying storage for each ptime while the micro-second resolution uses 64 bits per ptime (see Build Options for details). This time system uses the Gregorian calendar to implement the date portion of the time representation. Usage Examples
Introduction --
Header --
Construction --
Construct from String --
Construct from Clock --
Construct using Conversion functions --
Accessors --
Conversion To String --
Operators --
Struct tm, time_t, and FILETIME Functions
IntroductionThe class boost::posix_time::ptime is the primary interface for time point manipulation. In general, the ptime class is immutable once constructed although it does allow assignment. Class ptime is dependent on gregorian::date for the interface to the date portion of a time point. Other techniques for creating times include time iterators. 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_types.hpp" //no i/o just types
Construction
Construct from String
Construct from Clock
Construct using Conversion Functions
Accessors
Conversion to String
Operators
Struct tm, time_t, and FILETIME FunctionsFunctions for converting posix_time objects to, and from,
Introduction --
Header --
Construction --
Count Based Construction --
Construct from String --
Accessors --
Conversion To String --
Operators --
Struct tm Functions
IntroductionThe 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 configure able 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) + nanoseconds(5); //01:00:00.000000005 Note that the existence of the higher resolution classes (eg: nanoseconds) depends on the installation of the library. See Build-Compiler Information for more information. Another way to handle this is to utilize the ticks_per_second() method of time_duration to write code that is portable no matter how the library is compiled. The general equation for calculating a resolution independent count is as follows: count*(time_duration_ticks_per_second / count_ticks_per_second) For example, let's suppose we want to construct using a count that represents tenths of a second. That is, each tick is 0.1 second. int number_of_tenths = 5; //create a resolution independent count -- divide by 10 since there are //10 tenths in a second. int count = number_of_tenths*(time_duration::ticks_per_second()/10); time_duration td(1,2,3,count); //01:02:03.5 //no matter the resolution settings
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_types.hpp" //no i/o just types
Construction
Count Based Construction
Construct from String
Accessors
Conversion To String
Operators
Struct tm, time_t, and FILETIME FunctionsFunction for converting a time_duration to a
Introduction --
Header --
Construction --
Mutators --
Accessors --
Conversion To String --
Operators
IntroductionThe class boost::posix_time::time_period provides direct representation for ranges between two times. Periods provide the ability to simplify some types of calculations by simplifying the conditional logic of the program.
A period that is created with beginning and end points being equal, or with a duration of zero, is known as a zero length period. Zero length periods are considered invalid (it is perfectly legal to construct an invalid period). For these periods, the The time periods example provides an example of using time periods. 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_types.hpp" //no i/o just types
Construction
Mutators
Accessors
Conversion To String
Operators
Introduction --
Header --
Overview --
Operators
IntroductionTime iterators provide a mechanism for iteration through times. Time iterators are similar to Bidirectional Iterators. However, time_iterators are different than standard iterators in that there is no underlying sequence, just a calculation function. In addition, time_iterators are directly comparable against instances of class ptime. Thus a second iterator for the end point of the iteration is not required, but rather a point in time can be used directly. For example, the following code iterates using a 15 minute iteration interval. The print hours example also illustrates the use of the time_iterator.
#include "boost/date_time/posix_time/posix_time.hpp" #include <iostream> int main() { using namespace boost::gregorian; using namespace boost::posix_time; date d(2000,Jan,20); ptime start(d); ptime end = start + hours(1); time_iterator titr(start,minutes(15)); //increment by 15 minutes //produces 00:00:00, 00:15:00, 00:30:00, 00:45:00 while (titr < end) { std::cout << to_simple_string(*titr) << std::endl; ++titr; } std::cout << "Now backward" << std::endl; //produces 01:00:00, 00:45:00, 00:30:00, 00:15:00 while (titr > start) { std::cout << to_simple_string(*titr) << std::endl; --titr; } }
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_types.hpp" //no i/o just types
Overview
Operators
|