236 lines
11 KiB
C++
236 lines
11 KiB
C++
/*! \file pitime.h
|
|
* \brief Time structs
|
|
*/
|
|
/*
|
|
PIP - Platform Independent Primitives
|
|
Time structs
|
|
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef PITIME_H
|
|
#define PITIME_H
|
|
|
|
#include <ctime>
|
|
#include <csignal>
|
|
#include "pistring.h"
|
|
|
|
#ifdef DOXYGEN
|
|
//! \brief Sleep for "msecs" milliseconds
|
|
void msleep(int msecs);
|
|
#else
|
|
# ifdef WINDOWS
|
|
inline void msleep(int msecs) {Sleep(msecs);}
|
|
# else
|
|
inline void msleep(int msecs) {usleep(msecs * 1000);}
|
|
# endif
|
|
#endif
|
|
|
|
/*! \brief Precise sleep for "usecs" microseconds
|
|
* \details This function consider \c "usleep" offset
|
|
* on QNX/Linux/Mac, which is calculated with
|
|
* \a pip_sys_test program. If there is correct
|
|
* offset value in system config, this function
|
|
* wait \b exactly "usecs" microseconds. */
|
|
void piUSleep(int usecs); // on !Windows consider constant "usleep" offset
|
|
|
|
/*! \brief Precise sleep for "msecs" milliseconds
|
|
* \details This function exec \a piUSleep (msecs * 1000). */
|
|
inline void piMSleep(int msecs) {piUSleep(msecs * 1000);} // on !Windows consider constant "usleep" offset
|
|
|
|
class PIP_EXPORT PISystemTime {
|
|
public:
|
|
|
|
//! Contructs system time with s = ns = 0
|
|
PISystemTime() {seconds = nanoseconds = 0;}
|
|
|
|
//! Contructs system time with s = "s" and ns = "ns"
|
|
PISystemTime(long s, long ns) {seconds = s; nanoseconds = ns; checkOverflows();}
|
|
|
|
//! Contructs system time from another
|
|
PISystemTime(const PISystemTime & t) {seconds = t.seconds; nanoseconds = t.nanoseconds;}
|
|
|
|
|
|
//! Returns stored system time value in seconds
|
|
double toSeconds() const {return double(seconds) + nanoseconds / 1.e+9;}
|
|
|
|
//! Returns stored system time value in milliseconds
|
|
double toMilliseconds() const {return seconds * 1.e+3 + nanoseconds / 1.e+6;}
|
|
|
|
//! Returns stored system time value in microseconds
|
|
double toMicroseconds() const {return seconds * 1.e+6 + nanoseconds / 1.e+3;}
|
|
|
|
//! Returns stored system time value in nanoseconds
|
|
double toNanoseconds() const {return seconds * 1.e+9 + double(nanoseconds);}
|
|
|
|
|
|
//! Add to stored system time "v" seconds
|
|
PISystemTime & addSeconds(double v) {*this += fromSeconds(v); return *this;}
|
|
|
|
//! Add to stored system time "v" milliseconds
|
|
PISystemTime & addMilliseconds(double v) {*this += fromMilliseconds(v); return *this;}
|
|
|
|
//! Add to stored system time "v" microseconds
|
|
PISystemTime & addMicroseconds(double v) {*this += fromMicroseconds(v); return *this;}
|
|
|
|
//! Add to stored system time "v" nanoseconds
|
|
PISystemTime & addNanoseconds(double v) {*this += fromNanoseconds(v); return *this;}
|
|
|
|
|
|
//! Sleep for stored value. \warning Use this function to sleep for difference of system times or constructs system time.
|
|
//! If you call this function on system time returned from \a currentSystemTime() thread will be sleep almost forever.
|
|
void sleep() {piUSleep(piFloord(toMicroseconds()));} // wait self value, useful to wait some dT = (t1 - t0)
|
|
|
|
|
|
//! Returns copy of this system time with absolutely values of s and ns
|
|
PISystemTime abs() const {return PISystemTime(piAbsl(seconds), piAbsl(nanoseconds));}
|
|
|
|
//! Returns sum of this system time with "t"
|
|
PISystemTime operator +(const PISystemTime & t) {PISystemTime tt(*this); tt.seconds += t.seconds; tt.nanoseconds += t.nanoseconds; tt.checkOverflows(); return tt;}
|
|
|
|
//! Returns difference between this system time and "t"
|
|
PISystemTime operator -(const PISystemTime & t) {PISystemTime tt(*this); tt.seconds -= t.seconds; tt.nanoseconds -= t.nanoseconds; tt.checkOverflows(); return tt;}
|
|
|
|
//! Add to stored value system time "t"
|
|
PISystemTime & operator +=(const PISystemTime & t) {seconds += t.seconds; nanoseconds += t.nanoseconds; checkOverflows(); return *this;}
|
|
|
|
//! Subtract from stored value system time "t"
|
|
PISystemTime & operator -=(const PISystemTime & t) {seconds -= t.seconds; nanoseconds -= t.nanoseconds; checkOverflows(); return *this;}
|
|
|
|
|
|
//! Compare system times
|
|
bool operator ==(const PISystemTime & t) {return ((seconds == t.seconds) && (nanoseconds == t.nanoseconds));}
|
|
|
|
//! Compare system times
|
|
bool operator !=(const PISystemTime & t) {return ((seconds != t.seconds) || (nanoseconds != t.nanoseconds));}
|
|
|
|
//! Compare system times
|
|
bool operator >(const PISystemTime & t) {if (seconds == t.seconds) return nanoseconds > t.nanoseconds; return seconds > t.seconds;}
|
|
|
|
//! Compare system times
|
|
bool operator <(const PISystemTime & t) {if (seconds == t.seconds) return nanoseconds < t.nanoseconds; return seconds < t.seconds;}
|
|
|
|
//! Compare system times
|
|
bool operator >=(const PISystemTime & t) {if (seconds == t.seconds) return nanoseconds >= t.nanoseconds; return seconds >= t.seconds;}
|
|
|
|
//! Compare system times
|
|
bool operator <=(const PISystemTime & t) {if (seconds == t.seconds) return nanoseconds <= t.nanoseconds; return seconds <= t.seconds;}
|
|
|
|
|
|
//! Contructs system time from seconds "v"
|
|
static PISystemTime fromSeconds(double v) {long s = piFloord(v); return PISystemTime(s, (v - s) * 1000000000);}
|
|
|
|
//! Contructs system time from milliseconds "v"
|
|
static PISystemTime fromMilliseconds(double v) {long s = piFloord(v / 1000.); return PISystemTime(s, (v / 1000. - s) * 1000000000);}
|
|
|
|
//! Contructs system time from microseconds "v"
|
|
static PISystemTime fromMicroseconds(double v) {long s = piFloord(v / 1000000.); return PISystemTime(s, (v / 1000000. - s) * 1000000000);}
|
|
|
|
//! Contructs system time from nanoseconds "v"
|
|
static PISystemTime fromNanoseconds(double v) {long s = piFloord(v / 1000000000.); return PISystemTime(s, (v / 1000000000. - s) * 1000000000);}
|
|
|
|
|
|
//! Seconds of stored system time
|
|
long seconds;
|
|
|
|
//! Nanoseconds of stored system time
|
|
long nanoseconds;
|
|
|
|
private:
|
|
void checkOverflows() {while (nanoseconds >= 1000000000) {nanoseconds -= 1000000000; seconds++;} while (nanoseconds < 0) {nanoseconds += 1000000000; seconds--;}}
|
|
|
|
};
|
|
|
|
//! \relatesalso PICout \relatesalso PIByteArray \brief Output operator to PICout
|
|
inline PICout operator <<(PICout s, const PISystemTime & v) {s.setControl(0, true); s.space(); s << "(" << v.seconds << " s, " << v.nanoseconds << " ns)"; s.restoreControl(); return s;}
|
|
|
|
//! \relatesalso PISystemTime \relatesalso PIByteArray \brief Output operator to PIByteArray
|
|
inline PIByteArray & operator <<(PIByteArray & s, const PISystemTime & v) {s << v.seconds << v.nanoseconds; return s;}
|
|
|
|
//! \relatesalso PISystemTime \relatesalso PIByteArray \brief Input operator from PIByteArray
|
|
inline PIByteArray & operator >>(PIByteArray & s, PISystemTime & v) {s >> v.seconds >> v.nanoseconds; return s;}
|
|
|
|
struct PIP_EXPORT PITime {
|
|
PITime() {hours = minutes = seconds = milliseconds = 0;}
|
|
int milliseconds;
|
|
int seconds;
|
|
int minutes;
|
|
int hours;
|
|
PIString toString(const PIString & format = "h:mm:ss") const;
|
|
};
|
|
PIP_EXPORT bool operator ==(const PITime & t0, const PITime & t1);
|
|
PIP_EXPORT bool operator <(const PITime & t0, const PITime & t1);
|
|
PIP_EXPORT bool operator >(const PITime & t0, const PITime & t1);
|
|
inline bool operator !=(const PITime & t0, const PITime & t1) {return !(t0 == t1);}
|
|
inline bool operator <=(const PITime & t0, const PITime & t1) {return !(t0 > t1);}
|
|
inline bool operator >=(const PITime & t0, const PITime & t1) {return !(t0 < t1);}
|
|
|
|
struct PIP_EXPORT PIDate {
|
|
PIDate() {year = month = day = 0;}
|
|
int day;
|
|
int month;
|
|
int year;
|
|
PIString toString(const PIString & format = "d.MM.yyyy") const;
|
|
};
|
|
PIP_EXPORT bool operator ==(const PIDate & t0, const PIDate & t1);
|
|
PIP_EXPORT bool operator <(const PIDate & t0, const PIDate & t1);
|
|
PIP_EXPORT bool operator >(const PIDate & t0, const PIDate & t1);
|
|
inline bool operator !=(const PIDate & t0, const PIDate & t1) {return !(t0 == t1);}
|
|
inline bool operator <=(const PIDate & t0, const PIDate & t1) {return !(t0 > t1);}
|
|
inline bool operator >=(const PIDate & t0, const PIDate & t1) {return !(t0 < t1);}
|
|
|
|
struct PIP_EXPORT PIDateTime {
|
|
PIDateTime() {year = month = day = hours = minutes = seconds = 0;}
|
|
PIDateTime(const PITime & time) {year = month = day = 0; hours = time.hours; minutes = time.minutes; seconds = time.seconds; milliseconds = time.milliseconds;}
|
|
PIDateTime(const PIDate & date) {year = date.year; month = date.month; day = date.day; hours = minutes = seconds = milliseconds = 0;}
|
|
PIDateTime(const PIDate & date, const PITime & time) {year = date.year; month = date.month; day = date.day; hours = time.hours; minutes = time.minutes; seconds = time.seconds; milliseconds = time.milliseconds;}
|
|
int milliseconds;
|
|
int seconds;
|
|
int minutes;
|
|
int hours;
|
|
int day;
|
|
int month;
|
|
int year;
|
|
PIDateTime normalized() const {return PIDateTime::fromSecondSinceEpoch(toSecondSinceEpoch());}
|
|
void normalize() {*this = normalized();}
|
|
PIString toString(const PIString & format = "h:mm:ss d.MM.yyyy") const;
|
|
time_t toSecondSinceEpoch() const;
|
|
PISystemTime toSystemTime() const {return PISystemTime(int(toSecondSinceEpoch()), milliseconds * 1000000);}
|
|
void operator +=(const PIDateTime & d1) {year += d1.year; month += d1.month; day += d1.day; hours += d1.hours; minutes += d1.minutes; seconds += d1.seconds; normalize();}
|
|
void operator -=(const PIDateTime & d1) {year -= d1.year; month -= d1.month; day -= d1.day; hours -= d1.hours; minutes -= d1.minutes; seconds -= d1.seconds; normalize();}
|
|
static PIDateTime fromSecondSinceEpoch(const time_t sec);
|
|
static PIDateTime fromSystemTime(const PISystemTime & st) {PIDateTime dt = fromSecondSinceEpoch(st.seconds); dt.milliseconds = piClampi(st.nanoseconds / 1000000, 0, 999); return dt;}
|
|
};
|
|
inline PIDateTime operator +(const PIDateTime & d0, const PIDateTime & d1) {PIDateTime td = d0; td += d1; return td.normalized();}
|
|
inline PIDateTime operator -(const PIDateTime & d0, const PIDateTime & d1) {PIDateTime td = d0; td -= d1; return td.normalized();}
|
|
PIP_EXPORT bool operator ==(const PIDateTime & t0, const PIDateTime & t1);
|
|
PIP_EXPORT bool operator <(const PIDateTime & t0, const PIDateTime & t1);
|
|
PIP_EXPORT bool operator >(const PIDateTime & t0, const PIDateTime & t1);
|
|
inline bool operator !=(const PIDateTime & t0, const PIDateTime & t1) {return !(t0 == t1);}
|
|
inline bool operator <=(const PIDateTime & t0, const PIDateTime & t1) {return !(t0 > t1);}
|
|
inline bool operator >=(const PIDateTime & t0, const PIDateTime & t1) {return !(t0 < t1);}
|
|
|
|
PIP_EXPORT PITime currentTime();
|
|
PIP_EXPORT PIDate currentDate();
|
|
PIP_EXPORT PIDateTime currentDateTime();
|
|
|
|
//! \relatesalso PISystemTime \brief Returns current system time
|
|
PIP_EXPORT PISystemTime currentSystemTime();
|
|
PIP_EXPORT PIString time2string(const PITime & time, const PIString & format = "h:mm:ss"); // obsolete, use PITime.toString() instead
|
|
PIP_EXPORT PIString date2string(const PIDate & date, const PIString & format = "d.MM.yyyy"); // obsolete, use PITime.toString() instead
|
|
PIP_EXPORT PIString datetime2string(const PIDateTime & datetime, const PIString & format = "h:mm:ss d.MM.yyyy"); // obsolete, use PIDateTime.toString() instead
|
|
|
|
#endif // PITIME_H
|