1 | /* |
---|
2 | * adjustime() function - required by NTP |
---|
3 | * |
---|
4 | * I am unaware of the history behind the definition of this service |
---|
5 | * and don't know if its behavior is covered by any standard. --joel |
---|
6 | * |
---|
7 | * $Id$ |
---|
8 | */ |
---|
9 | |
---|
10 | #if HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <time.h> |
---|
15 | #include <sys/time.h> |
---|
16 | |
---|
17 | #include <rtems/system.h> |
---|
18 | #include <rtems/score/tod.h> |
---|
19 | #include <rtems/posix/time.h> |
---|
20 | |
---|
21 | static long __adjustment = 0; |
---|
22 | |
---|
23 | int adjtime ( struct timeval *delta, struct timeval *olddelta ) |
---|
24 | { |
---|
25 | struct timespec ts; |
---|
26 | |
---|
27 | if ( olddelta ) { |
---|
28 | olddelta->tv_sec = __adjustment / TOD_MICROSECONDS_PER_SECOND; |
---|
29 | olddelta->tv_usec = __adjustment / TOD_MICROSECONDS_PER_SECOND; |
---|
30 | } |
---|
31 | |
---|
32 | if ( !delta ) |
---|
33 | return -1; |
---|
34 | |
---|
35 | __adjustment = (delta->tv_sec * TOD_MICROSECONDS_PER_SECOND) + delta->tv_usec; |
---|
36 | /* too small to account for */ |
---|
37 | if ( __adjustment < _TOD_Microseconds_per_tick ) |
---|
38 | return 0; |
---|
39 | |
---|
40 | clock_gettime( CLOCK_REALTIME, &ts ); |
---|
41 | |
---|
42 | ts.tv_sec += (__adjustment / TOD_MICROSECONDS_PER_SECOND); |
---|
43 | ts.tv_nsec += (__adjustment % TOD_MICROSECONDS_PER_SECOND) * |
---|
44 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
45 | |
---|
46 | /* if adjustment is too much positive */ |
---|
47 | while ( ts.tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) { |
---|
48 | ts.tv_nsec -= TOD_NANOSECONDS_PER_SECOND; |
---|
49 | ts.tv_sec++; |
---|
50 | } |
---|
51 | |
---|
52 | /* if adjustment is too much negative */ |
---|
53 | while ( ts.tv_nsec <= (-1 * TOD_NANOSECONDS_PER_SECOND) ) { |
---|
54 | ts.tv_nsec += TOD_NANOSECONDS_PER_SECOND; |
---|
55 | ts.tv_sec--; |
---|
56 | } |
---|
57 | |
---|
58 | clock_settime( CLOCK_REALTIME, &ts ); |
---|
59 | return 0; |
---|
60 | } |
---|