-/* On SVR4, the compiler may complain if given this extra BSD arg. */
-#ifdef GETTIMEOFDAY_ONE_ARGUMENT
-#define EMACS_GET_TIME(time) gettimeofday (&(time))
-#else /* not GETTIMEOFDAY_ONE_ARGUMENT */
-/* Presumably the second arg is ignored. */
-#define EMACS_GET_TIME(time) gettimeofday (&(time), NULL)
-#endif /* not GETTIMEOFDAY_ONE_ARGUMENT */
-
-#define EMACS_ADD_TIME(dest, src1, src2) \
- do { \
- (dest).tv_sec = (src1).tv_sec + (src2).tv_sec; \
- (dest).tv_usec = (src1).tv_usec + (src2).tv_usec; \
- if ((dest).tv_usec > 1000000) \
- (dest).tv_usec -= 1000000, (dest).tv_sec++; \
- } while (0)
-
-#define EMACS_SUB_TIME(dest, src1, src2) \
- do { \
- (dest).tv_sec = (src1).tv_sec - (src2).tv_sec; \
- (dest).tv_usec = (src1).tv_usec - (src2).tv_usec; \
- if ((dest).tv_usec < 0) \
- (dest).tv_usec += 1000000, (dest).tv_sec--; \
- } while (0)
-
-#define EMACS_TIME_NEG_P(time) \
- ((long)(time).tv_sec < 0 \
- || ((time).tv_sec == 0 \
- && (long)(time).tv_usec < 0))
-
-#else /* ! defined (HAVE_TIMEVAL) */
-
-#define EMACS_TIME int
-#define EMACS_SECS(time) (time)
-#define EMACS_USECS(time) 0
-#define EMACS_SET_SECS(time, seconds) ((time) = (seconds))
-#define EMACS_SET_USECS(time, usecs) 0
-
-#define EMACS_GET_TIME(t) ((t) = time ((long *) 0))
-#define EMACS_ADD_TIME(dest, src1, src2) ((dest) = (src1) + (src2))
-#define EMACS_SUB_TIME(dest, src1, src2) ((dest) = (src1) - (src2))
-#define EMACS_TIME_NEG_P(t) ((t) < 0)
-
-#endif /* ! defined (HAVE_TIMEVAL) */
-
-#define EMACS_SET_SECS_USECS(time, secs, usecs) \
- (EMACS_SET_SECS (time, secs), EMACS_SET_USECS (time, usecs))
-
-extern int set_file_times (const char *, EMACS_TIME, EMACS_TIME);
+#include <sys/time.h> /* for 'struct timeval' */
+\f
+/* The type to use to represent non-negative temporal intervals. Its
+ address can be passed as the timeout argument to the pselect system
+ call. */
+typedef struct timespec EMACS_TIME;
+
+/* Resolution of EMACS_TIME time stamps (in units per second), and log
+ base 10 of the resolution. The log must be a positive integer. */
+enum { EMACS_TIME_RESOLUTION = 1000000000 };
+enum { LOG10_EMACS_TIME_RESOLUTION = 9 };
+
+/* EMACS_SECS (TIME) is the seconds component of TIME.
+ EMACS_NSECS (TIME) is the nanoseconds component of TIME.
+ emacs_secs_addr (PTIME) is the address of *PTIME's seconds component. */
+SYSTIME_INLINE time_t EMACS_SECS (EMACS_TIME t) { return t.tv_sec; }
+SYSTIME_INLINE int EMACS_NSECS (EMACS_TIME t) { return t.tv_nsec; }
+SYSTIME_INLINE time_t *emacs_secs_addr (EMACS_TIME *t) { return &t->tv_sec; }
+
+/* Return an Emacs time with seconds S and nanoseconds NS. */
+SYSTIME_INLINE EMACS_TIME
+make_emacs_time (time_t s, int ns)
+{
+ EMACS_TIME r = { s, ns };
+ return r;
+}
+
+/* Return an invalid Emacs time. */
+SYSTIME_INLINE EMACS_TIME
+invalid_emacs_time (void)
+{
+ EMACS_TIME r = { 0, -1 };
+ return r;
+}
+
+/* Return current system time. */
+SYSTIME_INLINE EMACS_TIME
+current_emacs_time (void)
+{
+ EMACS_TIME r;
+ gettime (&r);
+ return r;
+}
+
+/* Return the result of adding A to B, or of subtracting B from A.
+ On overflow, store an extremal value: ergo, if time_t is unsigned,
+ return 0 if the true answer would be negative.
+
+ WARNING: These are NOT general-purpose macros for adding or
+ subtracting arbitrary time values! They are generally intended to
+ be used with their first argument an absolute time since the epoch
+ and the second argument a non-negative offset. Do NOT use them for
+ anything else. */
+SYSTIME_INLINE EMACS_TIME
+add_emacs_time (EMACS_TIME a, EMACS_TIME b)
+{
+ return timespec_add (a, b);
+}
+SYSTIME_INLINE EMACS_TIME
+sub_emacs_time (EMACS_TIME a, EMACS_TIME b)
+{
+ return timespec_sub (a, b);
+}
+
+/* Return the sign of the valid time stamp TIME, either -1, 0, or 1.
+ Note: this can only return a negative value if time_t is a signed
+ data type. */
+SYSTIME_INLINE int
+EMACS_TIME_SIGN (EMACS_TIME t)
+{
+ return timespec_sign (t);
+}
+
+/* Return 1 if TIME is a valid time stamp. */
+SYSTIME_INLINE int
+EMACS_TIME_VALID_P (EMACS_TIME t)
+{
+ return 0 <= t.tv_nsec;
+}
+
+/* Convert the double D to the greatest EMACS_TIME not greater than D.
+ On overflow, return an extremal value; in particular, if time_t is
+ an unsigned data type and D is negative, return zero. Return the
+ minimum EMACS_TIME if D is not a number. */
+SYSTIME_INLINE EMACS_TIME
+EMACS_TIME_FROM_DOUBLE (double d)
+{
+ return dtotimespec (d);
+}
+
+/* Convert the Emacs time T to an approximate double value D. */
+SYSTIME_INLINE double
+EMACS_TIME_TO_DOUBLE (EMACS_TIME t)
+{
+ return timespectod (t);
+}
+
+/* defined in sysdep.c */
+extern int set_file_times (int, const char *, EMACS_TIME, EMACS_TIME);
+extern struct timeval make_timeval (EMACS_TIME);