blob: 357305155da32fe6b523260150ef10396a705b6a [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 * @{
25 */
26
27/*! \file timer_gettimeofday.c
28 */
29
30#include <stdbool.h>
31#include <sys/time.h>
Pau Espin Pedrol648f8782017-06-18 11:41:32 +020032#include <osmocom/core/timer_compat.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020033
34bool osmo_gettimeofday_override = false;
35struct timeval osmo_gettimeofday_override_time = { 23, 424242 };
36
37/*! \brief shim around gettimeofday to be able to set the time manually.
38 * To override, set osmo_gettimeofday_override == true and set the desired
39 * current time in osmo_gettimeofday_override_time. */
40int osmo_gettimeofday(struct timeval *tv, struct timezone *tz)
41{
42 if (osmo_gettimeofday_override) {
43 *tv = osmo_gettimeofday_override_time;
44 return 0;
45 }
46
47 return gettimeofday(tv, tz);
48}
49
50/*! \brief convenience function to advance the fake time.
51 * Add the given values to osmo_gettimeofday_override_time. */
52void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs)
53{
54 struct timeval val = { secs, usecs };
55 timeradd(&osmo_gettimeofday_override_time, &val,
56 &osmo_gettimeofday_override_time);
57}
58
59/*! @} */