...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Introduction -- Usage Examples
Defines 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.
Example | Description |
---|---|
Time Math | A few simple calculations using ptime and time_durations. |
Print Hours | Retrieve time from clock, use a time_iterator. |
Local to UTC Conversion | Demonstrates a couple different ways to convert a local to UTC time including daylight savings rules. |
Time Periods | Some simple examples of intersection and display of time periods. |
The 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.
#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
Syntax | Description |
---|---|
Example | |
ptime(date,time_duration) |
Construct from a date and offset |
ptime t1(date(2002,Jan,10), time_duration(1,2,3)); ptime t2(date(2002,Jan,10), hours(1)+nanosec(5)); | |
ptime(ptime) |
Copy constructor |
ptime t3(t1) | |
ptime(special_values sv) |
Constructor for infinities, not-a-date-time, max_date_time, and min_date_time |
ptime d1(neg_infin); ptime d2(pos_infin); ptime d3(not_a_date_time); ptime d4(max_date_time); ptime d5(min_date_time); | |
ptime; |
Default constructor. Creates a ptime object initialized to not_a_date_time. NOTE: this constructor can be disabled by defining DATE_TIME_NO_DEFAULT_CONSTRUCTOR (see compiler_config.hpp) |
ptime p; // p => not_a_date_time |
Syntax | Description |
---|---|
Example | |
ptime time_from_string(std::string) |
From delimited string. NOTE: Excess digits in fractional seconds will be dropped. Ex: "1:02:03.123456999" => 1:02:03.123456. This behavior is affected by the precision the library is compiled with (see Build-Compiler Information. |
std::string ts("2002-01-20 23:59:59.000"); ptime t(time_from_string(ts)) | |
ptime from_iso_string(std::string) |
From non delimited ISO 8601 form string. |
std::string ts("20020131T235959"); ptime t(from_iso_string(ts)) | |
ptime from_iso_extended_string(std::string) |
From delimited ISO 8601 form string. |
std::string ts("2020-01-31T23:59:59.123"); ptime t(from_iso_extended_string(ts)) |
Syntax | Description |
---|---|
Example | |
ptime second_clock::local_time() |
Get the local time, second level resolution, based on the time zone settings of the computer. |
ptime t(second_clock::local_time()); | |
ptime second_clock::universal_time() |
Get the UTC time. |
ptime t(second_clock::universal_time()) | |
ptime microsec_clock::local_time() |
Get the local time using a sub second resolution clock. On Unix systems this is implemented using gettimeofday. On most Win32 platforms it is implemented using ftime. Win32 systems often do not achieve microsecond resolution via this API. If higher resolution is critical to your application test your platform to see the achieved resolution. |
ptime t(microsec_clock::local_time()); | |
ptime microsec_clock::universal_time() |
Get the UTC time using a sub second resolution clock. On Unix systems this is implemented using gettimeofday. On most Win32 platforms it is implemented using ftime. Win32 systems often do not achieve microsecond resolution via this API. If higher resolution is critical to your application test your platform to see the achieved resolution. |
ptime t(microsec_clock::universal_time()); |
Syntax | Description |
---|---|
Example | |
ptime from_time_t(time_t t); |
Converts a time_t into a ptime. |
ptime t = from_time_t(tt); | |
ptime from_ftime<ptime>(FILETIME ft); |
Creates a ptime object from a FILETIME structure. |
ptime t = from_ftime<ptime>(ft); |
Syntax | Description |
---|---|
Example | |
date date() |
Get the date part of a time. |
date d(2002,Jan,10); ptime t(d, hour(1)); t.date() --> 2002-Jan-10; | |
time_duration time_of_day() |
Get the time offset in the day. |
date d(2002,Jan,10); ptime t(d, hour(1)); t.time_of_day() --> 01:00:00; | |
bool is_infinity() const |
Returns true if ptime is either positive or negative infinity |
ptime pt(pos_infin); pt.is_infinity(); // --> true | |
bool is_neg_infinity() const |
Returns true if ptime is negative infinity |
ptime pt(neg_infin); pt.is_neg_infinity(); // --> true | |
bool is_pos_infinity() const |
Returns true if ptime is positive infinity |
ptime pt(neg_infin); pt.is_pos_infinity(); // --> true | |
bool is_not_a_date_time() const |
Returns true if value is not a ptime |
ptime pt(not_a_date_time); pt.is_not_a_date_time(); // --> true | |
bool is_special() const |
Returns true if ptime is any special_value
|
ptime pt(pos_infin); ptime pt2(not_a_date_time); ptime pt3(date(2005,Mar,1), hours(10)); pt.is_special(); // --> true pt2.is_special(); // --> true pt3.is_special(); // --> false |
Syntax | Description |
---|---|
Example | |
std::string to_simple_string(ptime) |
To YYYY-mmm-DD HH:MM:SS.fffffffff string where mmm 3 char month name and the fractional seconds are only included if non-zero. |
2002-Jan-01 10:00:01.123456789 | |
std::string to_iso_string(ptime) |
Convert to form YYYYMMDDTHHMMSS.fffffffff where T is the date-time separator and the fractional seconds are only included if non-zero. |
20020131T100001.123456789 | |
std::string to_iso_extended_string(ptime) |
Convert to form YYYY-MM-DDTHH:MM:SS.fffffffff where T is the date-time separator and the fractional seconds are only included if non-zero. |
2002-01-31T10:00:01.123456789 |
Syntax | Description |
---|---|
Example | |
operator<<, operator>> |
Streaming operators. Note: As of version 1.33, streaming operations have been greatly improved. See Date Time IO System for more details (including exceptions and error conditions). |
ptime pt(not_a_date_time); stringstream ss("2002-Jan-01 14:23:11"); ss >> pt; std::cout << pt; // "2002-Jan-01 14:23:11" | |
operator==, operator!=, operator>, operator<, operator>=, operator<= |
A full complement of comparison operators |
t1 == t2, etc | |
ptime operator+(days) |
Return a ptime adding a day offset |
date d(2002,Jan,1); ptime t(d,minutes(5)); days dd(1); ptime t2 = t + dd; | |
ptime operator-(days) |
Return a ptime subtracting a day offset |
date d(2002,Jan,1); ptime t(d,minutes(5)); days dd(1); ptime t2 = t - dd; | |
ptime operator+(time_duration) |
Return a ptime adding a time duration |
date d(2002,Jan,1); ptime t(d,minutes(5)); ptime t2 = t + hours(1) + minutes(2); | |
ptime operator-(time_duration) |
Return a ptime subtracting a time duration |
date d(2002,Jan,1); ptime t(d,minutes(5)); ptime t2 = t - minutes(2); | |
time_duration operator-(ptime) |
Take the difference between two times. |
date d(2002,Jan,1); ptime t1(d,minutes(5)); ptime t2(d,seconds(5)); time_duration t3 = t2 - t1;//negative result |
Functions for converting posix_time objects to, and from, tm
structs are provided as well as conversion from time_t
and FILETIME
.
Syntax | Description |
---|---|
Example | |
tm to_tm(ptime) |
A function for converting a ptime object to a tm struct. The tm_isdst field is set to -1. |
ptime pt(date(2005,Jan,1), time_duration(1,2,3)); tm pt_tm = to_tm(pt); /* tm_year => 105 tm_mon => 0 tm_mday => 1 tm_wday => 6 (Saturday) tm_yday => 0 tm_hour => 1 tm_min => 2 tm_sec => 3 tm_isdst => -1 */ | |
ptime ptime_from_tm(tm timetm) |
A function for converting a tm struct to a ptime object. The fields: tm_wday , tm_yday , and tm_isdst are ignored. |
tm pt_tm; pt_tm.tm_year = 105; pt_tm.tm_mon = 0; pt_tm.tm_mday = 1; pt_tm.tm_hour = 1; pt_tm.tm_min = 2; pt_tm.tm_sec = 3; ptime pt = ptime_from_tm(pt_tm); // pt => 2005-Jan-01 01:02:03 | |
tm to_tm(time_duration) |
A function for converting a time_duration object to a tm struct. The fields: tm_year , tm_mon , tm_mday , tm_wday , tm_yday are set to zero. The tm_isdst field is set to -1. |
time_duration td(1,2,3); tm td_tm = to_tm(td); /* tm_year => 0 tm_mon => 0 tm_mday => 0 tm_wday => 0 tm_yday => 0 tm_hour => 1 tm_min => 2 tm_sec => 3 tm_isdst => -1 */ | |
ptime from_time_t(std::time_t) |
Creates a ptime from the time_t parameter. The seconds held in the time_t are added to a time point of 1970-Jan-01. |
ptime pt(not_a_date_time); std::time_t t; t = 1118158776; pt = from_time_t(t); // pt => 2005-Jun-07 15:39:36 | |
ptime from_ftime<ptime>(FILETIME) |
A template function that constructs a ptime from a FILETIME struct. |
FILETIME ft; ft.dwHighDateTime = 29715317; ft.dwLowDateTime = 3865122988UL; ptime pt = from_ftime<ptime>(ft); // pt => 2005-Jun-07 15:30:57.039582000 |
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 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:10 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
#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
Syntax | Description |
---|---|
Example | |
time_duration(hours, minutes, seconds, fractional_seconds) |
Construct a duration from the counts. The fractional_second parameter is a number of units and is therefore affected by the resolution the application is compiled with (see Build-Compiler Information). If the fractional_seconds argument exceeds the limit of the compiled precision, the excess value will be "carried over" into the seconds field. See above for techniques to creating a resolution independent count. |
time_duration td(1,2,3,9); //1 hr 2 min 3 sec 9 nanoseconds time_duration td2(1,2,3,123456789); time_duration td3(1,2,3,1000); // with microsecond resolution (6 digits) // td2 => "01:04:06.456789" // td3 => "01:02:03.001000" // with nanosecond resolution (9 digits) // td2 => "01:02:03.123456789" // td3 => "01:02:03.000001000" | |
time_duration(special_value sv) |
Special values constructor. Important note: When a time_duration is a special value, either by construction or other means, the following accessor functions will give unpredictable results: hours(), minutes(), seconds(), ticks(), fractional_seconds(), total_nanoseconds(), total_microseconds(), total_milliseconds(), total_seconds()The remaining accessor functions will work as expected. |
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); |
Syntax | Description |
---|---|
Example | |
time_duration duration_from_string(std::string) |
From delimited string. NOTE: Excess digits in fractional seconds will be dropped. Ex: "1:02:03.123456999" => 1:02:03.123456. This behavior is affected by the precision the library is compiled with (see Build-Compiler Information. |
std::string ts("23:59:59.000"); time_duration td(duration_from_string(ts)); |
Syntax | Description | |
---|---|---|
Example | ||
boost::int64_t hours() |
Get the number of normalized hours (will give unpredictable results if calling time_duration is a special_value ). |
|
time_duration td(1,2,3); time_duration neg_td(-1,2,3); td.hours(); // --> 1 neg_td.hours(); // --> -1 | ||
boost::int64_t minutes() |
Get the number of minutes normalized +/-(0..59) (will give unpredictable results if calling time_duration is a special_value ). |
|
time_duration td(1,2,3); time_duration neg_td(-1,2,3); td.minutes(); // --> 2 neg_td.minutes(); // --> -2 | ||
boost::int64_t seconds() const |
Get the normalized number of second +/-(0..59) (will give unpredictable results if calling time_duration is a special_value ). |
|
time_duration td(1,2,3); time_duration neg_td(-1,2,3); td.seconds(); // --> 3 neg_td.seconds(); // --> -3 | ||
boost::int64_t total_seconds() const |
Get the total number of seconds truncating any fractional seconds (will give unpredictable results if calling time_duration is a special_value ). |
|
time_duration td(1,2,3,10); td.total_seconds(); // --> (1*3600) + (2*60) + 3 == 3723 | ||
boost::int64_t total_milliseconds() const |
Get the total number of milliseconds truncating any remaining digits (will give unpredictable results if calling time_duration is a special_value ). |
|
time_duration td(1,2,3,123456789); td.total_milliseconds(); // HMS --> (1*3600) + (2*60) + 3 == 3723 seconds // milliseconds is 3 decimal places // (3723 * 1000) + 123 == 3723123 | ||
boost::int64_t total_microseconds() const |
Get the total number of microseconds truncating any remaining digits (will give unpredictable results if calling time_duration is a special_value ). |
|
time_duration td(1,2,3,123456789); td.total_microseconds(); // HMS --> (1*3600) + (2*60) + 3 == 3723 seconds // microseconds is 6 decimal places // (3723 * 1000000) + 123456 == 3723123456 | ||
boost::int64_t total_nanoseconds() const |
Get the total number of nanoseconds truncating any remaining digits (will give unpredictable results if calling time_duration is a special_value ). |
|
time_duration td(1,2,3,123456789); td.total_nanoseconds(); // HMS --> (1*3600) + (2*60) + 3 == 3723 seconds // nanoseconds is 9 decimal places // (3723 * 1000000000) + 123456789 // == 3723123456789 | ||
boost::int64_t fractional_seconds() const |
Get the number of fractional seconds (will give unpredictable results if calling time_duration is a special_value ). |
|
time_duration td(1,2,3, 1000); td.fractional_seconds(); // --> 1000 | ||
bool is_negative() const |
True if and only if duration is negative. | |
time_duration td(-1,0,0); td.is_negative(); // --> true | ||
bool is_zero() const |
True if and only if duration is zero. | |
time_duration td(0,0,0); td.is_zero(); // --> true | ||
bool is_positive() const |
True if and only if duration is positive. | |
time_duration td(1,0,0); td.is_positive(); // --> 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 | ||
time_duration abs() const |
Generate a new duration with the absolute value of the time duration. | |
time_duration td(-1,0,0); td.abs(); // --> 01:00:00 |
time_duration td(+1,0,0); td.abs(); // --> 01:00:00 |
|
date_time::time_resolutions time_duration::resolution() |
Describes the resolution capability of the time_duration class. time_resolutions is an enum of resolution possibilities ranging from seconds to nanoseconds. | |
time_duration::resolution() --> nano |
||
unsigned short time_duration::num_fractional_digits() |
Returns the number of fractional digits the time resolution has. | |
unsigned short secs; secs = time_duration::num_fractional_digits(); // 9 for nano, 6 for micro, etc. |
||
boost::int64_t time_duration::ticks_per_second() |
Return the number of ticks in a second. For example, if the duration supports nanoseconds then the returned result will be 1,000,000,000 (1e+9). | |
std::cout << time_duration::ticks_per_second(); |
||
boost::int64_t ticks() |
Return the raw count of the duration type (will give unpredictable results if calling time_duration is a special_value ). |
|
time_duration td(0,0,0, 1000); td.ticks() // --> 1000 |
||
time_duration time_duration::unit() |
Return smallest possible unit of duration type (1 nanosecond). | |
time_duration::unit() --> time_duration(0,0,0,1) |
||
bool is_neg_infinity() const |
Returns true if time_duration is negative infinity | |
time_duration td(neg_infin); td.is_neg_infinity(); // --> true |
||
bool is_pos_infinity() const |
Returns true if time_duration is positive infinity | |
time_duration td(pos_infin); td.is_pos_infinity(); // --> true |
||
bool is_not_a_date_time() const |
Returns true if value is not a time | |
time_duration td(not_a_date_time); td.is_not_a_date_time(); // --> true |
||
bool is_special() const |
Returns true if time_duration is any special_value
|
|
time_duration td(pos_infin); time_duration td2(not_a_date_time); time_duration td3(2,5,10); td.is_special(); // --> true td2.is_special(); // --> true td3.is_special(); // --> false |
Syntax | Description |
---|---|
Example | |
std::string to_simple_string(time_duration) |
To HH:MM:SS.fffffffff where fffffffff 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 where the fractional seconds are only included if non-zero. |
100001.123456789 |
Syntax | Description |
---|---|
Example | |
operator<<, operator>> |
Streaming operators. Note: As of version 1.33, streaming operations have been greatly improved. See Date Time IO System for more details (including exceptions and error conditions). |
time_duration td(0,0,0); stringstream ss("14:23:11.345678"); ss >> td; std::cout << td; // "14:23:11.345678" | |
operator==, operator!=, operator>, operator<, operator>=, operator<= |
A full complement of comparison operators |
dd1 == dd2, etc | |
time_duration operator+(time_duration) |
Add durations. |
time_duration td1(hours(1)+minutes(2)); time_duration td2(seconds(10)); time_duration td3 = td1 + td2; | |
time_duration operator-(time_duration) |
Subtract durations. |
time_duration td1(hours(1)+nanoseconds(2)); time_duration td2 = td1 - minutes(1); | |
time_duration operator/(int) |
Divide the length of a duration by an integer value. Discards any remainder. |
hours(3)/2 == time_duration(1,30,0); nanosecond(3)/2 == nanosecond(1); | |
time_duration operator*(int) |
Multiply the length of a duration by an integer value. |
hours(3)*2 == hours(6); |
Function for converting a time_duration to a tm
struct is provided.
Syntax | Description |
---|---|
Example | |
tm to_tm(time_duration) |
A function for converting a time_duration object to a tm struct. The fields: tm_year , tm_mon , tm_mday , tm_wday , tm_yday are set to zero. The tm_isdst field is set to -1. |
time_duration td(1,2,3); tm td_tm = to_tm(td); /* tm_year => 0 tm_mon => 0 tm_mday => 0 tm_wday => 0 tm_yday => 0 tm_hour => 1 tm_min => 2 tm_sec => 3 tm_isdst => -1 */ |
The 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 last
point will always be one unit less that the begin
point.
The time periods example provides an example of using time periods.
#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
Syntax | Description |
---|---|
Example | |
time_period(ptime, ptime) |
Create a period as [begin, end). If end is <= begin then the period will be defined as invalid. |
date d(2002,Jan,01); ptime t1(d, seconds(10)); //10 sec after midnight ptime t2(d, hours(10)); //10 hours after midnight time_period tp(t1, t2); | |
time_period(ptime, time_duration) |
Create a period as [begin, begin+len) where end would be begin+len. If len is <= zero then the period will be defined as invalid. |
date d(2002,Jan,01); ptime t(d, seconds(10)); //10 sec after midnight time_period tp(t, hours(3)); | |
time_period(time_period rhs) |
Copy constructor |
time_period tp1(tp); |
Syntax | Description |
---|---|
Example | |
time_period shift(time_duration) |
Add duration to both begin and end. |
time_period tp(ptime(date(2005,Jan,1),hours(1)), hours(2)); tp.shift(minutes(5)); // tp == 2005-Jan-01 01:05:00 to 2005-Jan-01 03:05:00 | |
time_period expand(days) |
Subtract duration from begin and add duration to end. |
time_period tp(ptime(date(2005,Jan,1),hours(1)), hours(2)); tp.expand(minutes(5)); // tp == 2005-Jan-01 00:55:00 to 2005-Jan-01 03:05:00 |
Syntax | Description |
---|---|
Example | |
ptime begin() |
Return first time of period. |
date d(2002,Jan,01); ptime t1(d, seconds(10)); //10 sec after midnight ptime t2(d, hours(10)); //10 hours after midnight time_period tp(t1, t2); tp.begin(); // --> 2002-Jan-01 00:00:10 | |
ptime last() |
Return last time in the period |
date d(2002,Jan,01); ptime t1(d, seconds(10)); //10 sec after midnight ptime t2(d, hours(10)); //10 hours after midnight time_period tp(t1, t2); tp.last();// --> 2002-Jan-01 09:59:59.999999999 | |
ptime end() |
Return one past the last in period |
date d(2002,Jan,01); ptime t1(d, seconds(10)); //10 sec after midnight ptime t2(d, hours(10)); //10 hours after midnight time_period tp(t1, t2); tp.end(); // --> 2002-Jan-01 10:00:00 | |
time_duration length() |
Return the length of the time period. |
date d(2002,Jan,01); ptime t1(d); //midnight time_period tp(t1, hours(1)); tp.length() --> 1 hour | |
bool is_null() |
True if period is not well formed. eg: end is less than or equal to begin. |
date d(2002,Jan,01); ptime t1(d, hours(12)); // noon on Jan 1st ptime t2(d, hours(9)); // 9am on Jan 1st time_period tp(t1, t2); tp.is_null(); // true | |
bool contains(ptime) |
True if ptime is within the period. Zero length periods cannot contain any points. |
date d(2002,Jan,01); ptime t1(d, seconds(10)); //10 sec after midnight ptime t2(d, hours(10)); //10 hours after midnight ptime t3(d, hours(2)); //2 hours after midnight time_period tp(t1, t2); tp.contains(t3); // true time_period tp2(t1, t1); tp2.contains(t1); // false | |
bool contains(time_period) |
True if period is within the period |
time_period tp1(ptime(d,hours(1)), ptime(d,hours(12))); time_period tp2(ptime(d,hours(2)), ptime(d,hours(4))); tp1.contains(tp2); // --> true tp2.contains(tp1); // --> false | |
bool intersects(time_period) |
True if periods overlap |
time_period tp1(ptime(d,hours(1)), ptime(d,hours(12))); time_period tp2(ptime(d,hours(2)), ptime(d,hours(4))); tp2.intersects(tp1); // --> true | |
time_period intersection(time_period) |
Calculate the intersection of 2 periods. Null if no intersection. |
time_period merge(time_period) |
Returns union of two periods. Null if no intersection. |
time_period span(time_period) |
Combines two periods and any gap between them such that begin = min(p1.begin, p2.begin) and end = max(p1.end , p2.end). |
Syntax | Description |
---|---|
Example | |
std::string to_simple_string(time_period dp) |
To [YYYY-mmm-DD hh:mm:ss.fffffffff/YYYY-mmm-DD hh:mm:ss.fffffffff] string where mmm is 3 char month name and the fractional seconds are only included if non-zero. |
[2002-Jan-01 01:25:10.000000001/ 2002-Jan-31 01:25:10.123456789] // string occupies one line |
Syntax | Description |
---|---|
Example | |
operator<< |
Output streaming operator for time duration. Uses facet to output [date time_of_day/date time_of_day]. The default is format is [YYYY-mmm-DD hh:mm:ss.fffffffff/YYYY-mmm-DD hh:mm:ss.fffffffff] string where mmm is 3 char month name and the fractional seconds are only included if non-zero. |
[2002-Jan-01 01:25:10.000000001/ \ 2002-Jan-31 01:25:10.123456789] | |
operator>> |
Input streaming operator for time duration. Uses facet to read [date time_of_day/date time_of_day]. The default is format is [YYYY-mmm-DD hh:mm:ss.fffffffff/YYYY-mmm-DD hh:mm:ss.fffffffff] string where mmm is 3 char month name and the fractional seconds are only included if non-zero. |
[2002-Jan-01 01:25:10.000000001/ \ 2002-Jan-31 01:25:10.123456789] | |
operator==, operator!= |
Equality operators. Periods are equal if p1.begin == p2.begin && p1.last == p2.last |
if (tp1 == tp2) {... | |
operator< |
Ordering with no overlap. True if tp1.end() less than tp2.begin() |
if (tp1 < tp2) {... | |
operator> |
Ordering with no overlap. True if tp1.begin() greater than tp2.end() |
if (tp1 > tp2) {... etc | |
operator<=, operator>= |
Defined in terms of the other operators. |
Time 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; } }
#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
Class | Description |
---|---|
Construction Parameters | |
time_iterator |
Iterate incrementing by the specified duration. |
ptime start_time, time_duration increment |
Syntax | Description |
---|---|
Example | |
operator==(const ptime& rhs), operator!=(const ptime& rhs), operator>, operator<, operator>=, operator<= |
A full complement of comparison operators |
date d(2002,Jan,1); ptime start_time(d, hours(1)); //increment by 10 minutes time_iterator titr(start_time, minutes(10)); ptime end_time = start_time + hours(2); if (titr == end_time) // false if (titr != end_time) // true if (titr >= end_time) // false if (titr <= end_time) // true | |
prefix increment |
Increment the iterator by the specified duration. |
//increment by 10 milli seconds time_iterator titr(start_time, milliseconds(10)); ++titr; // == start_time + 10 milliseconds | |
prefix decrement |
Decrement the iterator by the specified time duration. |
time_duration td(1,2,3); time_iterator titr(start_time, td); --titr; // == start_time - 01:02:03 |