epicsTime: rework
Re-implement around epicsTimeStamp (C API) with class epicsTime becoming a wrapper. Prefer epicsInt64 arithmetic. Remove opaque struct l_fp (NTP time conversion)
This commit is contained in:
1 parent
7c991f3f2a
commit
cb8c7998b6
4 files changed
+676
-1256
No files matched your search
File diff suppressed because it is too large.
Load diff
+337
-362
@@ -26,6 +26,14 @@
|
||||
/** \brief The EPICS Epoch is 00:00:00 Jan 1, 1990 UTC */
|
||||
#define POSIX_TIME_AT_EPICS_EPOCH 631152000u
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include <stdexcept>
|
||||
#include <ostream>
|
||||
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \brief EPICS time stamp, for use from C code.
|
||||
*
|
||||
* Because it uses an unsigned 32-bit integer to hold the seconds count, an
|
||||
@@ -65,277 +73,6 @@ struct timespec; /* POSIX real time */
|
||||
*/
|
||||
struct timeval; /* BSD */
|
||||
|
||||
/** \struct l_fp
|
||||
* \brief Network Time Protocol timestamp
|
||||
*
|
||||
* Network Time Protocol timestamp. The fields are:
|
||||
* \li \c lui - Number of seconds since 1900 (The NTP epoch)
|
||||
* \li \c luf - Fraction of a second. For example 0x800000000 represents 1/2 second.
|
||||
*/
|
||||
struct l_fp; /* NTP timestamp */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
/** \brief C++ only ANSI C <tt>struct tm</tt> with nanoseconds, local timezone
|
||||
*
|
||||
* Extend ANSI C "struct tm" to include nano seconds within a second
|
||||
* and a struct tm that is adjusted for the local timezone.
|
||||
*/
|
||||
struct local_tm_nano_sec {
|
||||
struct tm ansi_tm; /**< \brief ANSI C time details */
|
||||
unsigned long nSec; /**< \brief nanoseconds extension */
|
||||
};
|
||||
|
||||
/** \brief C++ only ANSI C <tt>sruct tm</tt> with nanoseconds, UTC
|
||||
*
|
||||
* Extend ANSI C "struct tm" to include nanoseconds within a second
|
||||
* and a struct tm that is adjusted for GMT (UTC).
|
||||
*/
|
||||
struct gm_tm_nano_sec {
|
||||
struct tm ansi_tm; /**< \brief ANSI C time details */
|
||||
unsigned long nSec; /**< \brief nanoseconds extension */
|
||||
};
|
||||
|
||||
/** \brief C++ only ANSI C time_t
|
||||
*
|
||||
* This is for converting to/from the ANSI C \c time_t. Since \c time_t
|
||||
* is usually an elementary type providing a conversion operator from
|
||||
* \c time_t to/from epicsTime could cause undesirable implicit
|
||||
* conversions. Providing a conversion operator to/from the
|
||||
* \c time_t_wrapper instead prevents implicit conversions.
|
||||
*/
|
||||
struct time_t_wrapper {
|
||||
time_t ts;
|
||||
};
|
||||
|
||||
/** \brief C++ Event number wrapper class
|
||||
*
|
||||
* Stores an event number for use by the epicsTime::getEvent() static
|
||||
* class method.
|
||||
*/
|
||||
class LIBCOM_API epicsTimeEvent
|
||||
{
|
||||
public:
|
||||
epicsTimeEvent (const int &number); /**< \brief Constructor */
|
||||
operator int () const; /**< \brief Extractor */
|
||||
private:
|
||||
int eventNumber;
|
||||
};
|
||||
|
||||
/** \brief C++ time stamp object
|
||||
*
|
||||
* Holds an EPICS time stamp, and provides conversion functions for both
|
||||
* input and output from/to other types.
|
||||
*
|
||||
* \note Time conversions: The epicsTime implementation will properly
|
||||
* convert between the various formats from the beginning of the EPICS
|
||||
* epoch until at least 2038. Unless the underlying architecture support
|
||||
* has defective POSIX, BSD/SRV5, or standard C time support the EPICS
|
||||
* implementation should be valid until 2106.
|
||||
*/
|
||||
class LIBCOM_API epicsTime
|
||||
{
|
||||
public:
|
||||
/// \brief Exception: Time provider problem
|
||||
class unableToFetchCurrentTime {};
|
||||
/// \brief Exception: Bad field(s) in <tt>struct tm</tt>
|
||||
class formatProblemWithStructTM {};
|
||||
|
||||
/** \brief The default constructor sets the time to the EPICS epoch. */
|
||||
epicsTime ();
|
||||
|
||||
/** \brief Get time of event system event.
|
||||
*
|
||||
* Returns an epicsTime indicating when the associated event system
|
||||
* event last occurred.
|
||||
*/
|
||||
static epicsTime getEvent ( const epicsTimeEvent & );
|
||||
/** \brief Get current clock time
|
||||
*
|
||||
* Returns an epicsTime containing the current time. For example:
|
||||
* \code{.cpp}
|
||||
* epicsTime now = epicsTime::getCurrent();
|
||||
* \endcode
|
||||
*/
|
||||
static epicsTime getCurrent ();
|
||||
/** \brief Get current monotonic time
|
||||
*
|
||||
* Returns an epicsTime containing the current monotonic time, an
|
||||
* OS clock which never going backwards or jumping forwards.
|
||||
* This time is has an undefined epoch, and is only useful for
|
||||
* measuring time differences.
|
||||
*/
|
||||
static epicsTime getMonotonic ();
|
||||
|
||||
/** \name epicsTimeStamp conversions
|
||||
* Convert to and from EPICS epicsTimeStamp format
|
||||
* @{ */
|
||||
/** \brief Convert to epicsTimeStamp */
|
||||
operator epicsTimeStamp () const;
|
||||
/** \brief Construct from epicsTimeStamp */
|
||||
epicsTime ( const epicsTimeStamp & ts );
|
||||
/** \brief Assign from epicsTimeStamp */
|
||||
epicsTime & operator = ( const epicsTimeStamp & );
|
||||
/** @} */
|
||||
|
||||
/** \name ANSI C time_t conversions
|
||||
* Convert to and from ANSI C \c time_t wrapper .
|
||||
* @{ */
|
||||
/** \brief Convert to ANSI C \c time_t */
|
||||
operator time_t_wrapper () const;
|
||||
/** \brief Construct from ANSI C \c time_t */
|
||||
epicsTime ( const time_t_wrapper & );
|
||||
/** \brief Assign from ANSI C \c time_t */
|
||||
epicsTime & operator = ( const time_t_wrapper & );
|
||||
/** @} */
|
||||
|
||||
/** \name ANSI C struct tm local-time conversions
|
||||
* Convert to and from ANSI Cs <tt>struct tm</tt> (with nano seconds),
|
||||
* adjusted for the local time zone.
|
||||
* @{ */
|
||||
/** \brief Convert to <tt>struct tm</tt> in local time zone */
|
||||
operator local_tm_nano_sec () const;
|
||||
/** \brief Construct from <tt>struct tm</tt> in local time zone */
|
||||
epicsTime ( const local_tm_nano_sec & );
|
||||
/** \brief Assign from <tt>struct tm</tt> in local time zone */
|
||||
epicsTime & operator = ( const local_tm_nano_sec & );
|
||||
/** @} */
|
||||
|
||||
/** \name ANSI C struct tm UTC conversions
|
||||
* Convert to and from ANSI Cs <tt>struct tm</tt> (with nano seconds),
|
||||
* adjusted for Greenwich Mean Time (UTC).
|
||||
* @{ */
|
||||
/** \brief Convert to <tt>struct tm</tt> in UTC/GMT */
|
||||
operator gm_tm_nano_sec () const;
|
||||
/** \brief Construct from <tt>struct tm</tt> in UTC/GMT */
|
||||
epicsTime ( const gm_tm_nano_sec & );
|
||||
/** \brief Assign from <tt>struct tm</tt> in UTC */
|
||||
epicsTime & operator = ( const gm_tm_nano_sec & );
|
||||
/** @} */
|
||||
|
||||
/** \name POSIX RT struct timespec conversions
|
||||
* Convert to and from the POSIX RealTime <tt>struct timespec</tt>
|
||||
* format.
|
||||
* @{ */
|
||||
/** \brief Convert to <tt>struct timespec</tt> */
|
||||
operator struct timespec () const;
|
||||
/** \brief Construct from <tt>struct timespec</tt> */
|
||||
epicsTime ( const struct timespec & );
|
||||
/** \brief Assign from <tt>struct timespec</tt> */
|
||||
epicsTime & operator = ( const struct timespec & );
|
||||
/** @} */
|
||||
|
||||
/** \name BSD's struct timeval conversions
|
||||
* Convert to and from the BSD <tt>struct timeval</tt> format.
|
||||
* @{ */
|
||||
/** \brief Convert to <tt>struct timeval</tt> */
|
||||
operator struct timeval () const;
|
||||
/** \brief Construct from <tt>struct timeval</tt> */
|
||||
epicsTime ( const struct timeval & );
|
||||
/** \brief Assign from <tt>struct timeval</tt> */
|
||||
epicsTime & operator = ( const struct timeval & );
|
||||
/** @} */
|
||||
|
||||
/** \name NTP timestamp conversions
|
||||
* Convert to and from the NTP timestamp structure \c l_fp
|
||||
* @{ */
|
||||
/** \brief Convert to NTP format */
|
||||
operator l_fp () const;
|
||||
/** \brief Construct from NTP format */
|
||||
epicsTime ( const l_fp & );
|
||||
/** \brief Assign from NTP format */
|
||||
epicsTime & operator = ( const l_fp & );
|
||||
/** @} */
|
||||
|
||||
/** \name WIN32 FILETIME conversions
|
||||
* Convert to and from WIN32s <tt> _FILETIME</tt>
|
||||
* \note These are only implemented on Windows targets.
|
||||
* @{ */
|
||||
/** \brief Convert to Windows <tt>struct _FILETIME</tt> */
|
||||
operator struct _FILETIME () const;
|
||||
/** \brief Constuct from Windows <tt>struct _FILETIME</tt> */
|
||||
epicsTime ( const struct _FILETIME & );
|
||||
/** \brief Assign from Windows <tt>struct _FILETIME</tt> */
|
||||
epicsTime & operator = ( const struct _FILETIME & );
|
||||
/** @} */
|
||||
|
||||
/** \name Arithmetic operators
|
||||
* Standard operators involving epicsTime objects and time differences
|
||||
* which are always expressed as a \c double in seconds.
|
||||
* @{ */
|
||||
/// \brief \p lhs minus \p rhs, in seconds
|
||||
double operator- ( const epicsTime & ) const;
|
||||
/// \brief \p lhs plus rhs seconds
|
||||
epicsTime operator+ ( const double & ) const;
|
||||
/// \brief \p lhs minus rhs seconds
|
||||
epicsTime operator- ( const double & ) const;
|
||||
/// \brief add rhs seconds to \p lhs
|
||||
epicsTime operator+= ( const double & );
|
||||
/// \brief subtract rhs seconds from \p lhs
|
||||
epicsTime operator-= ( const double & );
|
||||
/** @} */
|
||||
|
||||
/** \name Comparison operators
|
||||
* Standard comparisons between epicsTime objects.
|
||||
* @{ */
|
||||
/// \brief \p lhs equals \p rhs
|
||||
bool operator == ( const epicsTime & ) const;
|
||||
/// \brief \p lhs not equal to \p rhs
|
||||
bool operator != ( const epicsTime & ) const;
|
||||
/// \brief \p rhs no later than \p lhs
|
||||
bool operator <= ( const epicsTime & ) const;
|
||||
/// \brief \p lhs was before \p rhs
|
||||
bool operator < ( const epicsTime & ) const;
|
||||
/// \brief \p rhs not before \p lhs
|
||||
bool operator >= ( const epicsTime & ) const;
|
||||
/// \brief \p lhs was after \p rhs
|
||||
bool operator > ( const epicsTime & ) const;
|
||||
/** @} */
|
||||
|
||||
/** \brief Convert to string in user-specified format
|
||||
*
|
||||
* This method extends the standard C library routine strftime().
|
||||
* See your OS documentation for details about the standard routine.
|
||||
* The epicsTime method adds support for printing the fractional
|
||||
* portion of the time. It searches the format string for the
|
||||
* sequence <tt>%0<i>n</i>f</tt> where \a n is the desired precision,
|
||||
* and uses this format to convert the fractional seconds with the
|
||||
* requested precision. For example:
|
||||
* \code{.cpp}
|
||||
* epicsTime time = epicsTime::getCurrent();
|
||||
* char buf[30];
|
||||
* time.strftime(buf, 30, "%Y-%m-%d %H:%M:%S.%06f");
|
||||
* printf("%s\n", buf);
|
||||
* \endcode
|
||||
* This will print the current time in the format:
|
||||
* \code
|
||||
* 2001-01-26 20:50:29.813505
|
||||
* \endcode
|
||||
*/
|
||||
size_t strftime ( char * pBuff, size_t bufLength, const char * pFormat ) const;
|
||||
|
||||
/** \brief Dump current state to standard out */
|
||||
void show ( unsigned interestLevel ) const;
|
||||
|
||||
private:
|
||||
/*
|
||||
* private because:
|
||||
* a) application does not break when EPICS epoch is changed
|
||||
* b) no assumptions about internal storage or internal precision
|
||||
* in the application
|
||||
* c) it would be easy to forget which argument is nanoseconds
|
||||
* and which argument is seconds (no help from compiler)
|
||||
*/
|
||||
epicsTime ( const unsigned long secPastEpoch, const unsigned long nSec );
|
||||
void addNanoSec ( long nanoSecAdjust );
|
||||
|
||||
unsigned long secPastEpoch; /* seconds since O000 Jan 1, 1990 */
|
||||
unsigned long nSec; /* nanoseconds within second */
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/** \name Return status values
|
||||
* epicsTime routines return \c S_time_ error status values:
|
||||
* @{
|
||||
@@ -457,6 +194,12 @@ LIBCOM_API void epicsStdCall epicsTimeAddSeconds (
|
||||
epicsTimeStamp * pDest, double secondsToAdd ); /* adds seconds to *pDest */
|
||||
/** @} */
|
||||
|
||||
/** \brief Return difference LHS-RHS as signed integer nanoseconds.
|
||||
* @since UNRELEASED
|
||||
*/
|
||||
LIBCOM_API
|
||||
epicsInt64 epicsStdCall epicsTimeDiffInNS (const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight);
|
||||
|
||||
/** \name Comparison operators
|
||||
* Comparisons between epicsTimeStamp objects, returning 0=false, 1=true.
|
||||
* @{ */
|
||||
@@ -515,98 +258,330 @@ LIBCOM_API void osdMonotonicInit(void);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
/** \brief C++ only ANSI C <tt>struct tm</tt> with nanoseconds, local timezone
|
||||
*
|
||||
* Extend ANSI C "struct tm" to include nano seconds within a second
|
||||
* and a struct tm that is adjusted for the local timezone.
|
||||
*/
|
||||
struct local_tm_nano_sec {
|
||||
struct tm ansi_tm; /**< \brief ANSI C time details */
|
||||
unsigned long nSec; /**< \brief nanoseconds extension */
|
||||
};
|
||||
|
||||
/** \brief C++ only ANSI C <tt>sruct tm</tt> with nanoseconds, UTC
|
||||
*
|
||||
* Extend ANSI C "struct tm" to include nanoseconds within a second
|
||||
* and a struct tm that is adjusted for GMT (UTC).
|
||||
*/
|
||||
struct gm_tm_nano_sec {
|
||||
struct tm ansi_tm; /**< \brief ANSI C time details */
|
||||
unsigned long nSec; /**< \brief nanoseconds extension */
|
||||
};
|
||||
|
||||
/** \brief C++ only ANSI C time_t
|
||||
*
|
||||
* This is for converting to/from the ANSI C \c time_t. Since \c time_t
|
||||
* is usually an elementary type providing a conversion operator from
|
||||
* \c time_t to/from epicsTime could cause undesirable implicit
|
||||
* conversions. Providing a conversion operator to/from the
|
||||
* \c time_t_wrapper instead prevents implicit conversions.
|
||||
*/
|
||||
struct time_t_wrapper {
|
||||
time_t ts;
|
||||
};
|
||||
|
||||
/** \brief C++ Event number wrapper class
|
||||
*
|
||||
* Stores an event number for use by the epicsTime::getEvent() static
|
||||
* class method.
|
||||
*/
|
||||
class LIBCOM_API epicsTimeEvent
|
||||
{
|
||||
public:
|
||||
epicsTimeEvent (const int &number) :eventNumber(number) {}
|
||||
operator int () const { return eventNumber; }
|
||||
private:
|
||||
int eventNumber;
|
||||
};
|
||||
|
||||
/** \brief C++ time stamp object
|
||||
*
|
||||
* Holds an EPICS time stamp, and provides conversion functions for both
|
||||
* input and output from/to other types.
|
||||
*
|
||||
* \note Time conversions: The epicsTime implementation will properly
|
||||
* convert between the various formats from the beginning of the EPICS
|
||||
* epoch until at least 2038. Unless the underlying architecture support
|
||||
* has defective POSIX, BSD/SRV5, or standard C time support the EPICS
|
||||
* implementation should be valid until 2106.
|
||||
*/
|
||||
class LIBCOM_API epicsTime
|
||||
{
|
||||
// translate S_time_* code to exception
|
||||
static void throwError(int code);
|
||||
public:
|
||||
/// \brief Exception: Time provider problem
|
||||
typedef std::runtime_error unableToFetchCurrentTime;
|
||||
/// \brief Exception: Bad field(s) in <tt>struct tm</tt>
|
||||
typedef std::logic_error formatProblemWithStructTM;
|
||||
|
||||
/** \brief The default constructor sets the time to the EPICS epoch. */
|
||||
#if __cplusplus>=201103L
|
||||
constexpr epicsTime() :ts{} {}
|
||||
#else
|
||||
epicsTime () {
|
||||
ts.secPastEpoch = ts.nsec = 0u;
|
||||
}
|
||||
#endif
|
||||
|
||||
/** \brief Get time of event system event.
|
||||
*
|
||||
* Returns an epicsTime indicating when the associated event system
|
||||
* event last occurred.
|
||||
*/
|
||||
static inline epicsTime getEvent ( const epicsTimeEvent & evt) ;
|
||||
/** \brief Get current clock time
|
||||
*
|
||||
* Returns an epicsTime containing the current time. For example:
|
||||
* \code{.cpp}
|
||||
* epicsTime now = epicsTime::getCurrent();
|
||||
* \endcode
|
||||
*/
|
||||
static epicsTime getCurrent ();
|
||||
/** \brief Get current monotonic time
|
||||
*
|
||||
* Returns an epicsTime containing the current monotonic time, an
|
||||
* OS clock which never going backwards or jumping forwards.
|
||||
* This time is has an undefined epoch, and is only useful for
|
||||
* measuring time differences.
|
||||
*/
|
||||
static epicsTime getMonotonic () {
|
||||
epicsTime ret;
|
||||
epicsTimeGetMonotonic(&ret.ts); // can't fail
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** \name epicsTimeStamp conversions
|
||||
* Convert to and from EPICS epicsTimeStamp format
|
||||
* @{ */
|
||||
/** \brief Convert to epicsTimeStamp */
|
||||
operator const epicsTimeStamp& () const { return ts; }
|
||||
/** \brief Construct from epicsTimeStamp */
|
||||
epicsTime ( const epicsTimeStamp & replace );
|
||||
/** \brief Assign from epicsTimeStamp */
|
||||
epicsTime & operator = ( const epicsTimeStamp & replace) {
|
||||
ts = replace;
|
||||
return *this;
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** \name ANSI C time_t conversions
|
||||
* Convert to and from ANSI C \c time_t wrapper .
|
||||
* @{ */
|
||||
/** \brief Convert to ANSI C \c time_t */
|
||||
operator time_t_wrapper () const {
|
||||
time_t_wrapper ret;
|
||||
throwError(epicsTimeToTime_t(&ret.ts, &ts));
|
||||
return ret;
|
||||
}
|
||||
/** \brief Construct from ANSI C \c time_t */
|
||||
epicsTime ( const time_t_wrapper & replace ) {
|
||||
throwError(epicsTimeFromTime_t(&ts, replace.ts));
|
||||
}
|
||||
/** \brief Assign from ANSI C \c time_t */
|
||||
epicsTime & operator = ( const time_t_wrapper & replace) {
|
||||
throwError(epicsTimeFromTime_t(&ts, replace.ts));
|
||||
return *this;
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** \name ANSI C struct tm local-time conversions
|
||||
* Convert to and from ANSI Cs <tt>struct tm</tt> (with nano seconds),
|
||||
* adjusted for the local time zone.
|
||||
* @{ */
|
||||
/** \brief Convert to <tt>struct tm</tt> in local time zone */
|
||||
operator local_tm_nano_sec () const {
|
||||
local_tm_nano_sec ret;
|
||||
throwError(epicsTimeToTM(&ret.ansi_tm, 0, &ts));
|
||||
ret.nSec = ts.nsec;
|
||||
return ret;
|
||||
}
|
||||
/** \brief Construct from <tt>struct tm</tt> in local time zone */
|
||||
epicsTime ( const local_tm_nano_sec & replace) {
|
||||
throwError(epicsTimeFromTM(&ts, &replace.ansi_tm, replace.nSec));
|
||||
}
|
||||
/** \brief Assign from <tt>struct tm</tt> in local time zone */
|
||||
epicsTime & operator = ( const local_tm_nano_sec & replace) {
|
||||
throwError(epicsTimeFromTM(&ts, &replace.ansi_tm, replace.nSec));
|
||||
return *this;
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** \name ANSI C struct tm UTC conversions
|
||||
* Convert to and from ANSI Cs <tt>struct tm</tt> (with nano seconds),
|
||||
* adjusted for Greenwich Mean Time (UTC).
|
||||
* @{ */
|
||||
/** \brief Convert to <tt>struct tm</tt> in UTC/GMT */
|
||||
operator gm_tm_nano_sec () const {
|
||||
gm_tm_nano_sec ret;
|
||||
throwError(epicsTimeToGMTM(&ret.ansi_tm, 0, &ts));
|
||||
ret.nSec = ts.nsec;
|
||||
return ret;
|
||||
}
|
||||
/** \brief Construct from <tt>struct tm</tt> in UTC/GMT */
|
||||
epicsTime ( const gm_tm_nano_sec & replace) {
|
||||
throwError(epicsTimeFromGMTM(&ts, &replace.ansi_tm, replace.nSec));
|
||||
}
|
||||
/** \brief Assign from <tt>struct tm</tt> in UTC */
|
||||
epicsTime & operator = ( const gm_tm_nano_sec & replace) {
|
||||
throwError(epicsTimeFromGMTM(&ts, &replace.ansi_tm, replace.nSec));
|
||||
return *this;
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** \name POSIX RT struct timespec conversions
|
||||
* Convert to and from the POSIX RealTime <tt>struct timespec</tt>
|
||||
* format.
|
||||
* @{ */
|
||||
/** \brief Convert to <tt>struct timespec</tt> */
|
||||
operator struct timespec () const {
|
||||
timespec ret;
|
||||
epicsTimeToTimespec(&ret, &ts);
|
||||
return ret;
|
||||
}
|
||||
/** \brief Construct from <tt>struct timespec</tt> */
|
||||
epicsTime ( const struct timespec & replace) {
|
||||
throwError(epicsTimeFromTimespec(&ts, &replace));
|
||||
}
|
||||
/** \brief Assign from <tt>struct timespec</tt> */
|
||||
epicsTime & operator = ( const struct timespec & replace ) {
|
||||
throwError(epicsTimeFromTimespec(&ts, &replace));
|
||||
return *this;
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** \name BSD's struct timeval conversions
|
||||
* Convert to and from the BSD <tt>struct timeval</tt> format.
|
||||
* @{ */
|
||||
/** \brief Convert to <tt>struct timeval</tt> */
|
||||
operator struct timeval () const ;
|
||||
/** \brief Construct from <tt>struct timeval</tt> */
|
||||
epicsTime ( const struct timeval & replace);
|
||||
/** \brief Assign from <tt>struct timeval</tt> */
|
||||
epicsTime & operator = ( const struct timeval & replace);
|
||||
/** @} */
|
||||
|
||||
#ifdef _WIN32
|
||||
/** \name WIN32 FILETIME conversions
|
||||
* Convert to and from WIN32s <tt> _FILETIME</tt>
|
||||
* \note These are only implemented on Windows targets.
|
||||
* @{ */
|
||||
/** \brief Convert to Windows <tt>struct _FILETIME</tt> */
|
||||
operator struct _FILETIME () const;
|
||||
/** \brief Constuct from Windows <tt>struct _FILETIME</tt> */
|
||||
epicsTime ( const struct _FILETIME & );
|
||||
/** \brief Assign from Windows <tt>struct _FILETIME</tt> */
|
||||
epicsTime & operator = ( const struct _FILETIME & );
|
||||
/** @} */
|
||||
#endif /* _WIN32 */
|
||||
|
||||
/** \name Arithmetic operators
|
||||
* Standard operators involving epicsTime objects and time differences
|
||||
* which are always expressed as a \c double in seconds.
|
||||
* @{ */
|
||||
/// \brief \p lhs minus \p rhs, in seconds
|
||||
double operator- ( const epicsTime & other) const {
|
||||
return epicsTimeDiffInSeconds(&ts, &other.ts);
|
||||
}
|
||||
/// \brief \p lhs plus rhs seconds
|
||||
epicsTime operator+ (double delta) const {
|
||||
epicsTime ret(*this);
|
||||
epicsTimeAddSeconds(&ret.ts, delta);
|
||||
return ret;
|
||||
}
|
||||
/// \brief \p lhs minus rhs seconds
|
||||
epicsTime operator- (double delta ) const {
|
||||
return (*this)+(-delta);
|
||||
}
|
||||
/// \brief add rhs seconds to \p lhs
|
||||
epicsTime operator+= (double delta) {
|
||||
epicsTimeAddSeconds(&ts, delta);
|
||||
return *this;
|
||||
}
|
||||
/// \brief subtract rhs seconds from \p lhs
|
||||
epicsTime operator-= ( double delta ) {
|
||||
return (*this) += (-delta);
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** \name Comparison operators
|
||||
* Standard comparisons between epicsTime objects.
|
||||
* @{ */
|
||||
/// \brief \p lhs equals \p rhs
|
||||
bool operator == ( const epicsTime & other) const {
|
||||
return epicsTimeEqual(&ts, &other.ts);
|
||||
}
|
||||
/// \brief \p lhs not equal to \p rhs
|
||||
bool operator != ( const epicsTime & other) const {
|
||||
return epicsTimeNotEqual(&ts, &other.ts);
|
||||
}
|
||||
/// \brief \p rhs no later than \p lhs
|
||||
bool operator <= ( const epicsTime & other) const {
|
||||
return epicsTimeLessThanEqual(&ts, &other.ts);
|
||||
}
|
||||
/// \brief \p lhs was before \p rhs
|
||||
bool operator < ( const epicsTime & other) const {
|
||||
return epicsTimeLessThan(&ts, &other.ts);
|
||||
}
|
||||
/// \brief \p rhs not before \p lhs
|
||||
bool operator >= ( const epicsTime & other) const {
|
||||
return epicsTimeGreaterThanEqual(&ts, &other.ts);
|
||||
}
|
||||
/// \brief \p lhs was after \p rhs
|
||||
bool operator > ( const epicsTime & other) const {
|
||||
return epicsTimeGreaterThan(&ts, &other.ts);
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** \brief Convert to string in user-specified format
|
||||
*
|
||||
* This method extends the standard C library routine strftime().
|
||||
* See your OS documentation for details about the standard routine.
|
||||
* The epicsTime method adds support for printing the fractional
|
||||
* portion of the time. It searches the format string for the
|
||||
* sequence <tt>%0<i>n</i>f</tt> where \a n is the desired precision,
|
||||
* and uses this format to convert the fractional seconds with the
|
||||
* requested precision. For example:
|
||||
* \code{.cpp}
|
||||
* epicsTime time = epicsTime::getCurrent();
|
||||
* char buf[30];
|
||||
* time.strftime(buf, 30, "%Y-%m-%d %H:%M:%S.%06f");
|
||||
* printf("%s\n", buf);
|
||||
* \endcode
|
||||
* This will print the current time in the format:
|
||||
* \code
|
||||
* 2001-01-26 20:50:29.813505
|
||||
* \endcode
|
||||
*/
|
||||
size_t strftime ( char * pBuff, size_t bufLength, const char * pFormat ) const {
|
||||
return epicsTimeToStrftime(pBuff, bufLength, pFormat, &ts);
|
||||
}
|
||||
|
||||
/** \brief Dump current state to standard out */
|
||||
void show ( unsigned interestLevel ) const {
|
||||
epicsTimeShow(&ts, interestLevel);
|
||||
}
|
||||
|
||||
private:
|
||||
epicsTimeStamp ts;
|
||||
};
|
||||
|
||||
LIBCOM_API
|
||||
std::ostream& operator<<(std::ostream& strm, const epicsTime& ts);
|
||||
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/* inline member functions ,*/
|
||||
#ifdef __cplusplus
|
||||
|
||||
/* epicsTimeEvent */
|
||||
|
||||
inline epicsTimeEvent::epicsTimeEvent (const int &number) :
|
||||
eventNumber(number) {}
|
||||
|
||||
inline epicsTimeEvent::operator int () const
|
||||
{
|
||||
return this->eventNumber;
|
||||
}
|
||||
|
||||
|
||||
/* epicsTime */
|
||||
|
||||
inline epicsTime epicsTime::operator - ( const double & rhs ) const
|
||||
{
|
||||
return epicsTime::operator + ( -rhs );
|
||||
}
|
||||
|
||||
inline epicsTime epicsTime::operator += ( const double & rhs )
|
||||
{
|
||||
*this = epicsTime::operator + ( rhs );
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline epicsTime epicsTime::operator -= ( const double & rhs )
|
||||
{
|
||||
*this = epicsTime::operator + ( -rhs );
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline bool epicsTime::operator == ( const epicsTime & rhs ) const
|
||||
{
|
||||
if ( this->secPastEpoch == rhs.secPastEpoch && this->nSec == rhs.nSec ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool epicsTime::operator != ( const epicsTime & rhs ) const
|
||||
{
|
||||
return !epicsTime::operator == ( rhs );
|
||||
}
|
||||
|
||||
inline bool epicsTime::operator >= ( const epicsTime & rhs ) const
|
||||
{
|
||||
return ! ( *this < rhs );
|
||||
}
|
||||
|
||||
inline bool epicsTime::operator > ( const epicsTime & rhs ) const
|
||||
{
|
||||
return ! ( *this <= rhs );
|
||||
}
|
||||
|
||||
inline epicsTime & epicsTime::operator = ( const local_tm_nano_sec & rhs )
|
||||
{
|
||||
return *this = epicsTime ( rhs );
|
||||
}
|
||||
|
||||
inline epicsTime & epicsTime::operator = ( const gm_tm_nano_sec & rhs )
|
||||
{
|
||||
return *this = epicsTime ( rhs );
|
||||
}
|
||||
|
||||
inline epicsTime & epicsTime::operator = ( const struct timespec & rhs )
|
||||
{
|
||||
*this = epicsTime ( rhs );
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline epicsTime & epicsTime::operator = ( const epicsTimeStamp & rhs )
|
||||
{
|
||||
*this = epicsTime ( rhs );
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline epicsTime & epicsTime::operator = ( const l_fp & rhs )
|
||||
{
|
||||
*this = epicsTime ( rhs );
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline epicsTime & epicsTime::operator = ( const time_t_wrapper & rhs )
|
||||
{
|
||||
*this = epicsTime ( rhs );
|
||||
return *this;
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* epicsTimehInclude */
|
||||
@@ -510,8 +510,8 @@ static unsigned __stdcall _pllThreadEntry ( void * pCurrentTimeIn )
|
||||
epicsTime::operator FILETIME () const
|
||||
{
|
||||
LARGE_INTEGER ftTicks;
|
||||
ftTicks.QuadPart = ( this->secPastEpoch * FILE_TIME_TICKS_PER_SEC ) +
|
||||
( this->nSec / ET_TICKS_PER_FT_TICK );
|
||||
ftTicks.QuadPart = ( this->ts.secPastEpoch * FILE_TIME_TICKS_PER_SEC ) +
|
||||
( this->ts.nsec / ET_TICKS_PER_FT_TICK );
|
||||
ftTicks.QuadPart += epicsEpochInFileTime;
|
||||
FILETIME ts;
|
||||
ts.dwLowDateTime = ftTicks.LowPart;
|
||||
@@ -527,15 +527,15 @@ epicsTime::epicsTime ( const FILETIME & ts )
|
||||
if ( lift.QuadPart > epicsEpochInFileTime ) {
|
||||
LONGLONG fileTimeTicksSinceEpochEPICS =
|
||||
lift.QuadPart - epicsEpochInFileTime;
|
||||
this->secPastEpoch = static_cast < epicsUInt32 >
|
||||
this->ts.secPastEpoch = static_cast < epicsUInt32 >
|
||||
( fileTimeTicksSinceEpochEPICS / FILE_TIME_TICKS_PER_SEC );
|
||||
this->nSec = static_cast < epicsUInt32 >
|
||||
this->ts.nsec = static_cast < epicsUInt32 >
|
||||
( ( fileTimeTicksSinceEpochEPICS % FILE_TIME_TICKS_PER_SEC ) *
|
||||
ET_TICKS_PER_FT_TICK );
|
||||
}
|
||||
else {
|
||||
this->secPastEpoch = 0;
|
||||
this->nSec = 0;
|
||||
this->ts.secPastEpoch = 0;
|
||||
this->ts.nsec = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
/*
|
||||
* Authors: Jeff Hill, Marty Kraimer and Andrew Johnson
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
@@ -17,6 +20,7 @@
|
||||
#include <climits>
|
||||
#include <cstring>
|
||||
|
||||
#include "envDefs.h"
|
||||
#include "epicsTime.h"
|
||||
#include "epicsThread.h"
|
||||
#include "errlog.h"
|
||||
@@ -29,16 +33,45 @@ using namespace std;
|
||||
* routines is incorporated into epicsTimeTest () below.
|
||||
*/
|
||||
|
||||
struct l_fp { /* NTP time stamp */
|
||||
epicsUInt32 l_ui; /* sec past NTP epoch */
|
||||
epicsUInt32 l_uf; /* fractional seconds */
|
||||
};
|
||||
|
||||
static const unsigned mSecPerSec = 1000u;
|
||||
static const unsigned uSecPerSec = 1000u * mSecPerSec;
|
||||
static const unsigned nSecPerSec = 1000u * uSecPerSec;
|
||||
static const double precisionEPICS = 1.0 / nSecPerSec;
|
||||
|
||||
static void testAdd(epicsUInt32 lhsSec, epicsUInt32 lhsNS,
|
||||
double rhs,
|
||||
epicsUInt32 expectSec, epicsUInt32 expectNS)
|
||||
{
|
||||
epicsTimeStamp lhs = {lhsSec, lhsNS};
|
||||
epicsTimeStamp expect = {expectSec, expectNS};
|
||||
epicsTimeStamp actual = lhs;
|
||||
|
||||
|
||||
epicsTimeAddSeconds(&actual, rhs);
|
||||
testOk(epicsTimeEqual(&actual, &expect),
|
||||
"testAdd(%u:%u + %.9f -> %u:%u == %u:%u)",
|
||||
unsigned(lhs.secPastEpoch), unsigned(lhs.nsec),
|
||||
rhs,
|
||||
unsigned(actual.secPastEpoch), unsigned(actual.nsec),
|
||||
unsigned(expect.secPastEpoch), unsigned(expect.nsec));
|
||||
}
|
||||
|
||||
static void testDiff(epicsUInt32 lhsSec, epicsUInt32 lhsNS,
|
||||
epicsUInt32 rhsSec, epicsUInt32 rhsNS,
|
||||
double expect)
|
||||
{
|
||||
epicsTimeStamp lhs = {lhsSec, lhsNS};
|
||||
epicsTimeStamp rhs = {rhsSec, rhsNS};
|
||||
double actual = epicsTimeDiffInSeconds(&lhs, &rhs);
|
||||
double diff = actual - expect;
|
||||
|
||||
testOk(fabs(diff)<precisionEPICS,
|
||||
"testDiff(%u:%u - %u:%u) -> %.9f ~= %.9f (%g)",
|
||||
unsigned(lhs.secPastEpoch), unsigned(lhs.nsec),
|
||||
unsigned(rhs.secPastEpoch), unsigned(rhs.nsec),
|
||||
actual, expect, diff);
|
||||
}
|
||||
|
||||
static void crossCheck(double delay)
|
||||
{
|
||||
double mindelta = 2*epicsMonotonicResolution()*1e-9,
|
||||
@@ -80,12 +113,91 @@ static void testMonotonic()
|
||||
testDiag("Small Delta %u ns", (unsigned)(B-A));
|
||||
}
|
||||
|
||||
static void testTMGames()
|
||||
{
|
||||
testDiag("testTMGames()");
|
||||
|
||||
epicsTimeStamp now;
|
||||
testOk1(!epicsTimeGetCurrent(&now));
|
||||
now.nsec = 0; // not relevant
|
||||
|
||||
tm gtm, ltm;
|
||||
epicsTimeToTM(<m, 0, &now);
|
||||
epicsTimeToGMTM(>m, 0, &now);
|
||||
|
||||
// we can't do any tests on the decomposed time without knowing the current TZ
|
||||
testDiag("LTM mday=%u hour=%u min=%u sec=%u", ltm.tm_mday, ltm.tm_hour, ltm.tm_min, ltm.tm_sec);
|
||||
testDiag("GTM mday=%u hour=%u min=%u sec=%u", gtm.tm_mday, gtm.tm_hour, gtm.tm_min, gtm.tm_sec);
|
||||
|
||||
epicsTimeStamp gtime, ltime;
|
||||
epicsTimeFromTM(<ime, <m, 0);
|
||||
epicsTimeFromGMTM(>ime, >m, 0);
|
||||
|
||||
testOk(now.secPastEpoch==ltime.secPastEpoch, "localtime %u == %u",
|
||||
unsigned(now.secPastEpoch), unsigned(ltime.secPastEpoch));
|
||||
|
||||
testOk(now.secPastEpoch==gtime.secPastEpoch, "gmtime %u == %u",
|
||||
unsigned(now.secPastEpoch), unsigned(ltime.secPastEpoch));
|
||||
}
|
||||
|
||||
MAIN(epicsTimeTest)
|
||||
{
|
||||
const int wasteTime = 100000;
|
||||
const int nTimes = 10;
|
||||
|
||||
testPlan(22 + nTimes * 19);
|
||||
testPlan(52 + nTimes * 19);
|
||||
|
||||
testDiag("$TZ = \"%s\"", getenv("TZ"));
|
||||
testDiag("EPICS_TZ = \"%s\"", envGetConfigParamPtr(&EPICS_TZ));
|
||||
|
||||
#if !defined(_WIN32) && !defined(vxWorks)
|
||||
{
|
||||
// at least glibc doesn't initialize tzname[2] until some time.h function needs the time zone
|
||||
time_t junk = 0;
|
||||
(void)localtime(&junk);
|
||||
testDiag("Local TZ names \"%s\", \"%s\"", tzname[0], tzname[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
// sec:ns + double == sec:ns
|
||||
testAdd(0,0, 0.0, 0,0);
|
||||
testAdd(1,1, 0.0, 1,1);
|
||||
testAdd(1,999999999, 0.000000001, 2,0);
|
||||
testAdd(1,1, 2.000000002, 3,3);
|
||||
testAdd(1,0, -1.0, 0,0);
|
||||
testAdd(0,1, -0.000000001, 0,0);
|
||||
testAdd(1,1, -1.000000001, 0,0);
|
||||
testAdd(0xffffffff,0, -1.0, 0xfffffffe,0);
|
||||
testAdd(0x7fffffff,0, 1.0, 0x80000000,0);
|
||||
testAdd(0x7fffffff,999999999, 0.000000001, 0x80000000,0);
|
||||
|
||||
// sec:ns - sec:ns == double
|
||||
testDiff(0,0, 0,0, 0.0);
|
||||
|
||||
testDiff(0,1, 0,1, 0.0);
|
||||
testDiff(1,0, 1,0, 0.0);
|
||||
testDiff(1,1, 1,1, 0.0);
|
||||
|
||||
testDiff(2,0, 1,999999999, 0.000000001);
|
||||
testDiff(1,999999999, 2,0, -0.000000001);
|
||||
|
||||
testDiff(1,0, 0xffffffff,0, 2.0);
|
||||
testDiff(0xffffffff,0, 1,0, -2.0);
|
||||
|
||||
testDiff(1,999999999, 0xffffffff,999999999, 2.0);
|
||||
testDiff(0xffffffff,999999999, 1,999999999, -2.0);
|
||||
|
||||
testDiff(0,999999999, 0xffffffff,0, 1.999999999); // 0.99999.. - -1.0
|
||||
testDiff(0xffffffff,0, 0,999999999, -1.999999999); // -1.0 - 0.999..
|
||||
|
||||
testDiff(0x80000000,0, 0x7fffffff,0, 1.0);
|
||||
testDiff(0x7fffffff,0, 0x80000000,0, -1.0);
|
||||
|
||||
testDiff(0x80000000,0, 0x7fffffff,999999999, 0.000000001);
|
||||
testDiff(0x7fffffff,999999999, 0x80000000,0, -0.000000001);
|
||||
|
||||
testDiff(0x80000000,999999999, 0x7fffffff,0, 1.999999999);
|
||||
testDiff(0x7fffffff,0, 0x80000000,999999999, -1.999999999);
|
||||
|
||||
try {
|
||||
const epicsTimeStamp epochTS = {0, 0};
|
||||
@@ -113,7 +225,7 @@ MAIN(epicsTimeTest)
|
||||
ts.strftime(buf, sizeof(buf), pFormat);
|
||||
testFail("nanosecond overflow returned \"%s\"", buf);
|
||||
}
|
||||
catch ( ... ) {
|
||||
catch ( std::exception& ) {
|
||||
testPass("nanosecond overflow throws");
|
||||
}
|
||||
}
|
||||
@@ -178,20 +290,11 @@ MAIN(epicsTimeTest)
|
||||
now = epicsTime::getCurrent();
|
||||
testPass("default time provider");
|
||||
}
|
||||
catch ( ... ) {
|
||||
catch ( std::exception& ) {
|
||||
testFail("epicsTime::getCurrent() throws");
|
||||
testAbort("Can't continue, check your time provider");
|
||||
}
|
||||
|
||||
{
|
||||
l_fp ntp = now;
|
||||
epicsTime tsf = ntp;
|
||||
const double diff = fabs(tsf - now);
|
||||
// the difference in the precision of the two time formats
|
||||
static const double precisionNTP = 1.0 / (1.0 + 0xffffffff);
|
||||
testOk1(diff <= precisionEPICS + precisionNTP);
|
||||
}
|
||||
|
||||
testDiag("Running %d loops", nTimes);
|
||||
|
||||
const epicsTime begin = epicsTime::getCurrent();
|
||||
@@ -225,6 +328,7 @@ MAIN(epicsTimeTest)
|
||||
"now - begin ~= diff");
|
||||
|
||||
testOk1(begin + 0 == begin);
|
||||
std::cout<<"# begin + diff ("<<(begin + diff)<<") == now ("<<now<<")\n";
|
||||
testOk1(begin + diff == now);
|
||||
testOk1(now - 0 == now);
|
||||
testOk1(now - diff == begin);
|
||||
@@ -284,6 +388,7 @@ MAIN(epicsTimeTest)
|
||||
}
|
||||
|
||||
testMonotonic();
|
||||
testTMGames();
|
||||
|
||||
return testDone();
|
||||
}
|
||||
Reference in new issue
Block a user