NTP time stamp conversion

This commit is contained in:
Jeff Hill
2002-09-09 22:46:09 +00:00
parent 849b5e094e
commit 8870cf48b6
2 changed files with 28 additions and 80 deletions
+21 -66
View File
@@ -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<double>(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<long> (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<double>(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<unsigned long> (lti.ntpEpochOffset);
// underflow expected
ts.l_ui = this->secPastEpoch - offset;
}
else {
unsigned long offset = static_cast<unsigned long> (-lti.ntpEpochOffset);
// overflow expected
ts.l_ui = this->secPastEpoch + offset;
}
ts.l_uf = static_cast<unsigned long> ( ( 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<unsigned long> (lti.ntpEpochOffset);
// overflow expected
this->secPastEpoch = ts.l_ui + this->secPastEpoch + offset;
}
else {
unsigned long offset = static_cast<unsigned long> (-lti.ntpEpochOffset);
// underflow expected
this->secPastEpoch = ts.l_ui + this->secPastEpoch - offset;
}
this->nSec = static_cast<unsigned long> ( ( 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 "%0<n>f" (<n> is a number) in a format string,
// NULL if none; also return <n> and a pointer to the "f"
+7 -14
View File
@@ -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 )
{