* systime.h (EMACS_SUB_TIME): Clarify behavior with unsigned time_t.
[bpt/emacs.git] / src / systime.h
1 /* systime.h - System-dependent definitions for time manipulations.
2 Copyright (C) 1993-1994, 2002-2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 #ifndef EMACS_SYSTIME_H
20 #define EMACS_SYSTIME_H
21
22 #include <timespec.h>
23
24 #ifdef emacs
25 # ifdef HAVE_X_WINDOWS
26 # include <X11/X.h>
27 # else
28 typedef unsigned long Time;
29 # endif
30 #endif
31
32 /* On some configurations (hpux8.0, X11R4), sys/time.h and X11/Xos.h
33 disagree about the name of the guard symbol. */
34 #ifdef HPUX
35 #ifdef _STRUCT_TIMEVAL
36 #ifndef __TIMEVAL__
37 #define __TIMEVAL__
38 #endif
39 #endif
40 #endif
41
42 #ifdef WINDOWSNT
43 #include <sys/time.h> /* for 'struct timeval' */
44 #endif
45 \f
46 /* The type to use to represent temporal intervals. It can be passed
47 as the timeout argument to the pselect system call. */
48 #define EMACS_TIME struct timespec
49
50 /* Resolution of EMACS_TIME time stamps (in units per second), and log
51 base 10 of the resolution. The log must be a positive integer. */
52 #define EMACS_TIME_RESOLUTION 1000000000
53 #define LOG10_EMACS_TIME_RESOLUTION 9
54
55 /* EMACS_SECS (TIME) is an rvalue for the seconds component of TIME.
56 EMACS_SECS_ADDR (time) is the address of the seconds component.
57 EMACS_SET_SECS (TIME, SECONDS) sets that to SECONDS.
58
59 EMACS_NSECS (TIME) is an rvalue for the nanoseconds component of TIME.
60 EMACS_SET_NSECS (TIME, NANOSECONDS) sets that to NANOSECONDS.
61
62 EMACS_SET_SECS_NSECS (TIME, SECS, NSECS) sets both components of TIME. */
63 #define EMACS_SECS(time) ((time).tv_sec + 0)
64 #define EMACS_NSECS(time) ((time).tv_nsec + 0)
65 #define EMACS_SECS_ADDR(time) (&(time).tv_sec)
66 #define EMACS_SET_SECS(time, seconds) ((time).tv_sec = (seconds))
67 #define EMACS_SET_NSECS(time, ns) ((time).tv_nsec = (ns))
68 #define EMACS_SET_SECS_NSECS(time, s, ns) \
69 ((void) (EMACS_SET_SECS (time, s), EMACS_SET_NSECS (time, ns)))
70
71 /* Convenience macros for older code that counts microseconds. */
72 #define EMACS_SET_USECS(time, us) ((void) EMACS_SET_NSECS (time, (us) * 1000))
73 #define EMACS_SET_SECS_USECS(time, secs, usecs) \
74 (EMACS_SET_SECS (time, secs), EMACS_SET_USECS (time, usecs))
75
76 /* Set TIME to an invalid time stamp. */
77 #define EMACS_SET_INVALID_TIME(time) EMACS_SET_SECS_NSECS(time, 0, -1)
78
79 /* Set TIME to the current system time. */
80 #define EMACS_GET_TIME(time) gettime (&(time))
81
82 /* Put into DEST the result of adding SRC1 to SRC2, or of subtracting
83 SRC2 from SRC1. On overflow, store an extremal value: ergo, if
84 time_t is unsigned, return 0 if the true answer would be negative. */
85 #define EMACS_ADD_TIME(dest, src1, src2) ((dest) = timespec_add (src1, src2))
86 #define EMACS_SUB_TIME(dest, src1, src2) ((dest) = timespec_sub (src1, src2))
87
88 /* Return the sign of the valid time stamp TIME, either -1, 0, or 1. */
89 #define EMACS_TIME_SIGN(time) timespec_sign (time)
90
91 /* Return 1 if TIME is a valid time stamp. */
92 #define EMACS_TIME_VALID_P(time) (0 <= (time).tv_nsec)
93
94 /* Convert the double D to the greatest EMACS_TIME not greater than D.
95 On overflow, return an extremal value. Return the minimum
96 EMACS_TIME if D is not a number. */
97 #define EMACS_TIME_FROM_DOUBLE(d) dtotimespec (d)
98
99 /* Convert the Emacs time T to an approximate double value D. */
100 #define EMACS_TIME_TO_DOUBLE(t) timespectod (t)
101
102 /* defined in sysdep.c */
103 extern int set_file_times (int, const char *, EMACS_TIME, EMACS_TIME);
104 extern struct timeval make_timeval (EMACS_TIME);
105
106 /* defined in keyboard.c */
107 extern void set_waiting_for_input (EMACS_TIME *);
108
109 /* When lisp.h is not included Lisp_Object is not defined (this can
110 happen when this files is used outside the src directory).
111 Use GCPRO1 to determine if lisp.h was included. */
112 #ifdef GCPRO1
113 /* defined in editfns.c */
114 extern Lisp_Object make_lisp_time (EMACS_TIME);
115 extern int decode_time_components (Lisp_Object, Lisp_Object, Lisp_Object,
116 Lisp_Object, EMACS_TIME *, double *);
117 extern EMACS_TIME lisp_time_argument (Lisp_Object);
118 #endif
119
120 /* Compare times T1 and T2 for equality, inequality etc. */
121
122 #define EMACS_TIME_EQ(T1, T2) (timespec_cmp (T1, T2) == 0)
123 #define EMACS_TIME_NE(T1, T2) (timespec_cmp (T1, T2) != 0)
124 #define EMACS_TIME_GT(T1, T2) (timespec_cmp (T1, T2) > 0)
125 #define EMACS_TIME_GE(T1, T2) (timespec_cmp (T1, T2) >= 0)
126 #define EMACS_TIME_LT(T1, T2) (timespec_cmp (T1, T2) < 0)
127 #define EMACS_TIME_LE(T1, T2) (timespec_cmp (T1, T2) <= 0)
128
129 #endif /* EMACS_SYSTIME_H */