diff --git a/src/libCom/osiTime.cc b/src/libCom/osiTime.cc index bde0c7176..a6b1ac602 100644 --- a/src/libCom/osiTime.cc +++ b/src/libCom/osiTime.cc @@ -6,7 +6,6 @@ #include #include -#include #define epicsExportSharedSymbols #include @@ -69,8 +68,8 @@ osiTime osiTime::getCurrent () // does not include header files that do define this // struct timespec { - time_t tv_sec; /* seconds */ - long tv_nsec; /* nanoseconds */ + unsigned long tv_sec; /* seconds */ + unsigned long tv_nsec; /* nanoseconds */ }; // @@ -79,7 +78,6 @@ struct timespec { osiTime::operator struct timespec () const { struct timespec ts; - assert (this->sec<=LONG_MAX-osiTime::epicsEpochSecPast1970); ts.tv_sec = this->sec + osiTime::epicsEpochSecPast1970; ts.tv_nsec = this->nSec; return ts; @@ -90,21 +88,43 @@ osiTime::operator struct timespec () const // osiTime::osiTime (const struct timespec &ts) { - assert (ts.tv_sec >= osiTime::epicsEpochSecPast1970); - this->sec = ((unsigned long)ts.tv_sec) - osiTime::epicsEpochSecPast1970; - assert (ts.tv_nsec>=0 && ts.tv_nsecnSec = (unsigned long) ts.tv_nsec; + if (ts.tv_sec >= osiTime::epicsEpochSecPast1970) { + this->sec = ts.tv_sec - osiTime::epicsEpochSecPast1970; + } + else { + this->sec = 0; + } + + if (ts.tv_nsecnSec = ts.tv_nsec; + } + else { + this->sec += ts.tv_nsec / osiTime::nSecPerSec; + this->nSec = ts.tv_nsec % osiTime::nSecPerSec; + } } // // operator = (const struct timespec &rhs) // -osiTime::operator = (const struct timespec &rhs) +osiTime osiTime::operator = (const struct timespec &rhs) { - assert (rhs.tv_sec >= osiTime::epicsEpochSecPast1970); - this->sec = ((unsigned long)rhs.tv_sec) - osiTime::epicsEpochSecPast1970; - assert (rhs.tv_nsec>=0 && rhs.tv_nsecnSec = (unsigned long) rhs.tv_nsec; + if (rhs.tv_sec >= osiTime::epicsEpochSecPast1970) { + this->sec = rhs.tv_sec - osiTime::epicsEpochSecPast1970; + } + else { + this->sec = 0; + } + + if (rhs.tv_nsecnSec = rhs.tv_nsec; + } + else { + this->sec += rhs.tv_nsec / osiTime::nSecPerSec; + this->nSec = rhs.tv_nsec % osiTime::nSecPerSec; + } + + return *this; } // @@ -124,7 +144,6 @@ public: osiTime::operator aitTimeStamp () const { aitTimeStamp ts; - assert (this->sec<=ULONG_MAX-osiTime::epicsEpochSecPast1970); ts.tv_sec = this->sec + osiTime::epicsEpochSecPast1970; ts.tv_nsec = this->nSec; return ts; @@ -135,21 +154,41 @@ osiTime::operator aitTimeStamp () const // osiTime::osiTime (const aitTimeStamp &ts) { - assert (ts.tv_sec >= osiTime::epicsEpochSecPast1970); - this->sec = ts.tv_sec - osiTime::epicsEpochSecPast1970; - assert (ts.tv_nsecnSec = ts.tv_nsec; + if (ts.tv_sec >= osiTime::epicsEpochSecPast1970) { + this->sec = ts.tv_sec - osiTime::epicsEpochSecPast1970; + } + else { + this->sec = 0; + } + + if (ts.tv_nsecnSec = ts.tv_nsec; + } + else { + this->sec += ts.tv_nsec / osiTime::nSecPerSec; + this->nSec = ts.tv_nsec % osiTime::nSecPerSec; + } } // // operator = (const aitTimeStamp &rhs) // -osiTime::operator = (const aitTimeStamp &rhs) +osiTime osiTime::operator = (const aitTimeStamp &rhs) { - assert (rhs.tv_sec >= osiTime::epicsEpochSecPast1970); - this->sec = rhs.tv_sec - osiTime::epicsEpochSecPast1970; - assert (rhs.tv_nsecnSec = rhs.tv_nsec; + if (rhs.tv_sec >= osiTime::epicsEpochSecPast1970) { + this->sec = rhs.tv_sec - osiTime::epicsEpochSecPast1970; + } + else { + this->sec = 0; + } + if (rhs.tv_nsecnSec = rhs.tv_nsec; + } + else { + this->sec += rhs.tv_nsec / osiTime::nSecPerSec; + this->nSec = rhs.tv_nsec % osiTime::nSecPerSec; + } + return *this; } // @@ -175,10 +214,11 @@ osiTime::osiTime (const struct TS_STAMP &ts) // // operator = (const TS_STAMP &rhs) // -osiTime::operator = (const struct TS_STAMP &rhs) +osiTime osiTime::operator = (const struct TS_STAMP &rhs) { this->sec = rhs.secPastEpoch; this->nSec = rhs.nsec; + return *this; } // @@ -210,7 +250,7 @@ void osiTime::show (unsigned) const // osiTime osiTime::operator - (const osiTime &rhs) const { - unsigned long nSec, sec; + unsigned long nSecRes, secRes; if (this->secsec < ULONG_MAX/2) { @@ -229,12 +269,12 @@ osiTime osiTime::operator - (const osiTime &rhs) const // 69 years assume that the seconds counter has rolled // over and compute the "wrap around" difference // - sec = 1 + this->sec + (ULONG_MAX - rhs.sec); + secRes = 1 + this->sec + (ULONG_MAX - rhs.sec); } } else { - sec = this->sec - rhs.sec; - if (sec > ULONG_MAX/2) { + secRes = this->sec - rhs.sec; + if (secRes > ULONG_MAX/2) { // // assume that someone adjusted the time // backwards slightly. This would happen if a high @@ -246,16 +286,16 @@ osiTime osiTime::operator - (const osiTime &rhs) const } if (this->nSec>=rhs.nSec) { - nSec = this->nSec - rhs.nSec; + nSecRes = this->nSec - rhs.nSec; } else { - if (sec>0) { + if (secRes>0) { // // Borrow // - nSec = this->nSec + (osiTime::nSecPerSec - rhs.nSec); - sec--; + nSecRes = this->nSec + (osiTime::nSecPerSec - rhs.nSec); + secRes--; } else { // @@ -268,7 +308,7 @@ osiTime osiTime::operator - (const osiTime &rhs) const return osiTime (); } } - return osiTime (sec, nSec); + return osiTime (secRes, nSecRes); } // diff --git a/src/libCom/osiTime.h b/src/libCom/osiTime.h index 7114867fa..89974ba18 100644 --- a/src/libCom/osiTime.h +++ b/src/libCom/osiTime.h @@ -29,6 +29,9 @@ * * History * $Log$ + * Revision 1.9 1999/05/07 19:12:39 jhill + * many improvements to osiTime + * * Revision 1.8 1999/05/03 16:22:29 jhill * allow osiTime to convert toaitTimeStamp without binding to gdd * @@ -135,21 +138,21 @@ public: // operator struct TS_STAMP () const; osiTime (const struct TS_STAMP &ts); - operator = (const struct TS_STAMP &rhs); + osiTime operator = (const struct TS_STAMP &rhs); // // convert to and from GDD's aitTimeStamp format // operator aitTimeStamp () const; osiTime (const aitTimeStamp &ts); - operator = (const aitTimeStamp &rhs); + osiTime operator = (const aitTimeStamp &rhs); // // convert to and from POSIX RT's "struct timespec" // operator struct timespec () const; osiTime (const struct timespec &ts); - operator = (const struct timespec &rhs); + osiTime operator = (const struct timespec &rhs); // // arithmetic operators