/* * $Id$ * * Author Jeffrey O. Hill * johill@lanl.gov * 505 665 1831 * * Experimental Physics and Industrial Control System (EPICS) * * Copyright 1991, the Regents of the University of California, * and the University of Chicago Board of Governors. * * This software was produced under U.S. Government contracts: * (W-7405-ENG-36) at the Los Alamos National Laboratory, * and (W-31-109-ENG-38) at Argonne National Laboratory. * * Initial development by: * The Controls and Automation Group (AT-8) * Ground Test Accelerator * Accelerator Technology Division * Los Alamos National Laboratory * * Co-developed with * The Controls and Computing Group * Accelerator Systems Division * Advanced Photon Source * Argonne National Laboratory * */ #include #include #include #include #include #define epicsExportSharedSymbols #include "locationException.h" #include "epicsAssert.h" #include "envDefs.h" #include "osiTime.h" #include "tsStamp.h" // // useful public constants // const unsigned osiTime::mSecPerSec = 1000u; const unsigned osiTime::uSecPerSec = 1000u*osiTime::mSecPerSec; const unsigned osiTime::nSecPerSec = 1000u*osiTime::uSecPerSec; const unsigned osiTime::nSecPerUSec = 1000u; const unsigned osiTime::secPerMin = 60u; static const unsigned ntpEpochYear = 1900; static const unsigned ntpEpocMonth = 0; // January static const unsigned ntpEpocDayOfTheMonth = 1; // the 1st day of the month static const double ULONG_MAX_PLUS_ONE = (static_cast(ULONG_MAX) + 1.0); // // force this module to include code that can convert // to GDD's aitTimeStamp, but dont require that it must // link with gdd. Therefore, gdd.h is not included here. // class aitTimeStamp { public: unsigned long tv_sec; unsigned long tv_nsec; }; static const unsigned tmStructEpochYear = 1900; static const unsigned epicsEpochYear = 1990; static const unsigned epicsEpocMonth = 0; // January static const unsigned epicsEpocDayOfTheMonth = 1; // the 1st day of the month // // forward declarations for utility routines // static char *fracFormat (const char *pFormat, unsigned long *width); // // osiTime (const unsigned long secIn, const unsigned long nSecIn) // inline osiTime::osiTime (const unsigned long secIn, const unsigned long nSecIn) : secPastEpoch ( nSecIn / nSecPerSec + secIn ), nSec ( nSecIn % nSecPerSec ) {} // // osiTimeLoadTimeInit // class osiTimeLoadTimeInit { public: osiTimeLoadTimeInit (); double epicsEpochOffset; // seconds #ifdef NTP_SUPPORT double ntpEpochOffset; // seconds #endif double time_tSecPerTick; // seconds (both NTP and EPICS use int sec) }; static const osiTimeLoadTimeInit lti; // // osiTimeLoadTimeInit () // osiTimeLoadTimeInit::osiTimeLoadTimeInit () { static const time_t ansiEpoch = 0; double secWest; { time_t current = time (NULL); time_t error; tm date; gmtime_r (¤t, &date); error = mktime (&date); assert (error!=(time_t)-1); secWest = difftime (error, current); } { time_t first = static_cast (0); time_t last = static_cast (1); this->time_tSecPerTick = difftime (last, first); } { struct tm tmEpicsEpoch; time_t epicsEpoch; tmEpicsEpoch.tm_sec = 0; tmEpicsEpoch.tm_min = 0; tmEpicsEpoch.tm_hour = 0; tmEpicsEpoch.tm_mday = epicsEpocDayOfTheMonth; tmEpicsEpoch.tm_mon = epicsEpocMonth; tmEpicsEpoch.tm_year = epicsEpochYear-tmStructEpochYear; tmEpicsEpoch.tm_isdst = -1; // dont know if its daylight savings time epicsEpoch = mktime (&tmEpicsEpoch); assert (epicsEpoch!=(time_t)-1); this->epicsEpochOffset = difftime (epicsEpoch, ansiEpoch) - secWest; } #ifdef NTP_SUPPORT /* unfortunately, on NT mktime cant calculate a time_t for a date before 1970 */ { struct tm tmEpochNTP; time_t ntpEpoch; tmEpochNTP.tm_sec = 0; tmEpochNTP.tm_min = 0; tmEpochNTP.tm_hour = 0; tmEpochNTP.tm_mday = ntpEpocDayOfTheMonth; tmEpochNTP.tm_mon = ntpEpocMonth; tmEpochNTP.tm_year = ntpEpochYear-tmStructEpochYear; tmEpochNTP.tm_isdst = -1; // dont know if its daylight savings time ntpEpoch = mktime (&tmEpochNTP); assert (ntpEpoch!=(time_t)-1); this->ntpEpochOffset = static_cast (difftime (ansiEpoch, ntpEpoch) + this->epicsEpochOffset - secWest); } #endif } // // osiTime::addNanoSec () // // many of the UNIX timestamp formats have nano sec stored as a long // inline void osiTime::addNanoSec (long nSecAdj) { double secAdj = static_cast (nSecAdj) / nSecPerSec; *this += secAdj; } // // osiTime (const time_t_wrapper &tv) // osiTime::osiTime (const time_t_wrapper &ansiTimeTicks) { static double uLongMax = static_cast (ULONG_MAX); double sec; // // map time_t, which ansi C defines as some arithmetic type, into type double // sec = ansiTimeTicks.ts * lti.time_tSecPerTick - lti.epicsEpochOffset; // // map into the the EPICS time stamp range (which allows rollover) // if (sec < 0.0) { if (sec < -uLongMax) { sec = sec + static_cast(-sec/uLongMax)*uLongMax; } sec += uLongMax; } else if (sec > uLongMax) { sec = sec - static_cast(sec/uLongMax)*uLongMax; } this->secPastEpoch = static_cast (sec); this->nSec = static_cast ( (sec-this->secPastEpoch) * nSecPerSec ); } // // operator time_t_wrapper () // osiTime::operator time_t_wrapper () const { double tmp; time_t_wrapper wrap; tmp = (this->secPastEpoch + lti.epicsEpochOffset) / lti.time_tSecPerTick; tmp += (this->nSec / lti.time_tSecPerTick) / nSecPerSec; // // map type double into time_t which ansi C defines as some arithmetic type // wrap.ts = static_cast (tmp); return wrap; } // // convert to and from ANSI C struct tm (with nano seconds) // osiTime::operator tm_nano_sec () const { tm_nano_sec tm; time_t_wrapper ansiTimeTicks; ansiTimeTicks = *this; // // reentrant version of localtime() - from POSIX RT // // WRS returns int and others return &tm.ansi_tm on // succes? // localtime_r (&ansiTimeTicks.ts, &tm.ansi_tm); tm.nSec = this->nSec; return tm; } // // osiTime (const tm_nano_sec &tm) // osiTime::osiTime (const tm_nano_sec &tm) { static const time_t mktimeFailure = static_cast (-1); time_t_wrapper ansiTimeTicks; struct tm tmp = tm.ansi_tm; ansiTimeTicks.ts = mktime (&tmp); if (ansiTimeTicks.ts == mktimeFailure) { throwWithLocation ( formatProblemWithStructTM () ); } *this = osiTime (ansiTimeTicks); unsigned long nSecAdj = tm.nSec % nSecPerSec; unsigned long secAdj = tm.nSec / nSecPerSec; *this = osiTime (this->secPastEpoch+secAdj, this->nSec+nSecAdj); } // // operator struct timespec () // osiTime::operator struct timespec () const { struct timespec ts; time_t_wrapper ansiTimeTicks; ansiTimeTicks = *this; ts.tv_sec = ansiTimeTicks.ts; ts.tv_nsec = static_cast (this->nSec); return ts; } // // osiTime (const struct timespec &ts) // osiTime::osiTime (const struct timespec &ts) { time_t_wrapper ansiTimeTicks; ansiTimeTicks.ts = ts.tv_sec; *this = osiTime (ansiTimeTicks); this->addNanoSec (ts.tv_nsec); } // // operator struct timeval () // osiTime::operator struct timeval () const { struct timeval ts; time_t_wrapper ansiTimeTicks; ansiTimeTicks = *this; ts.tv_sec = ansiTimeTicks.ts; ts.tv_usec = static_cast (this->nSec / nSecPerUSec); return ts; } // // osiTime (const struct timeval &ts) // osiTime::osiTime (const struct timeval &ts) { time_t_wrapper ansiTimeTicks; ansiTimeTicks.ts = ts.tv_sec; *this = osiTime (ansiTimeTicks); this->addNanoSec (ts.tv_usec * nSecPerUSec); } // // operator aitTimeStamp () // osiTime::operator aitTimeStamp () const { aitTimeStamp ts; time_t_wrapper ansiTimeTicks; ansiTimeTicks = *this; ts.tv_sec = ansiTimeTicks.ts; ts.tv_nsec = this->nSec; return ts; } // // osiTime (const aitTimeStamp &ts) // osiTime::osiTime (const aitTimeStamp &ts) { time_t_wrapper ansiTimeTicks; ansiTimeTicks.ts = ts.tv_sec; *this = osiTime (ansiTimeTicks); unsigned long secAdj = ts.tv_nsec / nSecPerSec; unsigned long nSecAdj = ts.tv_nsec % nSecPerSec; *this = osiTime (this->secPastEpoch+secAdj, this->nSec+nSecAdj); } // // osiTime::ntpTimeStamp () // #ifdef NTP_SUPPORT osiTime::operator ntpTimeStamp () const { ntpTimeStamp ts; if (lti.ntpEpochOffset>=0) { unsigned long offset = static_cast (lti.ntpEpochOffset); // underflow expected ts.l_ui = this->secPastEpoch - offset; } else { unsigned long offset = static_cast (-lti.ntpEpochOffset); // overflow expected ts.l_ui = this->secPastEpoch + offset; } ts.l_uf = static_cast ( ( this->nSec * ULONG_MAX_PLUS_ONE ) / nSecPerSec ); return ts; } #endif // // osiTime::osiTime (const ntpTimeStamp &ts) // #ifdef NTP_SUPPORT osiTime::osiTime (const ntpTimeStamp &ts) { if (lti.ntpEpochOffset>=0) { unsigned long offset = static_cast (lti.ntpEpochOffset); // overflow expected this->secPastEpoch = ts.l_ui + this->secPastEpoch + offset; } else { unsigned long offset = static_cast (-lti.ntpEpochOffset); // underflow expected this->secPastEpoch = ts.l_ui + this->secPastEpoch - offset; } this->nSec = static_cast ( ( ts.l_uf / ULONG_MAX_PLUS_ONE ) * nSecPerSec ); } #endif // // size_t osiTime::strftime (char *pBuff, size_t bufLength, const char *pFormat) // size_t osiTime::strftime (char *pBuff, size_t bufLength, const char *pFormat) const { // copy format (needs to be writable) char format[256]; strcpy (format, pFormat); // look for "%0f" at the end (used for fractional second formatting) unsigned long fracWid; char *fracPtr = fracFormat (format, &fracWid); // if found, hide from strftime if (fracPtr != NULL) *fracPtr = '\0'; // format all but fractional seconds tm_nano_sec tmns = *this; size_t numChar = ::strftime (pBuff, bufLength, format, &tmns.ansi_tm); if (numChar == 0 || fracPtr == NULL) return numChar; // check there are enough chars for the fractional seconds if (numChar + fracWid < bufLength) { // divisors for fraction (see below) const int div[9+1] = {(int)1e9,(int)1e8,(int)1e7,(int)1e6,(int)1e5, (int)1e4,(int)1e3,(int)1e2,(int)1e1,(int)1e0}; // round and convert nanosecs to integer of correct range int ndp = fracWid <= 9 ? fracWid : 9; int frac = ((tmns.nSec + div[ndp]/2) % ((int)1e9)) / div[ndp]; // restore fractional format and format fraction *fracPtr = '%'; *(format + strlen (format) - 1) = 'u'; sprintf (pBuff+numChar, fracPtr, frac); } return numChar + fracWid; } // // osiTime::show (unsigned) // void osiTime::show (unsigned) const { char bigBuffer[256]; size_t numChar = strftime (bigBuffer, sizeof(bigBuffer), "%a %b %d %Y %H:%M:%S.%09f"); if (numChar>0) { printf ("osiTime: %s\n", bigBuffer); } } // // osiTime::operator + (const double &rhs) // // rhs has units seconds // osiTime osiTime::operator + (const double &rhs) const { unsigned long newSec, newNSec, secOffset, nSecOffset; double fnsec; if (rhs >= 0) { secOffset = static_cast (rhs); fnsec = rhs - secOffset; nSecOffset = static_cast (fnsec * nSecPerSec); newSec = this->secPastEpoch + secOffset; // overflow expected newNSec = this->nSec + nSecOffset; if (newNSec >= nSecPerSec) { newSec++; // overflow expected newNSec -= nSecPerSec; } } else { secOffset = static_cast (-rhs); fnsec = rhs + secOffset; nSecOffset = static_cast (-fnsec * nSecPerSec); newSec = this->secPastEpoch - secOffset; // underflow expected if (this->nSec>=nSecOffset) { newNSec = this->nSec - nSecOffset; } else { // borrow newSec--; // underflow expected newNSec = this->nSec + (nSecPerSec - nSecOffset); } } return osiTime (newSec, newNSec); } // // operator - // // To make this code robust during timestamp rollover events // time stamp differences greater than one half full scale are // interpreted as rollover situations: // // when RHS is greater than THIS: // RHS-THIS > one half full scale => return THIS + (ULONG_MAX-RHS) // RHS-THIS <= one half full scale => return -(RHS-THIS) // // when THIS is greater than or equal to RHS // THIS-RHS > one half full scale => return -(RHS + (ULONG_MAX-THIS)) // THIS-RHS <= one half full scale => return THIS-RHS // double osiTime::operator - (const osiTime &rhs) const { double nSecRes, secRes; // // first compute the difference between the nano-seconds members // // nano sec member is not allowed to be greater that 1/2 full scale // so the unsigned to signed conversion is ok // if (this->nSec>=rhs.nSec) { nSecRes = this->nSec - rhs.nSec; } else { nSecRes = rhs.nSec - this->nSec; nSecRes = -nSecRes; } // // next compute the difference between the seconds memebers // and invert the sign of the nano seconds result if there // is a range violation // if (this->secPastEpochsecPastEpoch; if (secRes > ULONG_MAX/2) { // // In this situation where the difference is more than // 68 years assume that the seconds counter has rolled // over and compute the "wrap around" difference // secRes = 1 + (ULONG_MAX-secRes); nSecRes = -nSecRes; } else { secRes = -secRes; } } else { secRes = this->secPastEpoch - rhs.secPastEpoch; if (secRes > ULONG_MAX/2) { // // In this situation where the difference is more than // 68 years assume that the seconds counter has rolled // over and compute the "wrap around" difference // secRes = 1 + (ULONG_MAX-secRes); secRes = -secRes; nSecRes = -nSecRes; } } return secRes + nSecRes/nSecPerSec; } // // operator <= // bool osiTime::operator <= (const osiTime &rhs) const { bool rc; if (this->secPastEpochsecPastEpoch < ULONG_MAX/2) { // // In this situation where the difference is less than // 69 years compute the expected result // rc = true; } else { // // In this situation where the difference is more than // 69 years assume that the seconds counter has rolled // over and compute the "wrap around" result // rc = false; } } else if (this->secPastEpoch>rhs.secPastEpoch) { if (this->secPastEpoch-rhs.secPastEpoch < ULONG_MAX/2) { // // In this situation where the difference is less than // 69 years compute the expected result // rc = false; } else { // // In this situation where the difference is more than // 69 years assume that the seconds counter has rolled // over and compute the "wrap around" result // rc = true; } } else { if (this->nSec<=rhs.nSec) { rc = true; } else { rc = false; } } return rc; } // // operator < // bool osiTime::operator < (const osiTime &rhs) const { bool rc; if (this->secPastEpochsecPastEpoch < ULONG_MAX/2) { // // In this situation where the difference is less than // 69 years compute the expected result // rc = true; } else { // // In this situation where the difference is more than // 69 years assume that the seconds counter has rolled // over and compute the "wrap around" result // rc = false; } } else if (this->secPastEpoch>rhs.secPastEpoch) { if (this->secPastEpoch-rhs.secPastEpoch < ULONG_MAX/2) { // // In this situation where the difference is less than // 69 years compute the expected result // rc = false; } else { // // In this situation where the difference is more than // 69 years assume that the seconds counter has rolled // over and compute the "wrap around" result // rc = true; } } else { if (this->nSec osiTime (*pRight); } epicsShareFunc int epicsShareAPI tsStampGreaterThanEqual (const TS_STAMP *pLeft, const TS_STAMP *pRight) { return osiTime (*pLeft) >= osiTime (*pRight); } epicsShareFunc size_t epicsShareAPI tsStampToStrftime (char *pBuff, size_t bufLength, const char *pFormat, const TS_STAMP *pTS) { return osiTime(*pTS).strftime (pBuff, bufLength, pFormat); } epicsShareFunc void epicsShareAPI tsStampShow (const TS_STAMP *pTS, unsigned interestLevel) { osiTime(*pTS).show (interestLevel); } } // // utility routines // // return pointer to trailing "%0f" ( is a number) in a format string, // NULL if none; also return and a pointer to the "f" static char *fracFormat (const char *pFormat, unsigned long *width) { // initialize returned field width *width = 0; // point at char past format string const char *ptr = pFormat + strlen (pFormat); // if (last) char not 'f', no match if (*(--ptr) != 'f') return NULL; // skip digits, extracting (must start with 0) while (isdigit (*(--ptr))); // NB, empty loop body ++ptr; // points to first digit, if any if (*ptr != '0') return NULL; if (sscanf (ptr, "%lu", width) != 1) return NULL; // if (prev) char not '%', no match if (*(--ptr) != '%') return NULL; // return pointer to '%' return (char *) ptr; }