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