aboutsummaryrefslogtreecommitdiffstats
path: root/libsimple/time.h
blob: aa92314d2327ee03189cbb004deced9f650d3724 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* See LICENSE file for copyright and license details. */


/**
 * Calculates the sum of two `struct timespec`s
 * 
 * @param   sum     Output parameter for the result
 * @param   augend  One of the terms
 * @param   addend  The other term
 * @return          0 on success, -1 on failure
 * @throws  ERANGE  The result is too large or too small to be store; the result
 *                  will be `{.tv_sec = TIME_MAX, .tv_nsec = 999999999L}` if the
 *                  result too large and `{.tv_sec = TIME_MIN, .tv_nsec = 0L}`
 *                  if the result too small
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_sumtimespec(struct timespec *, const struct timespec *, const struct timespec *);
#ifndef sumtimespec
# define sumtimespec libsimple_sumtimespec
#endif


/**
 * Calculates the difference of two `struct timespec`s
 * 
 * @param   diff        Output parameter for the result
 * @param   minuend     The left-hand term
 * @param   subtrahend  The right-hand term
 * @return              0 on success, -1 on failure
 * @throws  ERANGE      The result is too large or too small to be store; the result
 *                      will be `{.tv_sec = TIME_MAX, .tv_nsec = 999999999L}` if the
 *                      result too large and `{.tv_sec = TIME_MIN, .tv_nsec = 0L}`
 *                      if the result too small
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_difftimespec(struct timespec *, const struct timespec *, const struct timespec *);
#ifndef difftimespec
# define difftimespec libsimple_difftimespec
#endif


/**
 * Calculates the product of a `struct timespec` and an integer
 * 
 * @param   prod          Output parameter for the result
 * @param   multiplicand  One of the factors
 * @param   multiplier    The other factor
 * @return                0 on success, -1 on failure
 * @throws  ERANGE        The result is too large or too small to be store; the result
 *                        will be `{.tv_sec = TIME_MAX, .tv_nsec = 999999999L}` if the
 *                        result too large and `{.tv_sec = TIME_MIN, .tv_nsec = 0L}`
 *                        if the result too small
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_multimespec(struct timespec *, const struct timespec *, int);
#ifndef multimespec
# define multimespec libsimple_multimespec
#endif


/**
 * Compares two `struct timespec`s
 * 
 * @param   a  One of the `struct timespec`s
 * @param   b  The other `struct timespec`
 * @return     -1 if `a` < `b`, 0 if `a` = `b`, and +1 if `a` > `b`
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __warn_unused_result__)))
static inline int
libsimple_cmptimespec(const struct timespec *__a, const struct timespec *__b)
{
        if (__a->tv_sec != __b->tv_sec)
                return __a->tv_sec < __b->tv_sec ? -1 : +1;
        return __a->tv_nsec < __b->tv_nsec ? -1 : __a->tv_nsec > __b->tv_nsec;
}
#ifndef cmptimespec
# define cmptimespec libsimple_cmptimespec
#endif


/**
 * Calculates the sum of two `struct timeval`s
 * 
 * @param   sum     Output parameter for the result
 * @param   augend  One of the terms
 * @param   addend  The other term
 * @return          0 on success, -1 on failure
 * @throws  ERANGE  The result is too large or too small to be store; the result
 *                  will be `{.tv_sec = TIME_MAX, .tv_usec = 999999L}` if the
 *                  result too large and `{.tv_sec = TIME_MIN, .tv_usec = 0L}`
 *                  if the result too small
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_sumtimeval(struct timeval *, const struct timeval *, const struct timeval *);
#ifndef sumtimeval
# define sumtimeval libsimple_sumtimeval
#endif


/**
 * Calculates the difference of two `struct timeval`s
 * 
 * @param   diff        Output parameter for the result
 * @param   minuend     The left-hand term
 * @param   subtrahend  The right-hand term
 * @return              0 on success, -1 on failure
 * @throws  ERANGE      The result is too large or too small to be store; the result
 *                      will be `{.tv_sec = TIME_MAX, .tv_usec = 999999L}` if the
 *                      result too large and `{.tv_sec = TIME_MIN, .tv_usec = 0L}`
 *                      if the result too small
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_difftimeval(struct timeval *, const struct timeval *, const struct timeval *);
#ifndef difftimeval
# define difftimeval libsimple_difftimeval
#endif


/**
 * Calculates the product of a `struct timeval` and an integer
 * 
 * @param   prod          Output parameter for the result
 * @param   multiplicand  One of the factors
 * @param   multiplier    The other factor
 * @return                0 on success, -1 on failure
 * @throws  ERANGE        The result is too large or too small to be store; the result
 *                        will be `{.tv_sec = TIME_MAX, .tv_usec = 999999L}` if the
 *                        result too large and `{.tv_sec = TIME_MIN, .tv_usec = 0L}`
 *                        if the result too small
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_multimeval(struct timeval *, const struct timeval *, int);
#ifndef multimeval
# define multimeval libsimple_multimeval
#endif


/**
 * Compares two `struct timeval`s
 * 
 * @param   a  One of the `struct timeval`s
 * @param   b  The other `struct timeval`
 * @return     -1 if `a` < `b`, 0 if `a` = `b`, and +1 if `a` > `b`
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __warn_unused_result__)))
static inline int
libsimple_cmptimeval(const struct timeval *__a, const struct timeval *__b)
{
        if (__a->tv_sec != __b->tv_sec)
                return __a->tv_sec < __b->tv_sec ? -1 : +1;
        return __a->tv_usec < __b->tv_usec ? -1 : __a->tv_usec > __b->tv_usec;
}
#ifndef cmptimeval
# define cmptimeval libsimple_cmptimeval
#endif


/**
 * Converts a `struct timeval` to a `struct timespec`
 * 
 * @param  ts  Output parameter for the result
 * @param  tv  The value to convert
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
static inline void
libsimple_timeval2timespec(struct timespec *restrict __ts, const struct timeval *restrict __tv)
{
        __ts->tv_sec = __tv->tv_sec;
        __ts->tv_nsec = __tv->tv_usec;
        __ts->tv_nsec *= 1000L;
}
#ifndef timeval2timespec
# define timeval2timespec libsimple_timeval2timespec
#endif


/**
 * Converts a `struct timespec` to a `struct timeval`
 * 
 * @param   tv         The value to convert
 * @param   ts         Output parameter for the result
 * @return             0 on success, -1 on error
 * @throws  EOVERFLOW  The value is too large to be convert, the value will be
 *                     truncated to `{.tv_sec = TIME_MAX, .tv_usec = 999999L}`
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_timespec2timeval(struct timeval *restrict, const struct timespec *restrict);
#ifndef timespec2timeval
# define timespec2timeval libsimple_timespec2timeval
#endif


/**
 * Converts a string to `struct timespec`
 * 
 * Repeating decimal are supported ith the repetend inside
 * round brackets or after an additional point, for example
 * "0.1(6)" or "0.1.6" for one sixth
 *
 * @param   ts      Output parameter for the result
 * @param   s       The string to parse
 * @param   end     Output parameter for where the parsing stopped;
 *                  will only be set on success or on ERANGE error
 * @return          0 on success, -1 on error
 * @throws  EINVAL  The input string is invalid (`*end` is not set)
 * @throws  ERANGE  The result is too large or too small to be store; the result
 *                  will be `{.tv_sec = TIME_MAX, .tv_nsec = 999999999L}` if the
 *                  result too large and `{.tv_sec = TIME_MIN, .tv_nsec = 0L}`
 *                  if the result too small (`*end` is set)
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1, 2))))
int libsimple_strtotimespec(struct timespec *restrict, const char *restrict, char **restrict);
#ifndef strtotimespec
# define strtotimespec libsimple_strtotimespec
#endif


/**
 * Converts a string to `struct timeval`
 * 
 * Repeating decimal are supported ith the repetend inside
 * round brackets or after an additional point, for example
 * "0.1(6)" or "0.1.6" for one sixth
 *
 * @param   tv      Output parameter for the result
 * @param   s       The string to parse
 * @param   end     Output parameter for where the parsing stopped;
 *                  will only be set on success or on ERANGE error
 * @return          0 on success, -1 on error
 * @throws  EINVAL  The input string is invalid (`*end` is not set)
 * @throws  ERANGE  The result is too large or too small to be store; the result
 *                  will be `{.tv_sec = TIME_MAX, .tv_usec = 999999L}` if the
 *                  result too large and `{.tv_sec = TIME_MIN, .tv_usec = 0L}`
 *                  if the result too small (`*end` is set)
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1, 2))))
int libsimple_strtotimeval(struct timeval *restrict, const char *restrict, char **restrict);
#ifndef strtotimeval
# define strtotimeval libsimple_strtotimeval
#endif


/**
 * Converts a `struct timespec` to string
 * 
 * The output will always have a sign ("+" or "-") and 9 decimals.
 * 
 * @param   buf     Output buffer to use, if `NULL` a new buffer will be allocated
 * @param   ts      The value to convert
 * @return          The result (same pointer as `buf` unless `buf == NULL`),
 *                  `NULL` on failure
 * @throws  ENOMEM  Output buffer could not be allocated (only if `buf == NULL`)
 * @throws  EINVAL  `ts->tv_nsec` is negative or greater than 999999999
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2))))
char *libsimple_timespectostr(char *restrict, const struct timespec *restrict);
#ifndef timespectostr
# define timespectostr libsimple_timespectostr
#endif


/**
 * Converts a `struct timeval` to string
 * 
 * The output will always have a sign ("+" or "-") and 6 decimals.
 * 
 * @param   buf     Output buffer to use, if `NULL` a new buffer will be allocated
 * @param   tv      The value to convert
 * @return          The result (same pointer as `buf` unless `buf == NULL`),
 *                  `NULL` on failure
 * @throws  ENOMEM  Output buffer could not be allocated (only if `buf == NULL`)
 * @throws  EINVAL  `ts->tv_usec` is negative or greater than 999999
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2))))
char *libsimple_timevaltostr(char *restrict, const struct timeval *restrict);
#ifndef timevaltostr
# define timevaltostr libsimple_timevaltostr
#endif


/**
 * Converts a `struct timespec` to `double`
 * 
 * @param   ts  The value to convert
 * @return      The result
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
static inline double
libsimple_timespectodouble(const struct timespec *__ts)
{
	double __ret = (double)(__ts->tv_nsec);
	__ret /= (double)1000000000L;
	__ret += (double)(__ts->tv_sec);
	return __ret;
}
#ifndef timespectodouble
# define timespectodouble libsimple_timespectodouble
#endif


/**
 * Converts a `struct timeval` to `double`
 * 
 * @param   tv  The value to convert
 * @return      The result
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
static inline double
libsimple_timevaltodouble(const struct timeval *__tv)
{
	double __ret = (double)(__tv->tv_usec);
	__ret /= (double)1000000L;
	__ret += (double)(__tv->tv_sec);
	return __ret;
}
#ifndef timevaltodouble
# define timevaltodouble libsimple_timevaltodouble
#endif


/**
 * Converts a double to `struct timespec`
 * 
 * No overflow check is made
 * 
 * @param  ts  Output parameter for the result
 * @param  d   The value to convert
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
void libsimple_doubletotimespec(struct timespec *, double);
#ifndef doubletotimespec
# define doubletotimespec libsimple_doubletotimespec
#endif


/**
 * Converts a double to `struct timeval`
 * 
 * No overflow check is made
 * 
 * @param  tv  Output parameter for the result
 * @param  d   The value to convert
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
void libsimple_doubletotimeval(struct timeval *, double);
#ifndef doubletotimeval
# define doubletotimeval libsimple_doubletotimeval
#endif


/**
 * Minimises a numerical string
 * 
 * This function does not support repeating decimals
 * 
 * @param   s  The string to minimise
 * @return     `s`
 * 
 * @since  1.0
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__returns_nonnull__, __nonnull__)))
char *libsimple_minimise_number_string(char *);
#ifndef minimise_number_string
# define minimise_number_string libsimple_minimise_number_string
#endif


/**
 * Get the current time in the local timezone
 * 
 * This function is leap-second aware
 * 
 * @param   tm  Output parameter for the local time
 * @param   ts  Output parameter for the POSIX time, may be NULL
 * @return      0 on success, -1 on failure
 * 
 * @since  1.2
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1))))
int libsimple_localtime(struct tm *, struct timespec *);

/**
 * Get the current time in the local timezone
 * 
 * This function is leap-second aware
 * 
 * On failure, the `libsimple_enprintf` function is called,
 * cause the program to print an error message and exit,
 * see `libsimple_enprintf` for more information
 * 
 * @param  status  The exit value for the process in case of failure
 * @param  tm      Output parameter for the local time
 * @param  ts      Output parameter for the POSIX time, may be NULL
 * 
 * @since  1.2
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2))))
void libsimple_enlocaltime(int, struct tm *, struct timespec *);

/**
 * Get the current time in the local timezone
 * 
 * This function is leap-second aware
 * 
 * On failure, the `libsimple_eprintf` function is called,
 * cause the program to print an error message and exit,
 * see `libsimple_eprintf` for more information
 * 
 * @param  tm  Output parameter for the local time
 * @param  ts  Output parameter for the POSIX time, may be NULL
 * 
 * @since  1.2
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1))))
static inline void libsimple_elocaltime(struct tm *__tm, struct timespec *__ts)
{ libsimple_enlocaltime(libsimple_default_failure_exit, __tm, __ts); }


/**
 * Get the current time in the UTC timezone
 * 
 * This function is leap-second aware
 * 
 * @param   tm  Output parameter for the UTC time
 * @param   ts  Output parameter for the POSIX time, may be NULL
 * @return      0 on success, -1 on failure
 * 
 * @since  1.2
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1))))
int libsimple_gmtime(struct tm *, struct timespec *);

/**
 * Get the current time in the UTC timezone
 * 
 * This function is leap-second aware
 * 
 * On failure, the `libsimple_enprintf` function is called,
 * cause the program to print an error message and exit,
 * see `libsimple_enprintf` for more information
 * 
 * @param  status  The exit value for the process in case of failure
 * @param  tm      Output parameter for the UTC time
 * @param  ts      Output parameter for the POSIX time, may be NULL
 * 
 * @since  1.2
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2))))
void libsimple_engmtime(int, struct tm *, struct timespec *);

/**
 * Get the current time in the UTC timezone
 * 
 * This function is leap-second aware
 * 
 * On failure, the `libsimple_eprintf` function is called,
 * cause the program to print an error message and exit,
 * see `libsimple_eprintf` for more information
 * 
 * @param  tm  Output parameter for the UTC time
 * @param  ts  Output parameter for the POSIX time, may be NULL
 * 
 * @since  1.2
 */
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1))))
static inline void libsimple_egmtime(struct tm *__tm, struct timespec *__ts)
{ libsimple_engmtime(libsimple_default_failure_exit, __tm, __ts); }