Blame lib/timespec.h

Packit Service a2489d
/* timespec -- System time interface
Packit Service a2489d
Packit Service a2489d
   Copyright (C) 2000, 2002, 2004-2005, 2007, 2009-2018 Free Software
Packit Service a2489d
   Foundation, Inc.
Packit Service a2489d
Packit Service a2489d
   This program is free software: you can redistribute it and/or modify
Packit Service a2489d
   it under the terms of the GNU General Public License as published by
Packit Service a2489d
   the Free Software Foundation; either version 3 of the License, or
Packit Service a2489d
   (at your option) any later version.
Packit Service a2489d
Packit Service a2489d
   This program is distributed in the hope that it will be useful,
Packit Service a2489d
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2489d
   GNU General Public License for more details.
Packit Service a2489d
Packit Service a2489d
   You should have received a copy of the GNU General Public License
Packit Service a2489d
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service a2489d
Packit Service a2489d
#if ! defined TIMESPEC_H
Packit Service a2489d
# define TIMESPEC_H
Packit Service a2489d
Packit Service a2489d
# include <time.h>
Packit Service a2489d
Packit Service a2489d
#ifndef _GL_INLINE_HEADER_BEGIN
Packit Service a2489d
 #error "Please include config.h first."
Packit Service a2489d
#endif
Packit Service a2489d
_GL_INLINE_HEADER_BEGIN
Packit Service a2489d
#ifndef _GL_TIMESPEC_INLINE
Packit Service a2489d
# define _GL_TIMESPEC_INLINE _GL_INLINE
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
#ifdef __cplusplus
Packit Service a2489d
extern "C" {
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
#include "verify.h"
Packit Service a2489d
Packit Service a2489d
/* Resolution of timespec timestamps (in units per second), and log
Packit Service a2489d
   base 10 of the resolution.  */
Packit Service a2489d
Packit Service a2489d
enum { TIMESPEC_RESOLUTION = 1000000000 };
Packit Service a2489d
enum { LOG10_TIMESPEC_RESOLUTION = 9 };
Packit Service a2489d
Packit Service a2489d
/* Return a timespec with seconds S and nanoseconds NS.  */
Packit Service a2489d
Packit Service a2489d
_GL_TIMESPEC_INLINE struct timespec
Packit Service a2489d
make_timespec (time_t s, long int ns)
Packit Service a2489d
{
Packit Service a2489d
  struct timespec r;
Packit Service a2489d
  r.tv_sec = s;
Packit Service a2489d
  r.tv_nsec = ns;
Packit Service a2489d
  return r;
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
/* Return negative, zero, positive if A < B, A == B, A > B, respectively.
Packit Service a2489d
Packit Service a2489d
   For each timestamp T, this code assumes that either:
Packit Service a2489d
Packit Service a2489d
     * T.tv_nsec is in the range 0..999999999; or
Packit Service a2489d
     * T.tv_sec corresponds to a valid leap second on a host that supports
Packit Service a2489d
       leap seconds, and T.tv_nsec is in the range 1000000000..1999999999; or
Packit Service a2489d
     * T.tv_sec is the minimum time_t value and T.tv_nsec is -1; or
Packit Service a2489d
       T.tv_sec is the maximum time_t value and T.tv_nsec is 2000000000.
Packit Service a2489d
       This allows for special struct timespec values that are less or
Packit Service a2489d
       greater than all possible valid timestamps.
Packit Service a2489d
Packit Service a2489d
   In all these cases, it is safe to subtract two tv_nsec values and
Packit Service a2489d
   convert the result to integer without worrying about overflow on
Packit Service a2489d
   any platform of interest to the GNU project, since all such
Packit Service a2489d
   platforms have 32-bit int or wider.
Packit Service a2489d
Packit Service a2489d
   Replacing "a.tv_nsec - b.tv_nsec" with something like
Packit Service a2489d
   "a.tv_nsec < b.tv_nsec ? -1 : a.tv_nsec > b.tv_nsec" would cause
Packit Service a2489d
   this function to work in some cases where the above assumption is
Packit Service a2489d
   violated, but not in all cases (e.g., a.tv_sec==1, a.tv_nsec==-2,
Packit Service a2489d
   b.tv_sec==0, b.tv_nsec==999999999) and is arguably not worth the
Packit Service a2489d
   extra instructions.  Using a subtraction has the advantage of
Packit Service a2489d
   detecting some invalid cases on platforms that detect integer
Packit Service a2489d
   overflow.  */
Packit Service a2489d
Packit Service a2489d
_GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
Packit Service a2489d
timespec_cmp (struct timespec a, struct timespec b)
Packit Service a2489d
{
Packit Service a2489d
  if (a.tv_sec < b.tv_sec)
Packit Service a2489d
    return -1;
Packit Service a2489d
  if (a.tv_sec > b.tv_sec)
Packit Service a2489d
    return 1;
Packit Service a2489d
Packit Service a2489d
  /* Pacify gcc -Wstrict-overflow (bleeding-edge circa 2017-10-02).  See:
Packit Service a2489d
     https://lists.gnu.org/r/bug-gnulib/2017-10/msg00006.html  */
Packit Service a2489d
  assume (-1 <= a.tv_nsec && a.tv_nsec <= 2 * TIMESPEC_RESOLUTION);
Packit Service a2489d
  assume (-1 <= b.tv_nsec && b.tv_nsec <= 2 * TIMESPEC_RESOLUTION);
Packit Service a2489d
Packit Service a2489d
  return a.tv_nsec - b.tv_nsec;
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
/* Return -1, 0, 1, depending on the sign of A.  A.tv_nsec must be
Packit Service a2489d
   nonnegative.  */
Packit Service a2489d
_GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
Packit Service a2489d
timespec_sign (struct timespec a)
Packit Service a2489d
{
Packit Service a2489d
  return a.tv_sec < 0 ? -1 : a.tv_sec || a.tv_nsec;
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
struct timespec timespec_add (struct timespec, struct timespec)
Packit Service a2489d
  _GL_ATTRIBUTE_CONST;
Packit Service a2489d
struct timespec timespec_sub (struct timespec, struct timespec)
Packit Service a2489d
  _GL_ATTRIBUTE_CONST;
Packit Service a2489d
struct timespec dtotimespec (double)
Packit Service a2489d
  _GL_ATTRIBUTE_CONST;
Packit Service a2489d
Packit Service a2489d
/* Return an approximation to A, of type 'double'.  */
Packit Service a2489d
_GL_TIMESPEC_INLINE double
Packit Service a2489d
timespectod (struct timespec a)
Packit Service a2489d
{
Packit Service a2489d
  return a.tv_sec + a.tv_nsec / 1e9;
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
void gettime (struct timespec *);
Packit Service a2489d
int settime (struct timespec const *);
Packit Service a2489d
Packit Service a2489d
#ifdef __cplusplus
Packit Service a2489d
}
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
_GL_INLINE_HEADER_END
Packit Service a2489d
Packit Service a2489d
#endif