PIP 5.5.3
Platform-Independent Primitives
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
PISystemTime Class Reference

System time with nanosecond precision. More...

#include <pisystemtime.h>

Classes

class  Frequency
 Frequency type. More...
 

Public Member Functions

 PISystemTime ()
 Contructs time with seconds and nanoseconds = 0.
 
 PISystemTime (int s, int ns)
 Contructs time with "s" seconds and "ns" nanoseconds.
 
 PISystemTime (std::pair< int, int > s_ns)
 Contructs time with "s" seconds and "ns" nanoseconds.
 
bool isNull () const
 Returns if time is null.
 
bool isNotNull () const
 Returns if time is not null.
 
bool isPositive () const
 Returns if time is greater than 0.
 
bool isNegative () const
 Returns if time is lesser than 0.
 
double toSeconds () const
 Returns time value in seconds.
 
double toMilliseconds () const
 Returns time value in milliseconds.
 
double toMicroseconds () const
 Returns time value in microseconds.
 
double toNanoseconds () const
 Returns time value in nanoseconds.
 
PISystemTimeaddSeconds (double v)
 Add to time "v" seconds.
 
PISystemTimeaddMilliseconds (double v)
 Add to time "v" milliseconds.
 
PISystemTimeaddMicroseconds (double v)
 Add to time "v" microseconds.
 
PISystemTimeaddNanoseconds (double v)
 Add to time "v" nanoseconds.
 
void sleep ()
 Sleep for this time. More...
 
void toTimespec (void *ts)
 On *nix system assign current value to timespec struct.
 
PISystemTime::Frequency toFrequency ()
 Returns Frequency that corresponds this time interval.
 
PIString toString () const
 Returns "yyyy-MM-dd hh:mm:ss.zzz" for absolute time and "<V> <d|h|m|s|ms|us|ns> ..." for relative.
 
PISystemTime abs () const
 Returns copy of this time with absolutely values of s and ns.
 
PISystemTime operator+ (const PISystemTime &t) const
 Returns sum of this time with "t".
 
PISystemTime operator- (const PISystemTime &t) const
 Returns difference between this time and "t".
 
PISystemTime operator* (const double &v) const
 Returns multiplication between this time and "t".
 
PISystemTime operator/ (const double &v) const
 Returns division between this time and "t".
 
PISystemTimeoperator+= (const PISystemTime &t)
 Add to time "t".
 
PISystemTimeoperator-= (const PISystemTime &t)
 Subtract from time "t".
 
PISystemTimeoperator*= (const double &v)
 Multiply time by "v".
 
PISystemTimeoperator/= (const double &v)
 Divide time by "v".
 
bool operator== (const PISystemTime &t) const
 Compare operator.
 
bool operator!= (const PISystemTime &t) const
 Compare operator.
 
bool operator> (const PISystemTime &t) const
 Compare operator.
 
bool operator< (const PISystemTime &t) const
 Compare operator.
 
bool operator>= (const PISystemTime &t) const
 Compare operator.
 
bool operator<= (const PISystemTime &t) const
 Compare operator.
 

Static Public Member Functions

static PISystemTime fromSeconds (double v)
 Contructs time from seconds "v".
 
static PISystemTime fromMilliseconds (double v)
 Contructs time from milliseconds "v".
 
static PISystemTime fromMicroseconds (double v)
 Contructs time from microseconds "v".
 
static PISystemTime fromNanoseconds (double v)
 Contructs time from nanoseconds "v".
 
static PISystemTime fromString (PIString s)
 Contructs time from string "s" ("yyyy-MM-dd hh:mm:ss.zzz" or free form)
 
static PISystemTime current (bool precise_but_not_system=false)
 Returns current system time.
 

Public Attributes

int seconds
 Seconds time part.
 
int nanoseconds
 Nanoseconds time part.
 

Detailed Description

System time with nanosecond precision.

Synopsis

This class provide arithmetic functions for POSIX system time. This time represents as seconds and nanosecons in integer formats. You can take current system time with function PISystemTime::current(), compare times, sum or subtract two times, convert time to/from seconds, milliseconds, microseconds or nanoseconds.

Example

int main() {
PISystemTime t0; // s = ns = 0
t0.addMilliseconds(200); // s = 0, ns = 200000000
t0.addMilliseconds(900); // s = 1, ns = 100000000
t0 -= PISystemTime::fromSeconds(0.1); // s = 1, ns = 0
t0.sleep(); // sleep for 1 second
piMSleep(500);
(t1 - t0).sleep(); // sleep for 500 milliseconds
return 0;
};
System time with nanosecond precision.
Definition: pisystemtime.h:37
PISystemTime & addMilliseconds(double v)
Add to time "v" milliseconds.
Definition: pisystemtime.h:235
static PISystemTime current(bool precise_but_not_system=false)
Returns current system time.
Definition: pisystemtime.cpp:221
static PISystemTime fromSeconds(double v)
Contructs time from seconds "v".
Definition: pisystemtime.h:375
void sleep()
Sleep for this time.
Definition: pisystemtime.cpp:108
void piMSleep(double msecs)
Precise sleep for "msecs" milliseconds.
Definition: pitime.h:42

Member Function Documentation

◆ sleep()

void PISystemTime::sleep ( )

Sleep for this time.

Warning

Use this function to sleep for difference of system times or constructs system time. If you call this function on system time returned with PISystemTime::current() thread will be sleep almost forever