This is Unofficial EPICS BASE Doxygen Site
osdTime.cpp
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2008 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 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 
10 #include <stddef.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <errno.h>
15 
16 #define EPICS_EXPOSE_LIBCOM_MONOTONIC_PRIVATE
17 #include "osiSock.h"
18 
19 #include "cantProceed.h"
20 #include "epicsTime.h"
21 #include "generalTimeSup.h"
22 
23 #ifdef CLOCK_REALTIME
24  #include "osiClockTime.h"
25 
26  #define TIME_INIT ClockTime_Init(CLOCKTIME_NOSYNC)
27 #else
28  /* Some posix systems may not have CLOCK_REALTIME */
29 
30  #define TIME_INIT generalTimeCurrentTpRegister("GetTimeOfDay", \
31  LAST_RESORT_PRIORITY, osdTimeGetCurrent)
32 
33  extern "C" {
35  {
36  struct timeval tv;
37  struct timezone tz;
38 
39  if (gettimeofday (&tv, &tz))
40  return errno;
41 
42  *pDest = epicsTime(tv);
43  return epicsTimeOK;
44  }
45  } // extern "C"
46 #endif
47 
48 #ifdef __CYGWIN__
49 int clock_settime(clockid_t clock, const timespec *tp)
50 {
51  return -EFAULT;
52 }
53 #endif
54 
55 
56 static int timeRegister(void)
57 {
58  TIME_INIT;
59 
61  return 1;
62 }
63 static int done = timeRegister();
64 
65 
66 int epicsTime_gmtime ( const time_t *pAnsiTime,
67  struct tm *pTM )
68 {
69  struct tm * pRet = gmtime_r ( pAnsiTime, pTM );
70  if ( pRet ) {
71  return epicsTimeOK;
72  }
73  else {
74  return errno;
75  }
76 }
77 
78 int epicsTime_localtime ( const time_t *clock,
79  struct tm *result )
80 {
81  struct tm * pRet = localtime_r ( clock, result );
82  if ( pRet ) {
83  return epicsTimeOK;
84  }
85  else {
86  return errno;
87  }
88 }
89 
90 extern "C" LIBCOM_API void
91  convertDoubleToWakeTime(double timeout,struct timespec *wakeTime)
92 {
93  struct timespec now, wait;
94  int status;
95 
96  if (timeout < 0.0)
97  timeout = 0.0;
98  else if (timeout > 60 * 60 * 24 * 3652.5)
99  timeout = 60 * 60 * 24 * 3652.5; /* 10 years */
100 
101 #ifdef CLOCK_REALTIME
102  status = clock_gettime(CLOCK_REALTIME, &now);
103 #else
104  {
105  struct timeval tv;
106  struct timezone tz;
107  status = gettimeofday(&tv, &tz);
108  now.tv_sec = tv.tv_sec;
109  now.tv_nsec = tv.tv_usec * 1000;
110  }
111 #endif
112  if (status) {
113  perror("convertDoubleToWakeTime");
114  cantProceed("convertDoubleToWakeTime");
115  }
116 
117  wait.tv_sec = static_cast< time_t >(timeout);
118  wait.tv_nsec = static_cast< long >((timeout - (double)wait.tv_sec) * 1e9);
119 
120  wakeTime->tv_sec = now.tv_sec + wait.tv_sec;
121  wakeTime->tv_nsec = now.tv_nsec + wait.tv_nsec;
122  if (wakeTime->tv_nsec >= 1000000000L) {
123  wakeTime->tv_nsec -= 1000000000L;
124  ++wakeTime->tv_sec;
125  }
126 }
double timeout
Definition: pvutils.cpp:25
pvac::PutEvent result
Definition: clientSync.cpp:117
pvd::Status status
LIBCOM_API void convertDoubleToWakeTime(double timeout, struct timespec *wakeTime)
Definition: osdTime.cpp:68
#define TIME_INIT
Definition: osdTime.cpp:30
void osdMonotonicInit(void)
Definition: osdMonotonic.c:19
#define epicsTimeOK
Success.
Definition: epicsTime.h:339
time_t tv_sec
Definition: osdTime.h:22
long tv_nsec
Definition: osdTime.h:23
BSD and SRV5 Unix timestamp.
Definition: epicsTime.h:52
EPICS time stamp, for use from C code.
Definition: epicsTime.h:33
LIBCOM_API void cantProceed(const char *msg,...)
Definition: cantProceed.c:54
int osdTimeGetCurrent(epicsTimeStamp *pDest)
Definition: osdTime.cpp:28
Defined by POSIX Real Time.
Definition: osdTime.h:21
Routines for code that can&#39;t continue or return after an error.
EPICS time-stamps (epicsTimeStamp), epicsTime C++ class and C functions for handling wall-clock times...
int epicsTime_gmtime(const time_t *pAnsiTime, struct tm *pTM)
Break down a time_t into a struct tm in the UTC timezone.
Definition: osdTime.cpp:55
int epicsTime_localtime(const time_t *clock, struct tm *result)
Break down a time_t into a struct tm in the local timezone.
Definition: osdTime.cpp:61