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