(root)/
coreutils-9.4/
lib/
xtime.h
       1  /* xtime -- extended-resolution integer timestamps
       2  
       3     Copyright (C) 2005-2006, 2009-2023 Free Software Foundation, Inc.
       4  
       5     This program is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU General Public License as published by
       7     the Free Software Foundation, either version 3 of the License, or
       8     (at your option) any later version.
       9  
      10     This program 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
      13     GNU General Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  /* Written by Paul Eggert.  */
      19  
      20  #ifndef XTIME_H_
      21  #define XTIME_H_ 1
      22  
      23  /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE.  */
      24  #if !_GL_CONFIG_H_INCLUDED
      25   #error "Please include config.h first."
      26  #endif
      27  
      28  _GL_INLINE_HEADER_BEGIN
      29  #ifndef XTIME_INLINE
      30  # define XTIME_INLINE _GL_INLINE
      31  #endif
      32  
      33  /* xtime_t is a signed type used for timestamps.  It is an integer
      34     type that is a count of nanoseconds.  */
      35  typedef long long int xtime_t;
      36  #define XTIME_PRECISION 1000000000
      37  
      38  #ifdef  __cplusplus
      39  extern "C" {
      40  #endif
      41  
      42  /* Return an extended time value that contains S seconds and NS
      43     nanoseconds.  S and NS should be nonnegative; otherwise, integer
      44     overflow can occur even if the result is in range.  */
      45  XTIME_INLINE xtime_t
      46  xtime_make (xtime_t s, long int ns)
      47  {
      48    return XTIME_PRECISION * s + ns;
      49  }
      50  
      51  /* The following functions split an extended time value:
      52     T = XTIME_PRECISION * xtime_sec (T) + xtime_nsec (T)
      53     with  0 <= xtime_nsec (T) < XTIME_PRECISION.  */
      54  
      55  /* Return the number of seconds in T, which must be nonnegative.  */
      56  XTIME_INLINE xtime_t
      57  xtime_nonnegative_sec (xtime_t t)
      58  {
      59    return t / XTIME_PRECISION;
      60  }
      61  
      62  /* Return the number of seconds in T.  */
      63  XTIME_INLINE xtime_t
      64  xtime_sec (xtime_t t)
      65  {
      66    return (t + (t < 0)) / XTIME_PRECISION - (t < 0);
      67  }
      68  
      69  /* Return the number of nanoseconds in T, which must be nonnegative.  */
      70  XTIME_INLINE long int
      71  xtime_nonnegative_nsec (xtime_t t)
      72  {
      73    return t % XTIME_PRECISION;
      74  }
      75  
      76  /* Return the number of nanoseconds in T.  */
      77  XTIME_INLINE long int
      78  xtime_nsec (xtime_t t)
      79  {
      80    long int ns = t % XTIME_PRECISION;
      81    if (ns < 0)
      82      ns += XTIME_PRECISION;
      83    return ns;
      84  }
      85  
      86  #ifdef  __cplusplus
      87  }
      88  #endif
      89  
      90  _GL_INLINE_HEADER_END
      91  
      92  #endif