This is Unofficial EPICS BASE Doxygen Site
osdTime.cpp
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2013 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 #include <mach/mach.h>
16 #include <mach/clock.h>
17 
18 #define EPICS_EXPOSE_LIBCOM_MONOTONIC_PRIVATE
19 #include "osiSock.h"
20 
21 #include "cantProceed.h"
22 #include "epicsTime.h"
23 #include "generalTimeSup.h"
24 
25 static clock_serv_t host_clock;
26 
27 extern "C" {
29 {
30  mach_timespec_t mts;
31  struct timespec ts;
32 
33  clock_get_time(host_clock, &mts);
34  ts.tv_sec = mts.tv_sec;
35  ts.tv_nsec = mts.tv_nsec;
36  *pDest = epicsTime(ts);
37  return epicsTimeOK;
38 }
39 } // extern "C"
40 
41 
42 static int timeRegister(void)
43 {
44  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &host_clock);
45 
46  generalTimeCurrentTpRegister("MachTime", \
48 
50  return 1;
51 }
52 static int done = timeRegister();
53 
54 
55 int epicsTime_gmtime(const time_t *pAnsiTime, struct tm *pTM)
56 {
57  return gmtime_r(pAnsiTime, pTM) ?
58  epicsTimeOK : errno;
59 }
60 
61 int epicsTime_localtime(const time_t *clock, struct tm *result)
62 {
63  return localtime_r(clock, result) ?
64  epicsTimeOK : errno;
65 }
66 
67 extern "C" LIBCOM_API void
68 convertDoubleToWakeTime(double timeout, struct timespec *wakeTime)
69 {
70  mach_timespec_t now;
71  struct timespec wait;
72 
73  if (timeout < 0.0)
74  timeout = 0.0;
75  else if (timeout > 60 * 60 * 24 * 3652.5)
76  timeout = 60 * 60 * 24 * 3652.5; /* 10 years */
77 
78  clock_get_time(host_clock, &now);
79 
80  wait.tv_sec = static_cast< time_t >(timeout);
81  wait.tv_nsec = static_cast< long >((timeout - (double)wait.tv_sec) * 1e9);
82 
83  wakeTime->tv_sec = now.tv_sec + wait.tv_sec;
84  wakeTime->tv_nsec = now.tv_nsec + wait.tv_nsec;
85  if (wakeTime->tv_nsec >= 1000000000L) {
86  wakeTime->tv_nsec -= 1000000000L;
87  ++wakeTime->tv_sec;
88  }
89 }
double timeout
Definition: pvutils.cpp:25
pvac::PutEvent result
Definition: clientSync.cpp:117
LIBCOM_API void convertDoubleToWakeTime(double timeout, struct timespec *wakeTime)
Definition: osdTime.cpp:68
#define generalTimeCurrentTpRegister
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
#define LAST_RESORT_PRIORITY
EPICS time stamp, for use from C code.
Definition: epicsTime.h:33
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