Skip to content

Commit

Permalink
[time] Allow system clock to be adjusted at runtime
Browse files Browse the repository at this point in the history
Provide a mechanism to allow an arbitrary adjustment to be applied to
all subsequent calls to time().

Note that the underlying clock source (e.g. the RTC clock) will not be
changed; only the time as reported within iPXE will be affected.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Jun 13, 2016
1 parent 02d5cff commit e6111c1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/core/time.c
Expand Up @@ -43,6 +43,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
* 400.
*/

/** Current system clock offset */
signed long time_offset;

/** Days of week (for debugging) */
static const char *weekdays[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
Expand Down
15 changes: 14 additions & 1 deletion src/include/ipxe/time.h
Expand Up @@ -50,11 +50,24 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/* Include all architecture-dependent time API headers */
#include <bits/time.h>

extern signed long time_offset;

/**
* Get current time in seconds
* Get current time in seconds (ignoring system clock offset)
*
* @ret time Time, in seconds
*/
time_t time_now ( void );

/**
* Adjust system clock
*
* @v delta Clock adjustment, in seconds
*/
static inline __attribute__ (( always_inline )) void
time_adjust ( signed long delta ) {

time_offset += delta;
}

#endif /* _IPXE_TIME_H */
4 changes: 2 additions & 2 deletions src/include/time.h
Expand Up @@ -39,10 +39,10 @@ struct tm {
* @v t Time to fill in, or NULL
* @ret time Current time
*/
static inline time_t time ( time_t *t ) {
static inline __attribute__ (( always_inline )) time_t time ( time_t *t ) {
time_t now;

now = time_now();
now = ( time_now() + time_offset );
if ( t )
*t = now;
return now;
Expand Down

0 comments on commit e6111c1

Please sign in to comment.