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