blob: 429880534960e6cdcc84d76f681a414865f74c97 [file] [log] [blame]
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +02001/*
2 * (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
3 * All Rights Reserved
4 *
5 * Authors: Neels Hofmeyr <nhofmeyr@sysmocom.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23/*! \addtogroup timer
24 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 * \file timer_gettimeofday.c */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020026
27#include <stdbool.h>
28#include <sys/time.h>
Pau Espin Pedrol648f8782017-06-18 11:41:32 +020029#include <osmocom/core/timer_compat.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020030
31bool osmo_gettimeofday_override = false;
32struct timeval osmo_gettimeofday_override_time = { 23, 424242 };
33
Neels Hofmeyr87e45502017-06-20 00:17:59 +020034/*! shim around gettimeofday to be able to set the time manually.
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020035 * To override, set osmo_gettimeofday_override == true and set the desired
Max1fa8dfb2017-10-20 12:48:04 +020036 * current time in osmo_gettimeofday_override_time.
37 *
38 * N. B: gettimeofday() is affected by discontinuous jumps in the system time
39 * (e.g., if the system administrator manually changes the system time).
40 * Hence this should NEVER be used for elapsed time computation.
41 * Instead, clock_gettime(CLOCK_MONOTONIC, ..) should be used for that (with similar shim if necessary).
42 */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020043int osmo_gettimeofday(struct timeval *tv, struct timezone *tz)
44{
45 if (osmo_gettimeofday_override) {
46 *tv = osmo_gettimeofday_override_time;
47 return 0;
48 }
49
50 return gettimeofday(tv, tz);
51}
52
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053/*! convenience function to advance the fake time.
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020054 * Add the given values to osmo_gettimeofday_override_time. */
55void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs)
56{
57 struct timeval val = { secs, usecs };
58 timeradd(&osmo_gettimeofday_override_time, &val,
59 &osmo_gettimeofday_override_time);
60}
61
62/*! @} */