blob: 34949465a09218bcd128afd65dac96a3eb3e6342 [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 * @{
Pau Espin Pedrol8d827802018-02-28 18:28:53 +010026 * \file timer_gettimeofday.c
27 * Overriding Time: osmo_gettimeofday()
28 * - Useful to write and reproduce tests that depend on specific time
29 * factors. This API allows to fake the timeval provided by `gettimeofday()`
30 * by using a small shim osmo_gettimeofday().
31 * - If the clock override is disabled (default) for a given clock,
32 * osmo_gettimeofday() will do the same as regular `gettimeofday()`.
33 * - If you want osmo_gettimeofday() to provide a specific time, you must
34 * enable time override by setting the global variable
35 * osmo_gettimeofday_override (`osmo_gettimeofday_override = true`), then
36 * set the global struct timeval osmo_gettimeofday_override_time wih the
37 * desired value. Next time osmo_gettimeofday() is called, it will return
38 * the values previously set.
39 * - A helper osmo_gettimeofday_override_add() is provided to easily
40 * increment osmo_gettimeofday_override_time with a specific amount of
41 * time.
42 */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020043
44#include <stdbool.h>
45#include <sys/time.h>
Pau Espin Pedrol648f8782017-06-18 11:41:32 +020046#include <osmocom/core/timer_compat.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020047
48bool osmo_gettimeofday_override = false;
49struct timeval osmo_gettimeofday_override_time = { 23, 424242 };
50
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051/*! shim around gettimeofday to be able to set the time manually.
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020052 * To override, set osmo_gettimeofday_override == true and set the desired
Max1fa8dfb2017-10-20 12:48:04 +020053 * current time in osmo_gettimeofday_override_time.
54 *
55 * N. B: gettimeofday() is affected by discontinuous jumps in the system time
56 * (e.g., if the system administrator manually changes the system time).
57 * Hence this should NEVER be used for elapsed time computation.
Pau Espin Pedrol87fade82018-02-26 19:42:22 +010058 * Instead, osmo_clock_gettime() with CLOCK_MONOTONIC should be used for that.
Max1fa8dfb2017-10-20 12:48:04 +020059 */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020060int osmo_gettimeofday(struct timeval *tv, struct timezone *tz)
61{
62 if (osmo_gettimeofday_override) {
63 *tv = osmo_gettimeofday_override_time;
64 return 0;
65 }
66
67 return gettimeofday(tv, tz);
68}
69
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070/*! convenience function to advance the fake time.
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020071 * Add the given values to osmo_gettimeofday_override_time. */
72void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs)
73{
74 struct timeval val = { secs, usecs };
75 timeradd(&osmo_gettimeofday_override_time, &val,
76 &osmo_gettimeofday_override_time);
77}
78
79/*! @} */