source: rtems/cpukit/posix/src/adjtime.c @ e7bd66a7

4.104.114.84.95
Last change on this file since e7bd66a7 was 3d57435d, checked in by Joel Sherrill <joel.sherrill@…>, on 08/21/02 at 17:45:10

2002-08-21 Joel Sherrill <joel@…>

  • src/adjtime.c: New file -- adjtime() support required by the Network Time Protocol (NTP) port to RTEMS.
  • src/Makefile.am: Modified to reflect above.
  • Property mode set to 100644
File size: 1.5 KB
Line 
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
21static long __adjustment = 0;
22
23int  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}
Note: See TracBrowser for help on using the repository browser.