mirror hosted by tehsausage.com
PTypes: date/time:

C++ Portable Types Library (PTypes) Version 2.1


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), and as a time value alone.

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>
#include <pstreams.h>

USING_PTYPES

int main()
{
    // PTypes' birthday (birth moment, if you wish)
    datetime d = encodedate(2000, 3, 30) + encodetime(13, 24, 58, 995);
    // The format specifier %t is for timestamps
    pout.putf("PTypes' birth moment: %t\n", d);
    
    // 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(17);  // sleep 17 milliseconds
    pout.putf("A 17 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