...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
ptime Documentation
Header -- Construction -- Construct from String -- Construct from Clock -- Construct from time_t -- Accessors -- Conversion To String -- Operators
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 | ptime t1(neg_infin); ptime t2(pos_infin); ptime t3(not_a_date_time); |
Syntax | Description | Example |
ptime time_from_string(const std::string&) | From delimited string. | std::string ts("2002-01-20 23:59:59.000");
ptime t(time_from_string(ts)) |
ptime from_iso_string(const std::string&) | From non delimited iso form string. | std::string ts("20020131T235959");
ptime t(from_iso_string(ts)) |
Syntax | Description | Example |
static 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()) |
static ptime second_clock::universal_time() | Get the UTC time. | ptime t(second_clock::universal_day()) |
Syntax | Description | Example |
ptime from_time_t(time_t t); | Converts a time_t into a ptime. | ptime t = from_time_t(tt); |
Syntax | Description | Example |
date date() const | 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() const | Get the time offset in the day. | date d(2002,Jan,10); ptime t(d, hour(1)); t.time_of_day() --> 01:00:00; |
Syntax | Description | Example |
std::string to_simple_string(ptime) | To YYYY-mmm-DD HH:MM:SS.fffffffff string where mmm 3 char month name. Fractional seconds 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 | 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 | 2002-01-31T10:00:01,123456789 |
Syntax | Description | Example |
operator<< | Stream output operator. Format of the date output is subject to the current localization settings. See I/O localization for more. | date d(2002,Jan,1) ptime t(d, hours(3)+minutes(2)+milliseconds(1)); std::cout << t << std::endl;//2002-Jan-01 03:02:00.001 |
operator==, operator!=, operator>, operator< operator>=, operator<= |
A full complement of comparison operators | t1 == t2, etc |
ptime operator+(date_duration) const | Return a ptime adding a day offset | date d(2002,Jan,1); ptime t(d,minutes(5)); date_duration dd(1); ptime t2 = t + dd; |
ptime operator-(date_duration) const | Return a ptime subtracting a day offset | date d(2002,Jan,1); ptime t(d,minutes(5)); date_duration dd(1); ptime t2 = t - dd; |
ptime operator+(time_duration) const | 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) const | 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) const | 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 |