loaders: JPG: Fix bussy loop on corrupted file.
[gfxprim.git] / tests / framework / tst_timespec.c
blob45a44221c74c413a56a9b5aab1f6be0c7087ee0e
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include "tst_timespec.h"
25 #define NSEC_IN_SEC 1000000000
27 void timespec_diff(int *sec, int *nsec,
28 const struct timespec *start,
29 const struct timespec *stop)
31 if (stop->tv_nsec < start->tv_nsec) {
32 *sec = stop->tv_sec - start->tv_sec - 1;
33 *nsec = stop->tv_nsec + 1000000000 - start->tv_nsec;
34 } else {
35 *sec = stop->tv_sec - start->tv_sec;
36 *nsec = stop->tv_nsec - start->tv_nsec;
40 double timespec_to_double(const struct timespec *t)
42 double res;
44 res = t->tv_sec;
45 res *= NSEC_IN_SEC;
46 res += t->tv_nsec;
48 return res;
51 void double_to_timespec(const double time, struct timespec *res)
53 res->tv_sec = time / NSEC_IN_SEC;
54 res->tv_nsec = time - res->tv_sec * NSEC_IN_SEC;
57 void timespec_sub(const struct timespec *a, const struct timespec *b,
58 struct timespec *res)
60 res->tv_sec = a->tv_sec - b->tv_sec;
61 time_t nsec = a->tv_nsec;
63 if (b->tv_nsec > a->tv_nsec) {
64 res->tv_sec--;
65 nsec += NSEC_IN_SEC;
68 res->tv_nsec = nsec - b->tv_nsec;
71 void timespec_add(const struct timespec *a, struct timespec *res)
73 res->tv_sec += a->tv_sec;
74 res->tv_nsec += a->tv_nsec;
76 if (res->tv_nsec >= NSEC_IN_SEC) {
77 res->tv_sec += res->tv_nsec / NSEC_IN_SEC;
78 res->tv_nsec %= NSEC_IN_SEC;
82 void timespec_div(struct timespec *res, unsigned int div)
84 long sec = res->tv_sec;
86 res->tv_sec /= div;
87 sec %= div;
89 res->tv_nsec = (sec * NSEC_IN_SEC + res->tv_nsec)/div;