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