dss.c: Add some more documentation.
[dss.git] / time.c
1 #include <sys/time.h>
2 #include <time.h>
3 #include <inttypes.h>
4
5 /*
6 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
7 *
8 * Licensed under the GPL v2. For licencing details see COPYING.
9 */
10
11 /** \file time.c Helper functions for dealing with time values. */
12
13 /**
14 * Convert struct timeval to milliseconds.
15 *
16 * \param tv The time value value to convert.
17 *
18 * \return The number off milliseconds in \a tv.
19 */
20 long unsigned tv2ms(const struct timeval *tv)
21 {
22 return tv->tv_sec * 1000 + (tv->tv_usec + 500)/ 1000;
23 }
24
25 /**
26 * Convert milliseconds to a struct timeval.
27 *
28 * \param n The number of milliseconds.
29 * \param tv Result pointer.
30 */
31 void ms2tv(long unsigned n, struct timeval *tv)
32 {
33 tv->tv_sec = n / 1000;
34 tv->tv_usec = (n % 1000) * 1000;
35 }
36
37 /**
38 * Convert a double to a struct timeval.
39 *
40 * \param x The value to convert.
41 * \param tv Result pointer.
42 */
43 void d2tv(double x, struct timeval *tv)
44 {
45 tv->tv_sec = x;
46 tv->tv_usec = (x - (double)tv->tv_sec) * 1000.0 * 1000.0 + 0.5;
47 }
48
49 /**
50 * Compute the difference of two time values.
51 *
52 * \param b Minuend.
53 * \param a Subtrahend.
54 * \param diff Result pointer.
55 *
56 * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
57 * return.
58 *
59 * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
60 */
61 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff)
62 {
63 int ret = 1;
64
65 if ((b->tv_sec < a->tv_sec) ||
66 ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
67 const struct timeval *tmp = a;
68 a = b;
69 b = tmp;
70 ret = -1;
71 }
72 if (!diff)
73 return ret;
74 diff->tv_sec = b->tv_sec - a->tv_sec;
75 if (b->tv_usec < a->tv_usec) {
76 diff->tv_sec--;
77 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
78 } else
79 diff->tv_usec = b->tv_usec - a->tv_usec;
80 return ret;
81 }
82
83 /**
84 * Add two time values.
85 *
86 * \param a First addend.
87 * \param b Second addend.
88 * \param sum Contains the sum \a + \a b on return.
89 */
90 void tv_add(const struct timeval *a, const struct timeval *b,
91 struct timeval *sum)
92 {
93 sum->tv_sec = a->tv_sec + b->tv_sec;
94 if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
95 sum->tv_sec++;
96 sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000;
97 } else
98 sum->tv_usec = a->tv_usec + b->tv_usec;
99 }
100
101 /**
102 * Compute integer multiple of given struct timeval.
103 *
104 * \param mult The integer value to multiply with.
105 * \param tv The timevalue to multiply.
106 *
107 * \param result Contains \a mult * \a tv on return.
108 */
109 void tv_scale(const unsigned long mult, const struct timeval *tv,
110 struct timeval *result)
111 {
112 result->tv_sec = mult * tv->tv_sec;
113 result->tv_sec += tv->tv_usec * mult / 1000 / 1000;
114 result->tv_usec = tv->tv_usec * mult % (1000 * 1000);
115 }
116
117 /**
118 * Compute a fraction of given struct timeval.
119 *
120 * \param divisor The integer value to divide by.
121 * \param tv The timevalue to divide.
122 * \param result Contains (1 / mult) * tv on return.
123 */
124 void tv_divide(const unsigned long divisor, const struct timeval *tv,
125 struct timeval *result)
126 {
127 uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
128
129 result->tv_sec = x / 1000 / 1000;
130 result->tv_usec = x % (1000 * 1000);
131 }