aboutsummaryrefslogtreecommitdiff
path: root/src/util/time.c
blob: 3ae4725615cd03a8c761a6cc38dbb35950ce66c6 (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
/*
     This file is part of GNUnet.
     (C) 2001, 2002, 2006, 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file util/time.c
 * @author Christian Grothoff
 * @brief functions for handling time and time arithmetic
 */
#include "platform.h"
#include "gnunet_time_lib.h"


/**
 * Get the current time (works just as "time", just that we use the
 * unit of time that the cron-jobs use (and is 64 bit)).
 *
 * @return the current time
 */
struct GNUNET_TIME_Absolute
GNUNET_TIME_absolute_get ()
{
  struct GNUNET_TIME_Absolute ret;
  struct timeval tv;

  gettimeofday (&tv, NULL);
  ret.value = tv.tv_sec * 1000 + tv.tv_usec / 1000;
  return ret;
}


/**
 * Return relative time of 0ms.
 */
struct GNUNET_TIME_Relative
GNUNET_TIME_relative_get_zero ()
{
  static struct GNUNET_TIME_Relative zero;
  return zero;
}

/**
 * Return relative time of 1ms.
 */
struct GNUNET_TIME_Relative
GNUNET_TIME_relative_get_unit ()
{
  static struct GNUNET_TIME_Relative one = { 1 };
  return one;
}

/**
 * Return "forever".
 */
struct GNUNET_TIME_Relative
GNUNET_TIME_relative_get_forever ()
{
  static struct GNUNET_TIME_Relative forever = { (uint64_t) - 1LL };
  return forever;
}

/**
 * Return "forever".
 */
struct GNUNET_TIME_Absolute
GNUNET_TIME_absolute_get_forever ()
{
  static struct GNUNET_TIME_Absolute forever = { (uint64_t) - 1LL };
  return forever;
}

/**
 * Convert relative time to an absolute time in the
 * future.
 *
 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
 */
struct GNUNET_TIME_Absolute
GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
{
  struct GNUNET_TIME_Absolute ret;
  if (rel.value == (uint64_t) - 1LL)
    return GNUNET_TIME_absolute_get_forever ();
  struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
  if (rel.value + now.value < rel.value)
    {
      GNUNET_break (0);         /* overflow... */
      return GNUNET_TIME_absolute_get_forever ();
    }
  ret.value = rel.value + now.value;
  return ret;
}

/**
 * Given a timestamp in the future, how much time
 * remains until then?
 *
 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
 */
struct GNUNET_TIME_Relative
GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
{
  struct GNUNET_TIME_Relative ret;
  if (future.value == (uint64_t) - 1LL)
    return GNUNET_TIME_relative_get_forever ();
  struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
  if (now.value > future.value)
    return GNUNET_TIME_relative_get_zero ();
  ret.value = future.value - now.value;
  return ret;
}

/**
 * Compute the time difference between the given start and end times.
 * Use this function instead of actual subtraction to ensure that
 * "FOREVER" and overflows are handeled correctly.
 *
 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
 */
struct GNUNET_TIME_Relative
GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
                                     struct GNUNET_TIME_Absolute end)
{
  struct GNUNET_TIME_Relative ret;
  if (end.value == (uint64_t) - 1LL)
    return GNUNET_TIME_relative_get_forever ();
  if (end.value < start.value)
    return GNUNET_TIME_relative_get_zero ();
  ret.value = end.value - start.value;
  return ret;
}

/**
 * Get the duration of an operation as the
 * difference of the current time and the given start time "hence".
 *
 * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
 */
struct GNUNET_TIME_Relative
GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute hence)
{
  struct GNUNET_TIME_Absolute now;
  struct GNUNET_TIME_Relative ret;

  now = GNUNET_TIME_absolute_get ();
  GNUNET_assert (hence.value != (uint64_t) - 1LL);
  if (hence.value > now.value)
    return GNUNET_TIME_relative_get_zero ();
  ret.value = now.value - hence.value;
  return ret;
}


/**
 * Add a given relative duration to the
 * given start time.
 *
 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
 */
struct GNUNET_TIME_Absolute
GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
                          struct GNUNET_TIME_Relative duration)
{
  struct GNUNET_TIME_Absolute ret;

  if ((start.value == (uint64_t) - 1LL) ||
      (duration.value == (uint64_t) - 1LL))
    return GNUNET_TIME_absolute_get_forever ();
  if (start.value + duration.value < start.value)
    {
      GNUNET_break (0);
      return GNUNET_TIME_absolute_get_forever ();
    }
  ret.value = start.value + duration.value;
  return ret;
}

/**
 * Multiply relative time by a given factor.
 *
 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
 */
struct GNUNET_TIME_Relative
GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
                               unsigned int factor)
{
  struct GNUNET_TIME_Relative ret;
  if (factor == 0)
    return GNUNET_TIME_relative_get_zero ();
  ret.value = rel.value * factor;
  if (ret.value / factor != rel.value)
    {
      GNUNET_break (0);
      return GNUNET_TIME_relative_get_forever ();
    }
  return ret;
}

/**
 * Add relative times together.
 *
 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
 */
struct GNUNET_TIME_Relative
GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
                          struct GNUNET_TIME_Relative a2)
{
  struct GNUNET_TIME_Relative ret;

  if ((a1.value == (uint64_t) - 1LL) || (a2.value == (uint64_t) - 1LL))
    return GNUNET_TIME_relative_get_forever ();
  if (a1.value + a2.value < a1.value)
    {
      GNUNET_break (0);
      return GNUNET_TIME_relative_get_forever ();
    }
  ret.value = a1.value + a2.value;
  return ret;
}


/**
 * Convert relative time to network byte order.
 */
struct GNUNET_TIME_RelativeNBO
GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
{
  struct GNUNET_TIME_RelativeNBO ret;
  ret.value = GNUNET_htonll (a.value);
  return ret;
}

/**
 * Convert relative time from network byte order.
 */
struct GNUNET_TIME_Relative
GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
{
  struct GNUNET_TIME_Relative ret;
  ret.value = GNUNET_ntohll (a.value);
  return ret;

}

/**
 * Convert absolute time to network byte order.
 */
struct GNUNET_TIME_AbsoluteNBO
GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
{
  struct GNUNET_TIME_AbsoluteNBO ret;
  ret.value = GNUNET_htonll (a.value);
  return ret;
}

/**
 * Convert absolute time from network byte order.
 */
struct GNUNET_TIME_Absolute
GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
{
  struct GNUNET_TIME_Absolute ret;
  ret.value = GNUNET_ntohll (a.value);
  return ret;

}



/* end of time.c */