From 8870cf48b63382d2fcaf68a0a7b0069c99a80272 Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Mon, 9 Sep 2002 22:46:09 +0000 Subject: [PATCH] NTP time stamp conversion --- src/libCom/osi/epicsTime.cpp | 87 +++++++++--------------------------- src/libCom/osi/epicsTime.h | 21 +++------ 2 files changed, 28 insertions(+), 80 deletions(-) diff --git a/src/libCom/osi/epicsTime.cpp b/src/libCom/osi/epicsTime.cpp index 6d4110afa..57f4d0e81 100644 --- a/src/libCom/osi/epicsTime.cpp +++ b/src/libCom/osi/epicsTime.cpp @@ -35,11 +35,6 @@ const unsigned epicsTime::nSecPerSec = 1000u*epicsTime::uSecPerSec; const unsigned epicsTime::nSecPerUSec = 1000u; const unsigned epicsTime::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 @@ -69,11 +64,7 @@ inline epicsTime::epicsTime (const unsigned long secIn, const unsigned long nSec class epicsTimeLoadTimeInit { public: epicsTimeLoadTimeInit (); - double epicsEpochOffset; // seconds -#ifdef NTP_SUPPORT - double ntpEpochOffset; // seconds -#endif double time_tSecPerTick; // seconds (both NTP and EPICS use int sec) unsigned long epicsEpochOffsetAsAnUnsignedLong; bool useDiffTimeOptimization; @@ -133,27 +124,6 @@ epicsTimeLoadTimeInit::epicsTimeLoadTimeInit () this->useDiffTimeOptimization = false; this->epicsEpochOffsetAsAnUnsignedLong = 0; } - -#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 } // @@ -420,52 +390,37 @@ epicsTime::epicsTime (const aitTimeStamp &ts) *this = epicsTime (this->secPastEpoch+secAdj, this->nSec+nSecAdj); } +// 631152000 (at posix epic) + 2272060800 (btw posix and epics epoch) + +// 15 ( leap seconds ) +static const unsigned long epicsEpocSecsPastEpochNTP = 2903212815; +static const double ULONG_MAX_PLUS_ONE = (static_cast(ULONG_MAX) + 1.0); + +struct l_fp { /* NTP time stamp */ + epicsUInt32 l_ui; /* sec past NTP epoch */ + epicsUInt32 l_uf; /* fractional seconds */ +}; + // -// epicsTime::ntpTimeStamp () +// epicsTime::l_fp () // -#ifdef NTP_SUPPORT -epicsTime::operator ntpTimeStamp () const +epicsTime::operator l_fp () 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 ); - + l_fp ts; + ts.l_ui = this->secPastEpoch + epicsEpocSecsPastEpochNTP; + ts.l_uf = static_cast < unsigned long > + ( ( this->nSec * ULONG_MAX_PLUS_ONE ) / nSecPerSec ); return ts; } -#endif // -// epicsTime::epicsTime (const ntpTimeStamp &ts) +// epicsTime::epicsTime ( const l_fp & ts ) // -#ifdef NTP_SUPPORT -epicsTime::epicsTime (const ntpTimeStamp &ts) +epicsTime::epicsTime ( const l_fp & 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 ); + this->secPastEpoch = ts.l_ui - epicsEpocSecsPastEpochNTP; + this->nSec = static_cast < unsigned long > + ( ( ts.l_uf / ULONG_MAX_PLUS_ONE ) * nSecPerSec ); } -#endif // return pointer to trailing "%0f" ( is a number) in a format string, // NULL if none; also return and a pointer to the "f" diff --git a/src/libCom/osi/epicsTime.h b/src/libCom/osi/epicsTime.h index 7f5bcb7c9..544097239 100644 --- a/src/libCom/osi/epicsTime.h +++ b/src/libCom/osi/epicsTime.h @@ -35,12 +35,7 @@ typedef struct epicsTimeStamp { struct timespec; /* POSIX real time */ struct timeval; /* BSD */ - -/* Network Time Protocal Timestamp */ -struct ntpTimeStamp { - epicsUInt32 l_ui; /* sec past NTP epoch */ - epicsUInt32 l_uf; /* fractional seconds */ -}; +struct l_fp; /* NTP timestamp */ #ifdef __cplusplus @@ -122,16 +117,16 @@ public: epicsTime & operator = ( const struct timeval & ); // convert to and from NTP timestamp format - operator ntpTimeStamp () const; - epicsTime ( const ntpTimeStamp & ); - epicsTime & operator = ( const ntpTimeStamp & ); + operator l_fp () const; + epicsTime ( const l_fp & ); + epicsTime & operator = ( const l_fp & ); // convert to and from GDDs aitTimeStamp format operator aitTimeStamp () const; epicsTime ( const aitTimeStamp & ); epicsTime & operator = ( const aitTimeStamp & ); - // convert to and from WIN32s FILETIME + // convert to and from WIN32s FILETIME (implemented only on WIN32) operator struct _FILETIME () const; epicsTime ( const struct _FILETIME & ); epicsTime & operator = ( const struct _FILETIME & ); @@ -336,13 +331,11 @@ inline epicsTime::operator epicsTimeStamp () const return ts; } -#ifdef NTP_SUPPORT -inline epicsTime epicsTime::operator = ( const ntpTimeStamp & rhs ) +inline epicsTime & epicsTime::operator = ( const l_fp & rhs ) { - *this = epicsTime (rhs); + *this = epicsTime ( rhs ); return *this; } -#endif inline epicsTime & epicsTime::operator = ( const time_t_wrapper & rhs ) {