From 9bc2ceaa06bef9e407140587ea1fb94df6e83af2 Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Mon, 10 May 1999 23:38:33 +0000 Subject: [PATCH] convert to and from other time stamp formats --- src/gdd/aitHelpers.cc | 158 ++++++++++++++++++++++++++++++++++++++---- src/gdd/aitHelpers.h | 55 ++++++++------- 2 files changed, 174 insertions(+), 39 deletions(-) diff --git a/src/gdd/aitHelpers.cc b/src/gdd/aitHelpers.cc index 2453e4bfb..4909299d2 100644 --- a/src/gdd/aitHelpers.cc +++ b/src/gdd/aitHelpers.cc @@ -5,6 +5,9 @@ // $Id$ // // $Log$ +// Revision 1.10 1999/05/03 16:20:51 jhill +// allow aitTimeStamp to convert to TS_STAMP (without binding to libCom) +// // Revision 1.9 1998/05/05 21:08:26 jhill // fixed warning // @@ -37,18 +40,6 @@ #define epicsExportSharedSymbols #include "aitTypes.h" - -// -// force this module to include code that can convert -// to an EPICS time stamp, but dont force this library -// to link with libCom -// -#define INC_tsDefs_h -typedef struct { - aitUint32 secPastEpoch; /* seconds since 0000 Jan 1, 1990 */ - aitUint32 nsec; /* nanoseconds within second */ -} TS_STAMP; - #include "aitHelpers.h" // @@ -187,4 +178,145 @@ int aitString::init(const char* p, aitStrType typeIn, unsigned strLengthIn, unsi break; } return rc; -} \ No newline at end of file +} + +// +// allow this module to include code that can convert +// to an EPICS time stamp, but dont force this library +// to link with libCom +// +struct TS_STAMP { + aitUint32 secPastEpoch; /* seconds since 0000 Jan 1, 1990 */ + aitUint32 nsec; /* nanoseconds within second */ +}; + +aitTimeStamp::operator struct TS_STAMP () const +{ + TS_STAMP ts; + + if (this->tv_sec>aitTimeStamp::epicsEpochSecPast1970) { + ts.secPastEpoch = this->tv_sec - aitTimeStamp::epicsEpochSecPast1970; + ts.nsec = this->tv_nsec; + } + else { + ts.secPastEpoch = 0; + ts.nsec = 0; + } + return ts; +} + +void aitTimeStamp::get (struct TS_STAMP &ts) const +{ + if (this->tv_sec>aitTimeStamp::epicsEpochSecPast1970) { + ts.secPastEpoch = this->tv_sec - aitTimeStamp::epicsEpochSecPast1970; + ts.nsec = this->tv_nsec; + } + else { + ts.secPastEpoch = 0; + ts.nsec = 0; + } +} + +aitTimeStamp::aitTimeStamp (const struct TS_STAMP &ts) +{ + this->tv_sec = ts.secPastEpoch + aitTimeStamp::epicsEpochSecPast1970; + this->tv_nsec = ts.nsec; +} + +aitTimeStamp aitTimeStamp::operator = (const struct TS_STAMP &rhs) +{ + this->tv_sec = rhs.secPastEpoch + aitTimeStamp::epicsEpochSecPast1970; + this->tv_nsec = rhs.nsec; + return *this; +} + +// +// allow this module to include code that can convert +// to an EPICS time stamp, but dont force this library +// to link with libCom +// +class osiTime { +public: + unsigned long sec; /* seconds since 0000 Jan 1, 1990 */ + unsigned long nSec; /* nanoseconds within second */ +}; + +aitTimeStamp::operator osiTime () const +{ + osiTime ts; + + if (this->tv_sec>aitTimeStamp::epicsEpochSecPast1970) { + ts.sec = this->tv_sec - aitTimeStamp::epicsEpochSecPast1970; + ts.nSec = this->tv_nsec; + } + else { + ts.sec = 0; + ts.nSec = 0; + } + return ts; +} + +void aitTimeStamp::get (osiTime &ts) const +{ + if (this->tv_sec>aitTimeStamp::epicsEpochSecPast1970) { + ts.sec = this->tv_sec - aitTimeStamp::epicsEpochSecPast1970; + ts.nSec = this->tv_nsec; + } + else { + ts.sec = 0; + ts.nSec = 0; + } +} + +aitTimeStamp::aitTimeStamp (const osiTime &ts) +{ + this->tv_sec = ts.sec + aitTimeStamp::epicsEpochSecPast1970; + this->tv_nsec = ts.nSec; +} + +aitTimeStamp aitTimeStamp::operator = (const osiTime &rhs) +{ + this->tv_sec = rhs.sec + aitTimeStamp::epicsEpochSecPast1970; + this->tv_nsec = rhs.nSec; + return *this; +} + +// +// allow this module to include code that can convert +// to and from a POSIX timespec, but allow this library +// to compile on systems that dont support it +// +struct timespec +{ + unsigned long tv_sec; + unsigned long tv_nsec; +}; + +aitTimeStamp::operator struct timespec () const +{ + struct timespec ts; + ts.tv_sec = (unsigned long) this->tv_sec; + ts.tv_nsec = (unsigned long) this->tv_nsec; + return ts; +} + +void aitTimeStamp::get (struct timespec &ts) const +{ + ts.tv_sec = (unsigned long) this->tv_sec; + ts.tv_nsec = (unsigned long) this->tv_nsec; +} + +aitTimeStamp::aitTimeStamp (const struct timespec &ts) +{ + this->tv_sec = (aitUint32) ts.tv_sec; + this->tv_nsec = (aitUint32) ts.tv_nsec; +} + +aitTimeStamp aitTimeStamp::operator = (const struct timespec &rhs) +{ + this->tv_sec = (aitUint32) rhs.tv_sec; + this->tv_nsec = (aitUint32) rhs.tv_nsec; + return *this; +} + + diff --git a/src/gdd/aitHelpers.h b/src/gdd/aitHelpers.h index 4200f667a..d3b34b755 100644 --- a/src/gdd/aitHelpers.h +++ b/src/gdd/aitHelpers.h @@ -8,6 +8,9 @@ * $Id$ * * $Log$ + * Revision 1.18 1999/05/03 16:20:51 jhill + * allow aitTimeStamp to convert to TS_STAMP (without binding to libCom) + * * Revision 1.17 1999/04/30 00:09:47 jhill * proper borrow * @@ -89,10 +92,16 @@ inline char* strDup(const char* x) return y; } +struct timespec; +struct TS_STAMP; +class osiTime; +class gdd; + class epicsShareClass aitTimeStamp { friend inline aitTimeStamp operator+ (const aitTimeStamp &lhs, const aitTimeStamp &rhs); friend inline aitTimeStamp operator- (const aitTimeStamp &lhs, const aitTimeStamp &rhs); friend inline int operator>= (const aitTimeStamp &lhs, const aitTimeStamp &rhs); + friend class gdd; public: aitTimeStamp () : tv_sec(0u), tv_nsec(0u) {} aitTimeStamp (const aitTimeStamp &t) : tv_sec(t.tv_sec), tv_nsec(t.tv_nsec) {} @@ -135,41 +144,35 @@ public: return ((float)this->tv_nsec)/NSecPerSec+this->tv_sec; } + // + // convert to and from POSIX timespec format + // + operator struct timespec () const; + void get (struct timespec &) const; + aitTimeStamp (const struct timespec &ts); + aitTimeStamp operator = (const struct timespec &rhs); + // // convert to and from EPICS TS_STAMP format // - // include tsDefs.h prior to including osiTime.h - // if you need these capabilities + operator struct TS_STAMP () const; + void get (struct TS_STAMP &) const; + aitTimeStamp (const struct TS_STAMP &ts); + aitTimeStamp operator = (const struct TS_STAMP &rhs); + // -#ifdef INC_tsDefs_h - operator TS_STAMP () const - { - TS_STAMP ts; - assert (this->tv_sec>=aitTimeStamp::epicsEpochSecPast1970); - ts.secPastEpoch = this->tv_sec - aitTimeStamp::epicsEpochSecPast1970; - ts.nsec = this->tv_nsec; - return ts; - } - - aitTimeStamp (const TS_STAMP &ts) - { - this->tv_sec = ts.secPastEpoch + aitTimeStamp::epicsEpochSecPast1970; - this->tv_nsec = ts.nsec; - } - - operator = (const TS_STAMP &rhs) - { - this->tv_sec = rhs.secPastEpoch + aitTimeStamp::epicsEpochSecPast1970; - this->tv_nsec = rhs.nsec; - } -#endif + // convert to and from EPICS osiTime format + // + operator osiTime () const; + void get (osiTime &) const; + aitTimeStamp (const osiTime &ts); + aitTimeStamp operator = (const osiTime &rhs); static aitTimeStamp getCurrent(); -// private: +private: unsigned long tv_sec; unsigned long tv_nsec; -private: static const unsigned epicsEpochSecPast1970; };