systime.h 5.6 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* systime.h - System-dependent definitions for time manipulations.
Karl Heuer's avatar
Karl Heuer committed
2
   Copyright (C) 1993, 1994 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
3 4 5 6 7

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Karl Heuer's avatar
Karl Heuer committed
8
the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
9 10 11 12 13 14 15 16 17
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
18 19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
20

21 22
#ifndef EMACS_SYSTIME_H
#define EMACS_SYSTIME_H
23

24
#ifdef TIME_WITH_SYS_TIME
Jim Blandy's avatar
Jim Blandy committed
25
#include <sys/time.h>
Jim Blandy's avatar
Jim Blandy committed
26
#include <time.h>
27
#else
28 29
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
30 31
#else
#include <time.h>
32
#endif
Jim Blandy's avatar
Jim Blandy committed
33
#endif
Jim Blandy's avatar
Jim Blandy committed
34

35 36 37 38 39 40
#ifdef HAVE_TZNAME
#ifndef tzname		/* For SGI.  */
extern char *tzname[];	/* RS6000 and others want it this way.  */
#endif
#endif

41 42
/* SVr4 doesn't actually declare this in its #include files.  */
#ifdef USG5_4
43
extern time_t timezone;
44 45
#endif

46 47 48 49 50 51
#ifdef VMS
#ifdef VAXC
#include "vmstime.h"
#endif
#endif

52 53
/* On some configurations (hpux8.0, X11R4), sys/time.h and X11/Xos.h
   disagree about the name of the guard symbol.  */
54
#ifdef HPUX
55 56 57 58 59
#ifdef _STRUCT_TIMEVAL
#ifndef __TIMEVAL__
#define __TIMEVAL__
#endif
#endif
60
#endif
Jim Blandy's avatar
Jim Blandy committed
61

Jim Blandy's avatar
Jim Blandy committed
62 63
/* EMACS_TIME is the type to use to represent temporal intervals -
   struct timeval on some systems, int on others.  It can be passed as
Richard M. Stallman's avatar
Richard M. Stallman committed
64
   the timeout argument to the select  system call.
Jim Blandy's avatar
Jim Blandy committed
65 66 67 68 69

   EMACS_SECS (TIME) is an rvalue for the seconds component of TIME.
   EMACS_SET_SECS (TIME, SECONDS) sets that to SECONDS.

   EMACS_HAS_USECS is defined iff EMACS_TIME has a usecs component.
Jim Blandy's avatar
Jim Blandy committed
70 71 72 73
   EMACS_USECS (TIME) is an rvalue for the microseconds component of TIME.
   	This returns zero if EMACS_TIME doesn't have a microseconds component.
   EMACS_SET_USECS (TIME, MICROSECONDS) sets that to MICROSECONDS.
	This does nothing if EMACS_TIME doesn't have a microseconds component.
Jim Blandy's avatar
Jim Blandy committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

   EMACS_SET_SECS_USECS (TIME, SECS, USECS) sets both components of TIME.

   EMACS_GET_TIME (TIME) stores the current system time in TIME, which
	should be an lvalue.

   EMACS_ADD_TIME (DEST, SRC1, SRC2) adds SRC1 to SRC2 and stores the
	result in DEST.  SRC should not be negative.

   EMACS_SUB_TIME (DEST, SRC1, SRC2) subtracts SRC2 from SRC1 and
	stores the result in DEST.  SRC should not be negative. 
   EMACS_TIME_NEG_P (TIME) is true iff TIME is negative.

*/

#ifdef HAVE_TIMEVAL

91 92
#define EMACS_HAS_USECS

Jim Blandy's avatar
Jim Blandy committed
93 94 95 96
#define EMACS_TIME struct timeval
#define EMACS_SECS(time)		    ((time).tv_sec  + 0)
#define EMACS_USECS(time)		    ((time).tv_usec + 0)
#define EMACS_SET_SECS(time, seconds)	    ((time).tv_sec  = (seconds))
Jim Blandy's avatar
Jim Blandy committed
97
#define EMACS_SET_USECS(time, microseconds) ((time).tv_usec = (microseconds))
Jim Blandy's avatar
Jim Blandy committed
98

