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