mbgtools-lx  4.2.8
timeutil.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: timeutil.h 1.10 2019/02/11 09:49:46 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Definitions and prototypes for timeutil.c
10  *
11  * -----------------------------------------------------------------------
12  * $Log: timeutil.h $
13  * Revision 1.10 2019/02/11 09:49:46 martin
14  * Support the mingw build environment.
15  * Fixed build for targets that don't support 64 bit types.
16  * Revision 1.9 2018/12/11 15:36:40 martin
17  * cvt_to_time_t() now expects an int64_t type parameter.
18  * Revision 1.8 2018/11/29 15:32:04Z martin
19  * Moved some inline functions here.
20  * Include mbgtime.h.
21  * Revision 1.7 2018/01/30 09:54:12Z martin
22  * Updated function prototypes.
23  * Revision 1.6 2018/01/15 18:31:21Z martin
24  * Updated function prototypes.
25  * Revision 1.5 2017/11/16 13:34:09 philipp
26  * Extended ISO printing helper functions
27  * Revision 1.4 2017/11/16 11:33:28 philipp
28  * Added functions to print a time_t to ISO format
29  * Revision 1.3 2017/07/05 07:15:00 martin
30  * Provide basic support for clock_gettime()/clock_settime()
31  * compatible functions for Windows.
32  * Updated function prototypes.
33  * Revision 1.1 2016/07/15 14:14:20Z martin
34  * Initial revision
35  *
36  **************************************************************************/
37 
38 #ifndef _TIMEUTIL_H
39 #define _TIMEUTIL_H
40 
41 /* Other headers to be included */
42 
43 #include <gpsdefs.h>
44 #include <mbgerror.h>
45 #include <mbgtime.h>
46 
47 #include <time.h>
48 #include <stddef.h>
49 
50 
51 #ifdef _TIMEUTIL
52  #define _ext
53  #define _DO_INIT
54 #else
55  #define _ext extern
56 #endif
57 
58 
59 /* Start of header body */
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 
66 // exclude for MinGW
67 #if defined( MBG_TGT_WIN32 ) || defined( MBG_TGT_DOS )
68  #if !defined( MBG_TGT_MINGW )
69  typedef int clockid_t;
70  #define clockid_t clockid_t
71 
72  #define CLOCK_REALTIME ( (clockid_t) 0 )
73  #endif
74 #endif
75 
76 
77 
78 #if defined( MBG_TGT_WIN32 )
79 
80 #define __const__ const
81 
88 typedef VOID (WINAPI *GSTAFT_FNC)(LPFILETIME lpSystemTimeAsFileTime);
89 
90 _ext GSTAFT_FNC gstaft_fnc
91 #ifdef _DO_INIT
92  = GetSystemTimeAsFileTime
93 #endif
94 ;
95 
96 #endif
97 
98 
99 
100 #if !defined( MBG_TGT_MISSING_64_BIT_TYPES )
101  typedef int64_t mbg_time_t; // we try to always use 64 bit types
102 #else
103  typedef time_t mbg_time_t; // fall back to the default
104 #endif
105 
106 
107 static __mbg_inline
108 time_t cvt_to_time_t( mbg_time_t t )
109 {
110  // Eventually we can do some epoch check / conversion here.
111  return (time_t) t;
112 
113 } // cvt_to_time_t
114 
115 
116 
117 static __mbg_inline
118 int mbg_gmtime( struct tm *p_tm, const time_t *p_time )
119 {
120  struct tm *p_tm_tmp = gmtime( p_time );
121 
122  if ( p_tm_tmp == NULL ) // conversion failed
123  return mbg_get_last_error( NULL );
124 
125  *p_tm = *p_tm_tmp;
126 
127  return MBG_SUCCESS;
128 
129 } // mbg_gmtime
130 
131 
132 
133 #if !defined( MBG_TGT_MISSING_64_BIT_TYPES )
134 
135 static __mbg_inline /*HDR*/
137 {
138  return (double) t->seconds + ( ( (double) t->fractions ) / NTP_FRAC_PER_SEC );
139 
140 } // ntp_tstamp_to_double
141 
142 
143 
144 static __mbg_inline /*HDR*/
145 ulong ntp_frac_to_nsec( uint32_t frac )
146 {
147  uint64_t tmp = ( (uint64_t) frac * NSEC_PER_SEC ) / NTP_FRAC_PER_SEC;
148 
149 #if 0 // TODO Currently not supported
150  if ( tmp >= NSEC_PER_SEC )
151  mbglog( LOG_WARNING, "Range overflow in ntp_frac_to_nsec: 0x%X -> %Lu",
152  frac, tmp );
153 #endif
154 
155  return (ulong) tmp;
156 
157 } // ntp_frac_to_nsec
158 
159 
160 
161 static __mbg_inline /*HDR*/
162 uint32_t nsec_to_ntp_frac( ulong nsec )
163 {
164  uint64_t tmp;
165 
166  tmp = ( (uint64_t) nsec * NTP_FRAC_PER_SEC ) / NSEC_PER_SEC;
167 
168 #if 0 // TODO Currently not supported
169  if ( tmp >= NTP_FRAC_PER_SEC )
170  mbglog( LOG_WARNING, "Range overflow in nsec_to_ntp_frac: %lu -> 0x%LX",
171  (ulong) nsec, tmp );
172 #endif
173 
174  return (uint32_t) tmp;
175 
176 } // nsec_to_ntp_frac
177 
178 
179 
180 static __mbg_inline /*HDR*/
181 void timespec_to_ntp_tstamp( NTP_TSTAMP *t_ntp, const struct timespec *t_ts )
182 {
183  t_ntp->seconds = (uint32_t) t_ts->tv_sec + NTP_SEC_BIAS;
184  t_ntp->fractions = nsec_to_ntp_frac( t_ts->tv_nsec );
185 
186 } // timespec_to_ntp_tstamp
187 
188 
189 
190 static __mbg_inline /*HDR*/
191 void ntp_tstamp_to_timespec( struct timespec *t_ts, const NTP_TSTAMP *t_ntp )
192 {
193  t_ts->tv_sec = t_ntp->seconds - NTP_SEC_BIAS;
194  t_ts->tv_nsec = ntp_frac_to_nsec( t_ntp->fractions );
195 
196 } // ntp_tstamp_to_timespec
197 
198 #endif // !defined( MBG_TGT_MISSING_64_BIT_TYPES )
199 
200 
201 
202 static __mbg_inline /*HDR*/
203 int timespec_is_set( const struct timespec *p )
204 {
205  return p->tv_sec != 0 || p->tv_nsec != 0;
206 
207 } // timespec_is_set
208 
209 
210 
211 //
212 // compute the difference of two timespec variables
213 //
214 static __mbg_inline /*HDR*/
215 double delta_timespec_d_s( const struct timespec *ts,
216  const struct timespec *ts_ref )
217 {
218  return ( (double) ts->tv_sec - (double) ts_ref->tv_sec )
219  + ( (double) ts->tv_nsec - (double) ts_ref->tv_nsec ) / NSEC_PER_SEC;
220 
221 } // delta_timespec_d_s
222 
223 
224 
225 #if !defined( MBG_TGT_MISSING_64_BIT_TYPES )
226 
227 static __mbg_inline /*HDR*/
228 int64_t delta_timespec_ll_ns( const struct timespec *ts,
229  const struct timespec *ts_ref )
230 {
231  int64_t tmp = ts->tv_sec - ts_ref->tv_sec;
232  tmp = ( tmp * NSEC_PER_SEC ) + ( ts->tv_nsec - ts_ref->tv_nsec );
233 
234  return tmp;
235 
236 } // delta_timespec_ll_ns
237 
238 #endif // !defined( MBG_TGT_MISSING_64_BIT_TYPES )
239 
240 
241 
242 /* ----- function prototypes begin ----- */
243 
244 /* This section was generated automatically */
245 /* by MAKEHDR, do not remove the comments. */
246 
247  int snprint_gmtime_error( char *s, size_t max_len, int mbg_errno, time_t t, const char *calling_fnc ) ;
248  int mbg_clock_gettime( clockid_t clock_id, struct timespec *tp ) ;
249  int mbg_clock_settime( clockid_t clock_id, const struct timespec *tp ) ;
250  void check_precise_time_api( void ) ;
263  int snprint_utc_offs( char *s, size_t max_len, const char *info, long utc_offs ) ;
264 
265  int snprint_time_t_to_iso( time_t tstamp, int offs_hours, char *buf, size_t len ) ;
266  int snprint_time_t_to_iso_ms( time_t tstamp, int offs_hours, time_t frac, char *buf, size_t len ) ;
267  int snprint_time_t_to_iso_us( time_t tstamp, int offs_hours, time_t frac, char *buf, size_t len ) ;
268  int snprint_time_t_to_iso_ns( time_t tstamp, int offs_hours, time_t frac, char *buf, size_t len ) ;
269  int snprint_ntp_tstamp_to_iso_ms( const NTP_TSTAMP *ts, char *buf, size_t len ) ;
270  int snprint_ntp_tstamp_to_iso_us( const NTP_TSTAMP *ts, char *buf, size_t len ) ;
271  int snprint_ntp_tstamp_to_iso_ns( const NTP_TSTAMP *ts, char *buf, size_t len ) ;
272 
273 /* ----- function prototypes end ----- */
274 
275 #ifdef __cplusplus
276 }
277 #endif
278 
279 
280 /* End of header body */
281 
282 #undef _ext
283 #undef _DO_INIT
284 
285 #endif /* _TIMEUTIL_H */
uint32_t fractions
binary fractional part of a second, 0xFFFFFFFF -> 0.9999999... s (resolution 2^-32s =~ 233 ps) ...
Definition: gpsdefs.h:16487
int snprint_time_t_to_iso(time_t tstamp, int offs_hours, char *buf, size_t len)
Definition: timeutil.c:243
int snprint_time_t_to_iso_ns(time_t tstamp, int offs_hours, time_t frac, char *buf, size_t len)
Definition: timeutil.c:309
Structure that represents a timestamp in NTP Timestamp Format.
Definition: gpsdefs.h:16484
int snprint_ntp_tstamp_to_iso_ns(const NTP_TSTAMP *ts, char *buf, size_t len)
Definition: timeutil.c:346
int snprint_ntp_tstamp_to_iso_us(const NTP_TSTAMP *ts, char *buf, size_t len)
Definition: timeutil.c:334
void check_precise_time_api(void)
static __mbg_inline double ntp_tstamp_to_double(const NTP_TSTAMP *t)
Definition: timeutil.h:136
static __mbg_inline void ntp_tstamp_to_timespec(struct timespec *t_ts, const NTP_TSTAMP *t_ntp)
Definition: timeutil.h:191
static __mbg_inline uint32_t nsec_to_ntp_frac(ulong nsec)
Definition: timeutil.h:162
static __mbg_inline time_t cvt_to_time_t(mbg_time_t t)
Definition: timeutil.h:108
static __mbg_inline ulong ntp_frac_to_nsec(uint32_t frac)
Definition: timeutil.h:145
int snprint_ntp_tstamp_to_iso_ms(const NTP_TSTAMP *ts, char *buf, size_t len)
Definition: timeutil.c:322
#define NTP_FRAC_PER_SEC
Definition: mbgtime.h:145
int64_t mbg_time_t
Definition: timeutil.h:101
static __mbg_inline double delta_timespec_d_s(const struct timespec *ts, const struct timespec *ts_ref)
Definition: timeutil.h:215
static __mbg_inline int timespec_is_set(const struct timespec *p)
Definition: timeutil.h:203
int mbg_clock_gettime(clockid_t clock_id, struct timespec *tp)
#define _ext
Definition: timeutil.h:55
static __mbg_inline void timespec_to_ntp_tstamp(NTP_TSTAMP *t_ntp, const struct timespec *t_ts)
Definition: timeutil.h:181
__int64 int64_t
Definition: words.h:249
int snprint_time_t_to_iso_ms(time_t tstamp, int offs_hours, time_t frac, char *buf, size_t len)
Definition: timeutil.c:283
#define MBG_SUCCESS
Error codes used with Meinberg devices and drivers.
Definition: mbgerror.h:259
uint32_t seconds
seconds since NTP epoch, see NTP_SEC_BIAS
Definition: gpsdefs.h:16486
static __mbg_inline int mbg_gmtime(struct tm *p_tm, const time_t *p_time)
Definition: timeutil.h:118
int snprint_gmtime_error(char *s, size_t max_len, int mbg_errno, time_t t, const char *calling_fnc)
Definition: timeutil.c:55
#define NSEC_PER_SEC
Definition: mbgtime.h:264
int snprint_time_t_to_iso_us(time_t tstamp, int offs_hours, time_t frac, char *buf, size_t len)
Definition: timeutil.c:296
unsigned __int64 uint64_t
Definition: words.h:250
static __mbg_inline int64_t delta_timespec_ll_ns(const struct timespec *ts, const struct timespec *ts_ref)
Definition: timeutil.h:228
int snprint_utc_offs(char *s, size_t max_len, const char *info, long utc_offs)
Print a UTC offset into a string.
Definition: timeutil.c:211
unsigned long ulong
Definition: words.h:292
int mbg_get_last_error(const char *info)
Get and translate last error after non-socket function call.
Definition: mbgerror.c:993
int mbg_clock_settime(clockid_t clock_id, const struct timespec *tp)
#define NTP_SEC_BIAS
NTP epoch bias from ordinary time_t epoch.
Definition: mbgtime.h:142