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