1/* Internal declarations for sys/timex.h.
  2   Copyright (C) 2014-2025 Free Software Foundation, Inc.
  3   This file is part of the GNU C Library.
  4
  5   The GNU C Library is free software; you can redistribute it and/or
  6   modify it under the terms of the GNU Lesser General Public
  7   License as published by the Free Software Foundation; either
  8   version 2.1 of the License, or (at your option) any later version.
  9
 10   The GNU C Library is distributed in the hope that it will be useful,
 11   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13   Lesser General Public License for more details.
 14
 15   You should have received a copy of the GNU Lesser General Public
 16   License along with the GNU C Library; if not, see
 17   <https://www.gnu.org/licenses/>.  */
 18
 19#ifndef	_INCLUDE_SYS_TIMEX_H
 20#define	_INCLUDE_SYS_TIMEX_H	1
 21
 22#include_next <sys/timex.h>
 23
 24# ifndef _ISOMAC
 25
 26extern int __adjtimex (struct timex *__ntx) __nonnull ((1));
 27libc_hidden_proto (__adjtimex)
 28
 29#  include <time.h>
 30#  include <struct___timeval64.h>
 31/* Local definition of 64 bit time supporting timex struct */
 32#  if __TIMESIZE == 64
 33#   define __timex64 timex
 34#   define __clock_adjtime64 __clock_adjtime
 35#   define ___adjtimex64 ___adjtimex
 36#   define __ntptimeval64 ntptimeval
 37#   define __ntp_gettime64 __ntp_gettime
 38#   define __ntp_gettimex64 __ntp_gettimex
 39#  else
 40
 41struct __timex64
 42{
 43  unsigned int modes;          /* mode selector */
 44  int :32;                     /* pad */
 45  long long int offset;            /* time offset (usec) */
 46  long long int freq;              /* frequency offset (scaled ppm) */
 47  long long int maxerror;          /* maximum error (usec) */
 48  long long int esterror;          /* estimated error (usec) */
 49  int status;                  /* clock command/status */
 50  int :32;                     /* pad */
 51  long long int constant;          /* pll time constant */
 52  long long int precision;         /* clock precision (usec) (read only) */
 53  long long int tolerance;         /* clock frequency tolerance (ppm) (ro) */
 54  struct __timeval64 time;     /* (read only, except for ADJ_SETOFFSET) */
 55  long long int tick;              /* (modified) usecs between clock ticks */
 56  long long int ppsfreq;           /* pps frequency (scaled ppm) (ro) */
 57  long long int jitter;            /* pps jitter (us) (ro) */
 58  int shift;                   /* interval duration (s) (shift) (ro) */
 59  int :32;                     /* pad */
 60  long long int stabil;            /* pps stability (scaled ppm) (ro) */
 61  long long int jitcnt;            /* jitter limit exceeded (ro) */
 62  long long int calcnt;            /* calibration intervals (ro) */
 63  long long int errcnt;            /* calibration errors (ro) */
 64  long long int stbcnt;            /* stability limit exceeded (ro) */
 65
 66  int tai;                     /* TAI offset (ro) */
 67
 68  int  :32;
 69  int  :32;
 70  int  :32;
 71  int  :32;
 72  int  :32;
 73  int  :32;
 74  int  :32;
 75  int  :32;
 76  int  :32;
 77  int  :32;
 78  int  :32;
 79};
 80extern int __clock_adjtime64 (const clockid_t clock_id, struct __timex64 *tx64) __nonnull((2));
 81libc_hidden_proto (__clock_adjtime64);
 82extern int ___adjtimex64 (struct __timex64 *tx64) __nonnull ((1));
 83libc_hidden_proto (___adjtimex64)
 84
 85struct __ntptimeval64
 86{
 87  struct __timeval64 time;	/* current time (ro) */
 88  long int maxerror;	/* maximum error (us) (ro) */
 89  long int esterror;	/* estimated error (us) (ro) */
 90  long int tai;		/* TAI offset (ro) */
 91
 92  long int __glibc_reserved1;
 93  long int __glibc_reserved2;
 94  long int __glibc_reserved3;
 95  long int __glibc_reserved4;
 96};
 97extern int __ntp_gettime64 (struct __ntptimeval64 *ntv) __nonnull ((1));
 98libc_hidden_proto (__ntp_gettime64)
 99extern int __ntp_gettimex64 (struct __ntptimeval64 *ntv) __nonnull ((1));
