C++ Portable Types Library (PTypes) Version 1.7

Top: Basic types: Date/time:

The datetime data type and the accompanying utilities provide portable (system-independent) means of calendar and time manipulation. Datetime is equivalent to signed 64-bit integer type (large type in PTypes) which holds a time value with a millisecond precision in the range of dates 01/01/0001 through 12/31/9999. The value of a datetime variable represents the number of milliseconds since the beginning of the calendar, i.e. midnight January 1st, year 1. The upper limit is caused by the fact that the leap year rule may (and most likely will) change after the year 10000.

Variables of type datetime can be used in various ways: as a date/time stamp, as a difference between two events in the range from 1 millisecond up to 10,000 years, as a date alone without the time (e.g. for calendar manipulation), as a time value alone, etc.

The date/time manipulation functions are divided into 3 groups in this documentation: general, date/calendar and time. Most of these functions work with parameters of type datetime. The prototypes and other declarations are in the header file <ptime.h>.

Here are some examples of using datetime and the accompanying utilities:

#include <pasync.h>    // for psleep()
#include <ptime.h>


int main()
    // PTypes' birthday (birth moment, if you wish)
    datetime d = encodedate(2000, 3, 30) + encodetime(13, 24, 58, 995);
    pout.putf("PTypes' birth moment: %s\n", dttostring(d, "%c"));
    // now see how old is PTypes in milliseconds
    datetime diff = now() - d;
    int hours, mins, secs, msecs;
    decodetime(diff, hours, mins, secs, msecs);
    pout.putf("PTypes' life time: %d hours %d minutes %d seconds and %d milliseconds\n",
        days(diff) * 24 + hours, mins, secs, msecs);
    // measure the difference in milliseconds between two calls to now()
    datetime m = now();
    psleep(16);  // sleep 16 milliseconds
    pout.putf("A 16 millisecond dream lasted actually %d milliseconds\n", now() - m);
    // this will show the actual precision of the clock on the given platform;
    // Windows, f.ex., always shows the difference in 10 msec increments

PTypes home