Grid Community Toolkit  6.2.1629922860 (tag: v6.2.20210826)
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
globus_time.h
Go to the documentation of this file.
1 /*
2  * Copyright 1999-2006 University of Chicago
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
22 #if !defined(GLOBUS_TIME_H)
23 #define GLOBUS_TIME_H
24 
25 #include "globus_types.h"
26 #include <time.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define GLOBUS_I_TIME_INFINITY_SEC INT_MAX
33 #define GLOBUS_I_TIME_INFINITY_NSEC INT_MAX
34 #define GLOBUS_I_TIME_INFINITY_USEC INT_MAX
35 
36 #if defined(_WIN32) && !defined(_POSIX)
37 typedef struct globus_abstime_s
38 {
39  time_t tv_sec;
40  long tv_nsec;
41 } globus_abstime_t;
42 #else
43 typedef struct timespec globus_abstime_t;
44 #endif
45 
46 typedef struct timeval globus_reltime_t;
47 
51 #define GlobusTimeAbstimeSet(Abstime, Sec, USec) \
52 { \
53  GlobusTimeAbstimeGetCurrent(Abstime); \
54  (Abstime).tv_nsec += (USec * 1000); \
55  if((Abstime).tv_nsec >= 1000000000) \
56  { \
57  (Abstime).tv_sec += ((Abstime).tv_nsec / 1000000000);\
58  (Abstime).tv_nsec = (Abstime).tv_nsec % 1000000000; \
59  } \
60  (Abstime).tv_sec += Sec; \
61 }
62 
65 #define GlobusTimeAbstimeGet(Abstime, Sec, USec) \
66 { \
67  Sec = (Abstime).tv_sec; \
68  USec = ((Abstime).tv_nsec / 1000); \
69 }
70 
74 #define GlobusTimeReltimeSet(Reltime, Sec, USec) \
75 { \
76  (Reltime).tv_usec = (USec); \
77  (Reltime).tv_sec = Sec; \
78  if((Reltime).tv_usec >= 1000000) \
79  { \
80  (Reltime).tv_sec += ((Reltime).tv_usec / 1000000);\
81  (Reltime).tv_usec = (Reltime).tv_usec % 1000000; \
82  } \
83 }
84 
85 #define GlobusTimeReltimeGet(Reltime, Sec, USec) \
86 { \
87  (USec) = (Reltime).tv_usec; \
88  (Sec) = (Reltime).tv_sec; \
89 }
90 
91 #define GlobusTimeAbstimePrintf(Abstime) \
92 { \
93  printf("sec -->%lu\n", (unsigned long) (Abstime).tv_sec); \
94  printf("nsec -->%lu\n", (unsigned long) (Abstime).tv_nsec); \
95 }
96 
97 #define GlobusTimeReltimePrintf(Reltime) \
98 { \
99  printf("sec -->%lu\n", (unsigned long) (Reltime).tv_sec); \
100  printf("usec -->%lu\n", (unsigned long) (Reltime).tv_usec); \
101 }
102 
106 #define GlobusTimeAbstimeDiff(Reltime, T1, T2) \
107 { \
108  int __res = globus_abstime_cmp(&(T1), &(T2)); \
109  if(__res < 0) \
110  { \
111  (Reltime).tv_sec = (T2).tv_sec - (T1).tv_sec; \
112  (Reltime).tv_usec = \
113  (((T2).tv_nsec - (T1).tv_nsec) / 1000); \
114  if((Reltime).tv_usec < 0) \
115  { \
116  (Reltime).tv_sec--; \
117  (Reltime).tv_usec += 1000000; \
118  } \
119  } \
120  else if(__res > 0) \
121  { \
122  (Reltime).tv_sec = (T1).tv_sec - (T2).tv_sec; \
123  (Reltime).tv_usec = \
124  (((T1).tv_nsec - (T2).tv_nsec) / 1000); \
125  if((Reltime).tv_usec < 0) \
126  { \
127  (Reltime).tv_sec--; \
128  (Reltime).tv_usec += 1000000; \
129  } \
130  } \
131  else \
132  { \
133  (Reltime).tv_sec = 0; \
134  (Reltime).tv_usec = 0; \
135  } \
136 }
137 
138 #define GlobusTimeReltimeDiff(Reltime, T1, T2) \
139 { \
140  int __res = globus_reltime_cmp(&(T1), &(T2)); \
141  if(__res < 0) \
142  { \
143  (Reltime).tv_sec = (T2).tv_sec - (T1).tv_sec; \
144  (Reltime).tv_usec = \
145  ((T2).tv_usec - (T1).tv_usec); \
146  if((Reltime).tv_usec < 0) \
147  { \
148  (Reltime).tv_sec--; \
149  (Reltime).tv_usec += 1000000; \
150  } \
151  } \
152  else if(__res > 0) \
153  { \
154  (Reltime).tv_sec = (T1).tv_sec - (T2).tv_sec; \
155  (Reltime).tv_usec = \
156  ((T1).tv_usec - (T2).tv_usec); \
157  if((Reltime).tv_usec < 0) \
158  { \
159  (Reltime).tv_sec--; \
160  (Reltime).tv_usec += 1000000; \
161  } \
162  } \
163  else \
164  { \
165  (Reltime).tv_sec = 0; \
166  (Reltime).tv_usec = 0; \
167  } \
168 }
169 
173 #define GlobusTimeReltimeToUSec(SlpInt, Reltime) \
174 { \
175  SlpInt = ((Reltime).tv_sec * 1000000) + \
176  ((Reltime).tv_usec); \
177 }
178 
182 #define GlobusTimeReltimeToMilliSec( Milliseconds, Reltime) \
183 { \
184  Milliseconds = ((Reltime).tv_sec * 1000) + \
185  ((Reltime).tv_usec)/ 1000; \
186 }
187 
191 #define GlobusTimeAbstimeInc(Abstime, Reltime) \
192 { \
193  (Abstime).tv_nsec += ((Reltime).tv_usec * 1000); \
194  if((Abstime).tv_nsec >= 1000000000) \
195  { \
196  (Abstime).tv_sec++; \
197  (Abstime).tv_nsec -= 1000000000; \
198  } \
199  (Abstime).tv_sec += (Reltime).tv_sec; \
200 }
201 
202 #define GlobusTimeAbstimeDec(Abstime, Reltime) \
203 { \
204  (Abstime).tv_nsec -= ((Reltime).tv_usec * 1000); \
205  if((Abstime).tv_nsec < 0) \
206  { \
207  (Abstime).tv_sec--; \
208  (Abstime).tv_nsec += 1000000000; \
209  } \
210  (Abstime).tv_sec -= (Reltime).tv_sec; \
211 }
212 
213 
217 #if defined(_WIN32)
218 # define GlobusTimeAbstimeGetCurrent(Abstime) \
219  { \
220  struct _timeb timebuffer; \
221  \
222  _ftime(&timebuffer); \
223  (Abstime).tv_sec = timebuffer.time; \
224  (Abstime).tv_nsec = (timebuffer.millitm * 1000000); \
225  }
226 #else
227 # define GlobusTimeAbstimeGetCurrent(Abstime) \
228  { \
229  struct timeval __time; \
230  \
231  gettimeofday(&__time, GLOBUS_NULL); \
232  (Abstime).tv_sec = __time.tv_sec; \
233  (Abstime).tv_nsec = (__time.tv_usec * 1000); \
234  }
235 #endif
236 
240 #define GlobusTimeAbstimeCopy(Dest, Src) \
241 { \
242  (Dest).tv_sec = (Src).tv_sec; \
243  (Dest).tv_nsec = (Src).tv_nsec; \
244 }
245 
249 #define GlobusTimeReltimeCopy(Dest, Src) \
250 { \
251  (Dest).tv_sec = (Src).tv_sec; \
252  (Dest).tv_usec = (Src).tv_usec; \
253 }
254 
258 #define GlobusTimeReltimeMultiply(Reltime, Factor) \
259 { \
260  (Reltime).tv_usec *= Factor; \
261  (Reltime).tv_sec *= Factor; \
262  \
263  if((Reltime).tv_usec >= 1000000) \
264  { \
265  (Reltime).tv_sec += ((Reltime).tv_usec / 1000000);\
266  (Reltime).tv_usec = (Reltime).tv_usec % 1000000; \
267  } \
268 }
269 
273 #define GlobusTimeReltimeDivide(Reltime, Factor) \
274 { \
275  (Reltime).tv_usec /= Factor; \
276  (Reltime).tv_sec /= Factor; \
277 }
278 
279 extern const globus_abstime_t globus_i_abstime_infinity;
280 extern const globus_abstime_t globus_i_abstime_zero;
281 extern const globus_reltime_t globus_i_reltime_infinity;
282 extern const globus_reltime_t globus_i_reltime_zero;
283 
292  const globus_abstime_t * abstime);
293 
299  const globus_abstime_t * abstime);
300 
306  const globus_reltime_t * reltime);
307 
318 int
320  const globus_abstime_t * abstime_1,
321  const globus_abstime_t * abstime_2);
322 
333 int
335  const globus_reltime_t * reltime_1,
336  const globus_reltime_t * reltime_2);
337 
338 
339 #ifdef __cplusplus
340 }
341 #endif
342 #endif /* GLOBUS_TIME_H */
Common Primitive Types.
int globus_reltime_cmp(const globus_reltime_t *reltime_1, const globus_reltime_t *reltime_2)
Definition: globus_time.c:131
globus_bool_t globus_time_has_expired(const globus_abstime_t *abstime)
Definition: globus_time.c:46
int globus_bool_t
Boolean type.
Definition: globus_types.h:93
globus_bool_t globus_time_reltime_is_infinity(const globus_reltime_t *reltime)
Definition: globus_time.c:75
int globus_abstime_cmp(const globus_abstime_t *abstime_1, const globus_abstime_t *abstime_2)
Definition: globus_time.c:88
globus_bool_t globus_time_abstime_is_infinity(const globus_abstime_t *abstime)
Definition: globus_time.c:62