100libc_hidden_proto (__ntp_gettimex64)
101
102#  endif
103
104/* Convert a known valid struct timex into a struct __timex64.  */
105static inline struct __timex64
106valid_timex_to_timex64 (const struct timex tx)
107{
108  struct __timex64 tx64;
109
110  tx64.modes = tx.modes;
111  tx64.offset = tx.offset;
112  tx64.freq = tx.freq;
113  tx64.maxerror = tx.maxerror;
114  tx64.esterror = tx.esterror;
115  tx64.status = tx.status;
116  tx64.constant = tx.constant;
117  tx64.precision = tx.precision;
118  tx64.tolerance = tx.tolerance;
119  tx64.time = valid_timeval_to_timeval64 (tx.time);
120  tx64.tick = tx.tick;
121  tx64.ppsfreq = tx.ppsfreq;
122  tx64.jitter = tx.jitter;
123  tx64.shift = tx.shift;
124  tx64.stabil = tx.stabil;
125  tx64.jitcnt = tx.jitcnt;
126  tx64.calcnt = tx.calcnt;
127  tx64.errcnt = tx.errcnt;
128  tx64.stbcnt = tx.stbcnt;
129  tx64.tai = tx.tai;
130
131  return tx64;
132}
133
134/* Convert a known valid struct __timex64 into a struct timex.  */
135static inline struct timex
136valid_timex64_to_timex (const struct __timex64 tx64)
137{
138  struct timex tx;
139
140  tx.modes = tx64.modes;
141  tx.offset = tx64.offset;
142  tx.freq = tx64.freq;
143  tx.maxerror = tx64.maxerror;
144  tx.esterror = tx64.esterror;
145  tx.status = tx64.status;
146  tx.constant = tx64.constant;
147  tx.precision = tx64.precision;
148  tx.tolerance = tx64.tolerance;
149  tx.time = valid_timeval64_to_timeval (tx64.time);
150  tx.tick = tx64.tick;
151  tx.ppsfreq = tx64.ppsfreq;
152  tx.jitter = tx64.jitter;
153  tx.shift = tx64.shift;
154  tx.stabil = tx64.stabil;
155  tx.jitcnt = tx64.jitcnt;
156  tx.calcnt = tx64.calcnt;
157  tx.errcnt = tx64.errcnt;
158  tx.stbcnt = tx64.stbcnt;
159  tx.tai = tx64.tai;
160
161  return tx;
162}
163
164/* Convert a known valid struct ntptimeval into a struct __ntptimeval64.  */
165static inline struct __ntptimeval64
166valid_ntptimeval_to_ntptimeval64 (const struct ntptimeval ntv)
167{
168  struct __ntptimeval64 ntv64;
169
170  ntv64.time = valid_timeval_to_timeval64 (ntv.time);
171  ntv64.maxerror = ntv.maxerror;
172  ntv64.esterror = ntv.esterror;
173  ntv64.tai = ntv.tai;
174  ntv64.__glibc_reserved1 = 0;
175  ntv64.__glibc_reserved2 = 0;
176  ntv64.__glibc_reserved3 = 0;
177  ntv64.__glibc_reserved4 = 0;
178
179  return ntv64;
180}
181
182/* Convert a known valid struct __ntptimeval64 into a struct ntptimeval.  */
183static inline struct ntptimeval
184valid_ntptimeval64_to_ntptimeval (const struct __ntptimeval64 ntp64)
185{
186  struct ntptimeval ntp;
187
188  ntp.time = valid_timeval64_to_timeval (ntp64.time);
189  ntp.maxerror = ntp64.maxerror;
190  ntp.esterror = ntp64.esterror;
191  ntp.tai = ntp64.tai;
192  ntp.__glibc_reserved1 = 0;
193  ntp.__glibc_reserved2 = 0;
194  ntp.__glibc_reserved3 = 0;
195  ntp.__glibc_reserved4 = 0;
196
197  return ntp;
198}
199# endif /* _ISOMAC */
200#endif /* sys/timex.h */