Browse Source

[time] Allow system clock to be adjusted at runtime

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>
tags/v1.20.1
Michael Brown 8 years ago
parent
commit
e6111c1517
3 changed files with 19 additions and 3 deletions
  1. 3
    0
      src/core/time.c
  2. 14
    1
      src/include/ipxe/time.h
  3. 2
    2
      src/include/time.h

+ 3
- 0
src/core/time.c View File

@@ -43,6 +43,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
43 43
  * 400.
44 44
  */
45 45
 
46
+/** Current system clock offset */
47
+signed long time_offset;
48
+
46 49
 /** Days of week (for debugging) */
47 50
 static const char *weekdays[] = {
48 51
 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"

+ 14
- 1
src/include/ipxe/time.h View File

@@ -50,11 +50,24 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
50 50
 /* Include all architecture-dependent time API headers */
51 51
 #include <bits/time.h>
52 52
 
53
+extern signed long time_offset;
54
+
53 55
 /**
54
- * Get current time in seconds
56
+ * Get current time in seconds (ignoring system clock offset)
55 57
  *
56 58
  * @ret time		Time, in seconds
57 59
  */
58 60
 time_t time_now ( void );
59 61
 
62
+/**
63
+ * Adjust system clock
64
+ *
65
+ * @v delta		Clock adjustment, in seconds
66
+ */
67
+static inline __attribute__ (( always_inline )) void
68
+time_adjust ( signed long delta ) {
69
+
70
+	time_offset += delta;
71
+}
72
+
60 73
 #endif /* _IPXE_TIME_H */

+ 2
- 2
src/include/time.h View File

@@ -39,10 +39,10 @@ struct tm {
39 39
  * @v t			Time to fill in, or NULL
40 40
  * @ret time		Current time
41 41
  */
42
-static inline time_t time ( time_t *t ) {
42
+static inline __attribute__ (( always_inline )) time_t time ( time_t *t ) {
43 43
 	time_t now;
44 44
 
45
-	now = time_now();
45
+	now = ( time_now() + time_offset );
46 46
 	if ( t )
47 47
 		*t = now;
48 48
 	return now;

Loading…
Cancel
Save