]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - include/timespec_operations.h
Adding FOSA_CPP_BEING|END_DECLS to allow interfacing with C++
[frescor/fosa.git] / include / timespec_operations.h
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 FRESCOR consortium partners:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politécnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org for a link to partners' websites
17 //
18 //           FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 //   FSF API web pages: http://marte.unican.es/fsf/docs
32 //                      http://shark.sssup.it/contrib/first/docs/
33 //
34 //  This file is part of FRSH API
35 //
36 //  FRSH API is free software; you can  redistribute it and/or  modify
37 //  it under the terms of  the GNU General Public License as published by
38 //  the Free Software Foundation;  either  version 2, or (at  your option)
39 //  any later version.
40 //
41 //  FRSH API  is distributed  in  the hope  that  it  will  be useful,  but
42 //  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
43 //  MERCHANTABILITY  or  FITNESS FOR  A  PARTICULAR PURPOSE. See  the  GNU
44 //  General Public License for more details.
45 //
46 //  You should have  received a  copy of  the  GNU  General Public License
47 //  distributed  with  FRSH API;  see file COPYING.   If not,  write to the
48 //  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
49 //  02111-1307, USA.
50 //
51 //  As a special exception, if you include this header file into source
52 //  files to be compiled, this header file does not by itself cause
53 //  the resulting executable to be covered by the GNU General Public
54 //  License.  This exception does not however invalidate any other
55 //  reasons why the executable file might be covered by the GNU General
56 //  Public License.
57 // -----------------------------------------------------------------------
58 /*-----------------------------------------------------------------------
59  *                                                         V1.0  Dec 2001
60  *
61  *                    't i m e s p e c _ o p e r a t i o n s'
62  *
63  *                                      H
64  *
65  * File 'timespec_operations.h'                                   by MAR.
66  *                           (july 2002)  transformed into macros by Jul.
67  * Some basic operations with the type 'timespec'.
68  *
69  * ----------------------------------------------------------------------
70  *  Copyright (C) 2001   Universidad de Cantabria, SPAIN
71  *
72  *  Authors: Mario Aldea Rivas          aldeam@ctr.unican.es
73  *           Michael Gonzalez Harbour      mgh@ctr.unican.es
74  *
75  * MaRTE OS  is free software; you can  redistribute it and/or  modify it
76  * under the terms of the GNU General Public License  as published by the
77  * Free Software Foundation;  either  version 2, or (at  your option) any
78  * later version.
79  *
80  * MaRTE OS  is distributed  in the  hope  that  it will be   useful, but
81  * WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
82  * MERCHANTABILITY  or  FITNESS FOR A  PARTICULAR PURPOSE.    See the GNU
83  * General Public License for more details.
84  *
85  * You should have received  a  copy of  the  GNU General Public  License
86  * distributed with MaRTE  OS;  see file COPYING.   If not,  write to the
87  * Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
88  * 02111-1307, USA.
89  *
90  * As a  special exception, if you  link this  unit  with other  files to
91  * produce an   executable,   this unit  does  not  by  itself cause  the
92  * resulting executable to be covered by the  GNU General Public License.
93  * This exception does  not however invalidate  any other reasons why the
94  * executable file might be covered by the GNU Public License.
95  *
96  *---------------------------------------------------------------------------*/
97 // 16-Jul-2007 SANGORRIN corrections to msec_addto_timespec and msec2timespec
98 // 23-Jul-2007 SANGORRIN moved t2d and d2t from frsh_time_operations
99 // (TODO: test for __ALL__ the functions)
100 // -----------------------------------------------------------------------
101
102 #ifndef _MARTE_MISC_TIMESPEC_OPERATIONS_H_
103 #define _MARTE_MISC_TIMESPEC_OPERATIONS_H_
104
105 /**
106  * @file timespec_operations.h
107  **/
108
109 #include <time.h> // for timespec
110 #include <string.h> // for memset
111
112 FOSA_CPP_BEGIN_DECLS
113
114 #define smaller_timespec(t1, t2) \
115  ( \
116   (t1)->tv_sec < (t2)->tv_sec || ((t1)->tv_sec == (t2)->tv_sec &&   \
117   (t1)->tv_nsec < (t2)->tv_nsec) \
118  )
119
120 #define smaller_or_equal_timespec(t1, t2) \
121  ( \
122   (t1)->tv_sec < (t2)->tv_sec || ((t1)->tv_sec == (t2)->tv_sec &&    \
123   (t1)->tv_nsec <= (t2)->tv_nsec) \
124  )
125
126 #define incr_timespec(t1, t2) \
127 do { \
128   (t1)->tv_sec += (t2)->tv_sec; \
129   (t1)->tv_nsec += (t2)->tv_nsec; \
130   if ((t1)->tv_nsec >= 1000000000) { \
131     (t1)->tv_sec ++; \
132     (t1)->tv_nsec -= 1000000000; \
133   } \
134 } while (0)
135
136 #define decr_timespec(t1, t2) \
137 do { \
138   if ((t1)->tv_nsec < (t2)->tv_nsec) { \
139     (t1)->tv_sec -= (t2)->tv_sec + 1; \
140     (t1)->tv_nsec = (t1)->tv_nsec + 1000000000 - (t2)->tv_nsec; \
141   } else { \
142     (t1)->tv_sec -= (t2)->tv_sec; \
143     (t1)->tv_nsec -= (t2)->tv_nsec; \
144   } \
145 } while (0)
146
147 #define  add_timespec( sum , t1 , t2 ) \
148 do { \
149   (sum)->tv_sec  = (t1)->tv_sec  + (t2)->tv_sec; \
150   (sum)->tv_nsec = (t1)->tv_nsec + (t2)->tv_nsec; \
151   if ((sum)->tv_nsec >= 1000000000) { \
152     (sum)->tv_sec ++; \
153     (sum)->tv_nsec -= 1000000000; \
154   } \
155 } while (0)
156
157 #define float_to_timespec( f1 , t1 ) \
158 ( \
159   (t1)->tv_sec = (int)(f1), \
160   (t1)->tv_nsec = (int)(((f1)-(float)((t1)->tv_sec))*1000000000.0), \
161   (t1) \
162 )
163
164 #define float_to_timespec_value(f1, t1) \
165 ( \
166   (t1).tv_sec = (int)(f1), \
167   (t1).tv_nsec = (int)(((f1)-(float)((t1).tv_sec))*1000000000.0), \
168   (t1) \
169 )
170
171 //---------------//
172 // msec2timespec //
173 //---------------//
174
175 static inline struct timespec msec2timespec(long msec)
176 {
177     struct timespec result = {-1, -1};
178
179     if (msec >= 1000) {
180         result.tv_sec = msec/1000;
181         result.tv_nsec = (msec % 1000) * 1000000;
182     } else {
183         result.tv_sec = 0;
184         result.tv_nsec = msec * 1000000;
185     }
186
187     return result;
188 }
189
190 static inline long timespec2msec(const struct timespec *timespec)
191 {
192     
193     return (timespec->tv_sec % 2147482) * 1000 + timespec->tv_nsec/1000000;
194 }
195
196
197 #define HOURS_IN_MSECS 3600000
198 #define MINUTES_IN_MSECS 60000
199
200 #define HOURS_IN_TS     1000000
201 #define MINUTES_IN_TS    100000
202
203 static inline long timespec2msects(const struct timespec *timespec)
204 {
205     long msec_total = -1;
206     long minutes = -1;
207     long hours = -1;
208
209     long remainder_msecs = -1;
210     long timestamp = -1;
211
212     msec_total = (timespec->tv_sec % 2147482) * 1000 + timespec->tv_nsec/1000000;
213
214     hours = msec_total / HOURS_IN_MSECS;
215     minutes = (msec_total % HOURS_IN_MSECS) / MINUTES_IN_MSECS;
216     remainder_msecs = ( (msec_total % HOURS_IN_MSECS) % MINUTES_IN_MSECS);
217     
218     timestamp = hours*HOURS_IN_TS + minutes*MINUTES_IN_TS + remainder_msecs;
219
220     return timestamp;
221 }
222     
223
224 //------------------------//
225 // timespec_lessthan_msec //
226 //------------------------//
227
228 static inline int timespec_lessthan_msec(struct timespec *timespec, long msec)
229 {
230     struct timespec msec_timespec = {0, 0};
231
232     msec_timespec = msec2timespec(msec);
233     return smaller_timespec(timespec, &msec_timespec);
234 }
235
236 //---------------------//
237 // msec_addto_timespec //
238 //---------------------//
239
240 static inline void msec_addto_timespec(long msec, struct timespec *timespec)
241 {
242     struct timespec msec_timespec = {0, 0};
243
244     msec_timespec = msec2timespec(msec);
245     incr_timespec(timespec, &msec_timespec);
246 }
247
248 //--------------------------//
249 // t2d (timespec to double) //
250 //--------------------------//
251
252 static inline double t2d(struct timespec time)
253 {
254     return time.tv_nsec*0.000000001 + (double)time.tv_sec;
255 }
256
257 //--------------------------//
258 // d2t (double to timespec) //
259 //--------------------------//
260
261 static inline struct timespec d2t(double time)
262 {
263     struct timespec tmp;
264
265     tmp.tv_sec = (long) time;
266     tmp.tv_nsec = (long)((time - (double)tmp.tv_sec) * 1000000000);
267
268     return tmp;
269 }
270
271 FOSA_CPP_BEGIN_DECLS
272
273 #endif /* _MARTE_MISC_TIMESPEC_OPERATIONS_H_ */