EPICS Base  7.0.6.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
epicsTime.h
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2009 UChicago Argonne LLC, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * SPDX-License-Identifier: EPICS
7 * EPICS BASE is distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
16 #ifndef epicsTimehInclude
17 #define epicsTimehInclude
18 
19 #include <time.h>
20 
21 #include "libComAPI.h"
22 #include "epicsTypes.h"
23 #include "osdTime.h"
24 #include "errMdef.h"
25 
27 #define POSIX_TIME_AT_EPICS_EPOCH 631152000u
28 
29 #ifdef __cplusplus
30 
31 #include <stdexcept>
32 #include <ostream>
33 
34 extern "C" {
35 #endif
36 
42 typedef struct epicsTimeStamp {
43  epicsUInt32 secPastEpoch;
44  epicsUInt32 nsec;
46 
49 typedef epicsUInt64 epicsUTag;
50 
55 #define TS_STAMP epicsTimeStamp
56 
57 
65 struct timespec; /* POSIX real time */
66 
74 struct timeval; /* BSD */
75 
81 #define epicsTimeOK 0
82 
83 #define S_time_noProvider (M_time| 1) /*No time provider*/
84 
85 #define S_time_badEvent (M_time| 2) /*Bad event number*/
86 
87 #define S_time_badArgs (M_time| 3) /*Invalid arguments*/
88 
89 #define S_time_noMemory (M_time| 4) /*Out of memory*/
90 
91 #define S_time_unsynchronized (M_time| 5) /*Provider not synchronized*/
92 
93 #define S_time_timezone (M_time| 6) /*Invalid timezone*/
94 
95 #define S_time_conversion (M_time| 7) /*Time conversion error*/
96 
102 #define epicsTimeEventCurrentTime 0
103 #define epicsTimeEventBestTime -1
104 #define epicsTimeEventDeviceTime -2
105 
112 LIBCOM_API int epicsStdCall epicsTimeGetCurrent ( epicsTimeStamp * pDest );
114 LIBCOM_API int epicsStdCall epicsTimeGetEvent (
115  epicsTimeStamp *pDest, int eventNumber);
117 LIBCOM_API int epicsTimeGetMonotonic ( epicsTimeStamp * pDest );
127 LIBCOM_API int epicsTimeGetCurrentInt(epicsTimeStamp *pDest);
129 LIBCOM_API int epicsTimeGetEventInt(epicsTimeStamp *pDest, int eventNumber);
137 LIBCOM_API int epicsStdCall epicsTimeToTime_t (
138  time_t * pDest, const epicsTimeStamp * pSrc );
140 LIBCOM_API int epicsStdCall epicsTimeFromTime_t (
141  epicsTimeStamp * pDest, time_t src );
149 LIBCOM_API int epicsStdCall epicsTimeToTM (
150  struct tm * pDest, unsigned long * pNSecDest, const epicsTimeStamp * pSrc );
152 LIBCOM_API int epicsStdCall epicsTimeToGMTM (
153  struct tm * pDest, unsigned long * pNSecDest, const epicsTimeStamp * pSrc );
155 LIBCOM_API int epicsStdCall epicsTimeFromTM (
156  epicsTimeStamp * pDest, const struct tm * pSrc, unsigned long nSecSrc );
158 LIBCOM_API int epicsStdCall epicsTimeFromGMTM (
159  epicsTimeStamp * pDest, const struct tm * pSrc, unsigned long nSecSrc );
167 LIBCOM_API int epicsStdCall epicsTimeToTimespec (
168  struct timespec * pDest, const epicsTimeStamp * pSrc );
170 LIBCOM_API int epicsStdCall epicsTimeFromTimespec (
171  epicsTimeStamp * pDest, const struct timespec * pSrc );
178 LIBCOM_API int epicsStdCall epicsTimeToTimeval (
179  struct timeval * pDest, const epicsTimeStamp * pSrc );
181 LIBCOM_API int epicsStdCall epicsTimeFromTimeval (
182  epicsTimeStamp * pDest, const struct timeval * pSrc );
190 LIBCOM_API double epicsStdCall epicsTimeDiffInSeconds (
191  const epicsTimeStamp * pLeft, const epicsTimeStamp * pRight );/* left - right */
193 LIBCOM_API void epicsStdCall epicsTimeAddSeconds (
194  epicsTimeStamp * pDest, double secondsToAdd ); /* adds seconds to *pDest */
199 LIBCOM_API epicsInt64 epicsStdCall epicsTimeDiffInNS (
200  const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight);
207 LIBCOM_API int epicsStdCall epicsTimeEqual (
208  const epicsTimeStamp * pLeft, const epicsTimeStamp * pRight);
210 LIBCOM_API int epicsStdCall epicsTimeNotEqual (
211  const epicsTimeStamp * pLeft, const epicsTimeStamp * pRight);
213 LIBCOM_API int epicsStdCall epicsTimeLessThan (
214  const epicsTimeStamp * pLeft, const epicsTimeStamp * pRight);
216 LIBCOM_API int epicsStdCall epicsTimeLessThanEqual (
217  const epicsTimeStamp * pLeft, const epicsTimeStamp * pRight);
219 LIBCOM_API int epicsStdCall epicsTimeGreaterThan (
220  const epicsTimeStamp * pLeft, const epicsTimeStamp * pRight);
222 LIBCOM_API int epicsStdCall epicsTimeGreaterThanEqual (
223  const epicsTimeStamp * pLeft, const epicsTimeStamp * pRight);
227 LIBCOM_API size_t epicsStdCall epicsTimeToStrftime (
228  char * pBuff, size_t bufLength, const char * pFormat, const epicsTimeStamp * pTS );
229 
231 LIBCOM_API void epicsStdCall epicsTimeShow (
232  const epicsTimeStamp *, unsigned interestLevel );
233 
239 LIBCOM_API int epicsStdCall epicsTime_localtime ( const time_t * clock, struct tm * result );
241 LIBCOM_API int epicsStdCall epicsTime_gmtime ( const time_t * clock, struct tm * result );
250 LIBCOM_API epicsUInt64 epicsMonotonicResolution(void);
253 LIBCOM_API epicsUInt64 epicsMonotonicGet(void);
256 #ifdef EPICS_EXPOSE_LIBCOM_MONOTONIC_PRIVATE
257 LIBCOM_API void osdMonotonicInit(void);
258 #endif
259 
260 #ifdef __cplusplus
261 } // extern "C"
262 
269  struct tm ansi_tm;
270  unsigned long nSec;
271 };
272 
279  struct tm ansi_tm;
280  unsigned long nSec;
281 };
282 
292  time_t ts;
293 };
294 
300 class LIBCOM_API epicsTimeEvent
301 {
302 public:
303  epicsTimeEvent (const int &number) :eventNumber(number) {}
304  operator int () const { return eventNumber; }
305 private:
306  int eventNumber;
307 };
308 
320 class LIBCOM_API epicsTime
321 {
322  // translate S_time_* code to exception
323  static void throwError(int code);
324 public:
326  typedef std::runtime_error unableToFetchCurrentTime;
328  typedef std::logic_error formatProblemWithStructTM;
329 
331 #if __cplusplus>=201103L
332  constexpr epicsTime() :ts{} {}
333 #else
335  ts.secPastEpoch = ts.nsec = 0u;
336  }
337 #endif
338 
344  static inline epicsTime getEvent ( const epicsTimeEvent & evt) ;
352  static epicsTime getCurrent ();
361  epicsTime ret;
362  epicsTimeGetMonotonic(&ret.ts); // can't fail
363  return ret;
364  }
365 
370  operator const epicsTimeStamp& () const { return ts; }
372  epicsTime ( const epicsTimeStamp & replace );
374  epicsTime & operator = ( const epicsTimeStamp & replace) {
375  ts = replace;
376  return *this;
377  }
384  operator time_t_wrapper () const {
385  time_t_wrapper ret;
386  throwError(epicsTimeToTime_t(&ret.ts, &ts));
387  return ret;
388  }
390  epicsTime ( const time_t_wrapper & replace ) {
391  throwError(epicsTimeFromTime_t(&ts, replace.ts));
392  }
394  epicsTime & operator = ( const time_t_wrapper & replace) {
395  throwError(epicsTimeFromTime_t(&ts, replace.ts));
396  return *this;
397  }
405  operator local_tm_nano_sec () const {
406  local_tm_nano_sec ret;
407  throwError(epicsTimeToTM(&ret.ansi_tm, 0, &ts));
408  ret.nSec = ts.nsec;
409  return ret;
410  }
412  epicsTime ( const local_tm_nano_sec & replace) {
413  throwError(epicsTimeFromTM(&ts, &replace.ansi_tm, replace.nSec));
414  }
416  epicsTime & operator = ( const local_tm_nano_sec & replace) {
417  throwError(epicsTimeFromTM(&ts, &replace.ansi_tm, replace.nSec));
418  return *this;
419  }
427  operator gm_tm_nano_sec () const {
428  gm_tm_nano_sec ret;
429  throwError(epicsTimeToGMTM(&ret.ansi_tm, 0, &ts));
430  ret.nSec = ts.nsec;
431  return ret;
432  }
434  epicsTime ( const gm_tm_nano_sec & replace) {
435  throwError(epicsTimeFromGMTM(&ts, &replace.ansi_tm, replace.nSec));
436  }
438  epicsTime & operator = ( const gm_tm_nano_sec & replace) {
439  throwError(epicsTimeFromGMTM(&ts, &replace.ansi_tm, replace.nSec));
440  return *this;
441  }
449  operator struct timespec () const {
450  timespec ret;
451  epicsTimeToTimespec(&ret, &ts);
452  return ret;
453  }
455  epicsTime ( const struct timespec & replace) {
456  throwError(epicsTimeFromTimespec(&ts, &replace));
457  }
459  epicsTime & operator = ( const struct timespec & replace ) {
460  throwError(epicsTimeFromTimespec(&ts, &replace));
461  return *this;
462  }
469  operator struct timeval () const ;
471  epicsTime ( const struct timeval & replace);
473  epicsTime & operator = ( const struct timeval & replace);
476 #ifdef _WIN32
477 
482  operator struct _FILETIME () const;
484  epicsTime ( const struct _FILETIME & );
486  epicsTime & operator = ( const struct _FILETIME & );
488 #endif /* _WIN32 */
489 
494  double operator- ( const epicsTime & other) const {
496  return epicsTimeDiffInSeconds(&ts, &other.ts);
497  }
499  epicsTime operator+ (double delta) const {
500  epicsTime ret(*this);
501  epicsTimeAddSeconds(&ret.ts, delta);
502  return ret;
503  }
505  epicsTime operator- (double delta ) const {
506  return (*this)+(-delta);
507  }
509  epicsTime operator+= (double delta) {
510  epicsTimeAddSeconds(&ts, delta);
511  return *this;
512  }
514  epicsTime operator-= ( double delta ) {
515  return (*this) += (-delta);
516  }
522  bool operator == ( const epicsTime & other) const {
524  return epicsTimeEqual(&ts, &other.ts);
525  }
527  bool operator != ( const epicsTime & other) const {
528  return epicsTimeNotEqual(&ts, &other.ts);
529  }
531  bool operator <= ( const epicsTime & other) const {
532  return epicsTimeLessThanEqual(&ts, &other.ts);
533  }
535  bool operator < ( const epicsTime & other) const {
536  return epicsTimeLessThan(&ts, &other.ts);
537  }
539  bool operator >= ( const epicsTime & other) const {
540  return epicsTimeGreaterThanEqual(&ts, &other.ts);
541  }
543  bool operator > ( const epicsTime & other) const {
544  return epicsTimeGreaterThan(&ts, &other.ts);
545  }
568  size_t strftime ( char * pBuff, size_t bufLength, const char * pFormat ) const {
569  return epicsTimeToStrftime(pBuff, bufLength, pFormat, &ts);
570  }
571 
573  void show ( unsigned interestLevel ) const {
574  epicsTimeShow(&ts, interestLevel);
575  }
576 
577 private:
578  epicsTimeStamp ts;
579 };
580 
581 LIBCOM_API
582 std::ostream& operator<<(std::ostream& strm, const epicsTime& ts);
583 
584 #endif /* __cplusplus */
585 
586 
587 #endif /* epicsTimehInclude */
LIBCOM_API double epicsStdCall epicsTimeDiffInSeconds(const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight)
Time difference between left and right in seconds.
LIBCOM_API int epicsStdCall epicsTimeToGMTM(struct tm *pDest, unsigned long *pNSecDest, const epicsTimeStamp *pSrc)
Convert epicsTimeStamp to struct tm in UTC/GMT.
C++ only ANSI C time_t.
Definition: epicsTime.h:291
C++ only ANSI C struct tm with nanoseconds, local timezone.
Definition: epicsTime.h:268
LIBCOM_API int epicsStdCall epicsTimeLessThanEqual(const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight)
right was no later than left
LIBCOM_API int epicsStdCall epicsTimeGreaterThan(const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight)
left was after right
LIBCOM_API epicsUInt64 epicsMonotonicResolution(void)
Monotonic time resolution, may not be accurate. Returns the minimum non-zero time difference between ...
LIBCOM_API int epicsStdCall epicsTimeFromTime_t(epicsTimeStamp *pDest, time_t src)
Convert ANSI C time_t to epicsTimeStamp.
epicsTime(const local_tm_nano_sec &replace)
Construct from struct tm in local time zone.
Definition: epicsTime.h:412
epicsUInt64 epicsUTag
Type of UTAG field (dbCommon::utag)
Definition: epicsTime.h:49
LIBCOM_API int epicsStdCall epicsTimeGetEvent(epicsTimeStamp *pDest, int eventNumber)
Get time of event eventNumber into *pDest.
unsigned long nSec
nanoseconds extension
Definition: epicsTime.h:280
epicsUInt32 nsec
nanoseconds within second
Definition: epicsTime.h:44
LIBCOM_API int epicsStdCall epicsTimeEqual(const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight)
left equals right
The core data types used by epics.
LIBCOM_API epicsInt64 epicsStdCall epicsTimeDiffInNS(const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight)
Time difference between left and right, as a signed integer number of nanoseconds.
LIBCOM_API int epicsTimeGetCurrentInt(epicsTimeStamp *pDest)
Get current time into *pDest (ISR-safe)
LIBCOM_API int epicsStdCall epicsTimeToTimeval(struct timeval *pDest, const epicsTimeStamp *pSrc)
Convert epicsTimeStamp to struct timeval
LIBCOM_API int epicsStdCall epicsTimeToTimespec(struct timespec *pDest, const epicsTimeStamp *pSrc)
Convert epicsTimeStamp to struct timespec
struct tm ansi_tm
ANSI C time details.
Definition: epicsTime.h:269
LIBCOM_API int epicsStdCall epicsTimeFromTimespec(epicsTimeStamp *pDest, const struct timespec *pSrc)
Set epicsTimeStamp from struct timespec
C++ time stamp object.
Definition: epicsTime.h:320
std::runtime_error unableToFetchCurrentTime
Exception: Time provider problem.
Definition: epicsTime.h:326
LIBCOM_API int epicsStdCall epicsTimeFromTimeval(epicsTimeStamp *pDest, const struct timeval *pSrc)
Set epicsTimeStamp from struct timeval
unsigned long nSec
nanoseconds extension
Definition: epicsTime.h:270
epicsUInt32 secPastEpoch
seconds since 0000 Jan 1, 1990
Definition: epicsTime.h:43
LIBCOM_API int epicsStdCall epicsTime_localtime(const time_t *clock, struct tm *result)
Break down a time_t into a struct tm in the local timezone.
EPICS time stamp, for use from C code.
Definition: epicsTime.h:42
epicsTime()
The default constructor sets the time to the EPICS epoch.
Definition: epicsTime.h:334
LIBCOM_API int epicsStdCall epicsTimeToTM(struct tm *pDest, unsigned long *pNSecDest, const epicsTimeStamp *pSrc)
Convert epicsTimeStamp to struct tm in local time zone.
C++ only ANSI C sruct tm with nanoseconds, UTC.
Definition: epicsTime.h:278
LIBCOM_API int epicsStdCall epicsTimeGetCurrent(epicsTimeStamp *pDest)
Get current time into *pDest.
BSD and SRV5 Unix timestamp.
Definition: epicsTime.h:65
LIBCOM_API int epicsStdCall epicsTime_gmtime(const time_t *clock, struct tm *result)
Break down a time_t into a struct tm in the UTC timezone.
C++ Event number wrapper class.
Definition: epicsTime.h:300
LIBCOM_API epicsUInt64 epicsMonotonicGet(void)
Fetch monotonic counter, returns the number of nanoseconds since some unspecified time...
size_t strftime(char *pBuff, size_t bufLength, const char *pFormat) const
Convert to string in user-specified format.
Definition: epicsTime.h:568
epicsTime(const struct timespec &replace)
Construct from struct timespec
Definition: epicsTime.h:455
std::logic_error formatProblemWithStructTM
Exception: Bad field(s) in struct tm
Definition: epicsTime.h:328
struct tm ansi_tm
ANSI C time details.
Definition: epicsTime.h:279
LIBCOM_API void epicsStdCall epicsTimeShow(const epicsTimeStamp *, unsigned interestLevel)
Dump current state to stdout.
LIBCOM_API int epicsStdCall epicsTimeLessThan(const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight)
left was before right
Defined by POSIX Real Time.
epicsTime(const gm_tm_nano_sec &replace)
Construct from struct tm in UTC/GMT.
Definition: epicsTime.h:434
LIBCOM_API int epicsStdCall epicsTimeFromTM(epicsTimeStamp *pDest, const struct tm *pSrc, unsigned long nSecSrc)
Set epicsTimeStamp from struct tm in local time zone.
LIBCOM_API int epicsStdCall epicsTimeGreaterThanEqual(const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight)
right was not before left
static epicsTime getMonotonic()
Get current monotonic time.
Definition: epicsTime.h:360
LIBCOM_API int epicsStdCall epicsTimeFromGMTM(epicsTimeStamp *pDest, const struct tm *pSrc, unsigned long nSecSrc)
Set epicsTimeStamp from struct tm in UTC/GMT.
void show(unsigned interestLevel) const
Dump current state to standard out.
Definition: epicsTime.h:573
LIBCOM_API int epicsTimeGetMonotonic(epicsTimeStamp *pDest)
Get monotonic time into *pDest.
LIBCOM_API int epicsStdCall epicsTimeNotEqual(const epicsTimeStamp *pLeft, const epicsTimeStamp *pRight)
left not equal to right
LIBCOM_API int epicsTimeGetEventInt(epicsTimeStamp *pDest, int eventNumber)
Get time of event eventNumber into *pDest (ISR-safe)
LIBCOM_API size_t epicsStdCall epicsTimeToStrftime(char *pBuff, size_t bufLength, const char *pFormat, const epicsTimeStamp *pTS)
Convert epicsTimeStamp to string. See epicsTime::strftime()
LIBCOM_API void epicsStdCall epicsTimeAddSeconds(epicsTimeStamp *pDest, double secondsToAdd)
Add some number of seconds to dest.
LIBCOM_API int epicsStdCall epicsTimeToTime_t(time_t *pDest, const epicsTimeStamp *pSrc)
Convert epicsTimeStamp to ANSI C time_t.
epicsTime(const time_t_wrapper &replace)
Construct from ANSI C time_t.
Definition: epicsTime.h:390