Basic Types

The Pt framework defines a number of signed and unsigned, fixed-size integer types. These are typedefs of fundamental integer types and the actual type depends on the platform.

The classes that represent dates and times, namely Pt::Date, Pt::Time, Pt::DateTime and Pt::Timespan, can be used for comparison, sorting or calculating dates, times and timespans. These types are serializable and conversion to ISO string format is supported as well.

The class Pt::Any is able to contain any other copyable type. This is useful in situations where type erasure is required, for example, to store completely unrelated types in a list or vector.

Fixed Size Integers

The Pt framework defines a number of fixed-size, signed and unsigned integers ranging from 8-bit to 64-bit widths. They are typedefs for builtin fundamental types such as int or long and the actual type depends on the platform. For example, Pt::uint8_t is a typedef for an unsigned 8 bits wide integer type and Pt::int32_t is a typedef for a signed 32 bits wide integer. The following table shows all available fixed-size integer types:

int8_t signed 8 bit integer uint18_t unsigned 8 bit integer
int16_t signed 16 bit integer uint16_t unsigned 16 bit integer
int32_t signed 32 bit integer uint32_t unsigned 32 bit integer
int64_t signed 64 bit integer uint64_t unsigned 64 bit integer

Dates and Times

Time intervals can be represented by Pt::Timespan objects with microsecond accuracy. It is often the result of the calculations involving Pt::Date, Pt::Time ot Pt::DateTime. A Pt::Timespan can be constructed from the number of microseconds and then be converted to other time units with toHours(), toSeconds(), toMSecs() and toUSecs(). When two Pt::Timespans are compared, the shorter one is considered less. Addition and subtraction is supported as shown in the next example:

#include <Pt/Timespan.h>
#include <iostream>
Pt::Timespan ts1(10000);
Pt::Timespan ts2(1000);
Pt::Timespan d = ts2 - ts1;
std::cout << "The difference is " << d.toSecs() << " secs" << std::endl;

Pt::Date is an easy way to handle calendar dates. It can be constructed from the days, month and year components or from an ISO string using fromIsoString(). The date components can be accessed with day(), month() and year(). Once a Date object is created, calendar information can be accessed, for example with dayOfYear(), dayOfWeek() or isLeapYear(). Dates can be compared and a Date is considered less if it is earlier than another Date. It is also possible to add or subtract days from a Date, which yields a new date or modifies it. Dates can be subtracted, which yields the number of days between them, as shown in the next example:

#include <Pt/Date.h>
#include <iostream>
int daysUntilChristmas(const std::string& todayStr)
Pt::Date today = Pt::Date::fromIsoString(todayStr);
Pt::Date xmas(today.year(), 12, 24);
return xmas - today;

Pt::InvalidDate is thrown, if a date could not be constructed, for example if one of the date components is out of range. To avoid an exception, date components can be validated with isValid().

A Pt::Time object contains a wall-clock time in hours, minutes, seconds amd milliseconds. It can be constructed either from the numeric time values or from a string in ISO format using fromIsoString(). The separate time values can be accessed with hour(), minute(), second() and msec(). Times can be compared and a Time is considered less, if it is earlier than another Time. Subtracting a time from another yields a Pt::Timespan as the result. Pt::Timespans can also be added or subtracted from a Time, yielding a new Time or modifying it, as shown in the following example:

#include <Pt/Date.h>
#include <iostream>
Pt::Timespan untilMidnight(const std::string& timeStr)
Pt::Date midnight(0 ,0, 0);
return midnight - now;

Pt::InvalidTime is thrown, if a time could not be constructed, for example if one of the time values is out of range. To avoid an exception, time values can be validated with isValid().

Pt::DateTime combines a Pt::Date and a Pt::Time object into one instance. It can either be constructed from the corrsponding numeric values or a string in ISO format. The date and time parts can be accessed with date() and time(). When two DateTimes are compared, one is considered less, if its date is earlier or if the time is earlier in case of equal dates. A Pt::Timespan can be added or subtracted from a DateTime and this is also the result when two DateTimes are subtracted. To avoid the exceptions thrown by the underlying time and date, isValid() can be used to check numeric date and time values.

The Any Class

Pt::Any can contain any other type, which is default- and copy constructible. When a value is assigned to an Any, a copy is made, just like when a type is inserted in a standard C++ container. Anys can be assigned to another Any, which will also copy the contained value. The contained type can be accessed via any_cast(). It is only possible to get the contained value, if the type matches.

#include <Pt/Any.h>
Pt::Any a = 5;
int i = any_cast<int>( a ); // i is 5 now
float f = any_cast<float>( a ) // throws std::bad_cast

The any_cast() to value and reference types will throw a std::bad_cast exception, if the type is not correct. Using a pointer type as the template argument for any_cast() will not throw an exception, but return a null pointer if the requested type does not match.