99
/* On SVR4, the compiler may complain if given this extra BSD arg.  */
100
#ifdef GETTIMEOFDAY_ONE_ARGUMENT
101
#define EMACS_GET_TIME(time) gettimeofday (&(time))
102
#else /* not GETTIMEOFDAY_ONE_ARGUMENT */
103
#ifdef HAVE_STRUCT_TIMEZONE
104 105 106 107 108
#define EMACS_GET_TIME(time)			\
  do {						\
    struct timezone dummy;			\
    gettimeofday (&(time), &dummy);		\
  } while (0)
109 110 111 112
#else
/* Presumably the second arg is ignored.  */
#define EMACS_GET_TIME(time) gettimeofday (&(time), NULL)
#endif /* HAVE_STRUCT_TIMEZONE */
113
#endif /* not GETTIMEOFDAY_ONE_ARGUMENT */
Jim Blandy's avatar
Jim Blandy committed
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#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)
Jim Blandy's avatar
Jim Blandy committed
130 131

#define EMACS_TIME_NEG_P(time)					\
132
  ((long)(time).tv_sec < 0					\
Jim Blandy's avatar
Jim Blandy committed
133
   || ((time).tv_sec == 0					\
134
       && (long)(time).tv_usec < 0))
Jim Blandy's avatar
Jim Blandy committed
135

Jim Blandy's avatar
Jim Blandy committed
136
#else /* ! defined (HAVE_TIMEVAL) */
Jim Blandy's avatar
Jim Blandy committed
137 138 139

#define EMACS_TIME int
#define EMACS_SECS(time)		    (time)
Jim Blandy's avatar
Jim Blandy committed
140
#define EMACS_USECS(time)		    0
Jim Blandy's avatar
Jim Blandy committed
141
#define EMACS_SET_SECS(time, seconds)	    ((time) = (seconds))
Jim Blandy's avatar
Jim Blandy committed
142
#define EMACS_SET_USECS(time, usecs)	    0
Jim Blandy's avatar
Jim Blandy committed
143 144 145 146 147 148

#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)

Jim Blandy's avatar
Jim Blandy committed
149
#endif /* ! defined (HAVE_TIMEVAL) */
Jim Blandy's avatar
Jim Blandy committed
150 151 152 153

#define EMACS_SET_SECS_USECS(time, secs, usecs) 		\
  (EMACS_SET_SECS (time, secs), EMACS_SET_USECS (time, usecs))

Dave Love's avatar
Dave Love committed
154
extern int set_file_times __P ((char *, EMACS_TIME, EMACS_TIME));
155 156 157 158 159 160 161 162 163 164 165 166

/* Compare times T1 and T2.  Value is 0 if T1 and T2 are the same.
   Value is < 0 if T1 is less than T2.  Value is > 0 otherwise.  */

#define EMACS_TIME_CMP(T1, T2)			\
  (EMACS_SECS (T1) - EMACS_SECS (T2)		\
   + (EMACS_SECS (T1) == EMACS_SECS (T2)	\
      ? EMACS_USECS (T1) - EMACS_USECS (T2)	\
      : 0))

/* Compare times T1 and T2 for equality, inequality etc.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
167 168 169 170 171 172
#define EMACS_TIME_EQ(T1, T2) (EMACS_TIME_CMP (T1, T2) == 0)
#define EMACS_TIME_NE(T1, T2) (EMACS_TIME_CMP (T1, T2) != 0)
#define EMACS_TIME_GT(T1, T2) (EMACS_TIME_CMP (T1, T2) > 0)
#define EMACS_TIME_GE(T1, T2) (EMACS_TIME_CMP (T1, T2) >= 0)
#define EMACS_TIME_LT(T1, T2) (EMACS_TIME_CMP (T1, T2) < 0)
#define EMACS_TIME_LE(T1, T2) (EMACS_TIME_CMP (T1, T2) <= 0)
173

174
#endif /* EMACS_SYSTIME_H */