]> code.delx.au - gnu-emacs/blob - src/systime.h
Merge from emacs-24
[gnu-emacs] / 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 #include <sys/time.h> /* for 'struct timeval' */
43 \f
44 /* The type to use to represent non-negative temporal intervals. Its
45 address can be passed as the timeout argument to the pselect system
46 call. */
47 typedef struct timespec EMACS_TIME;
48
49 /* Resolution of EMACS_TIME time stamps (in units per second), and log
50 base 10 of the resolution. The log must be a positive integer. */
51 enum { EMACS_TIME_RESOLUTION = 1000000000 };
52 enum { LOG10_EMACS_TIME_RESOLUTION = 9 };
53
54 /* EMACS_SECS (TIME) is the seconds component of TIME.
55 EMACS_NSECS (TIME) is the nanoseconds component of TIME.
56 emacs_secs_addr (PTIME) is the address of *PTIME's seconds component. */
57 static inline time_t EMACS_SECS (EMACS_TIME t) { return t.tv_sec; }
58 static inline int EMACS_NSECS (EMACS_TIME t) { return t.tv_nsec; }
59 static inline time_t *emacs_secs_addr (EMACS_TIME *t) { return &t->tv_sec; }
60
61 /* Return an Emacs time with seconds S and nanoseconds NS. */
62 static inline EMACS_TIME
63 make_emacs_time (time_t s, int ns)
64 {
65 EMACS_TIME r = { s, ns };
66 return r;
67 }
68
69 /* Return an invalid Emacs time. */
70 static inline EMACS_TIME
71 invalid_emacs_time (void)
72 {
73 EMACS_TIME r = { 0, -1 };
74 return r;
75 }
76
77 /* Return current system time. */
78 static inline EMACS_TIME
79 current_emacs_time (void)
80 {
81 EMACS_TIME r;
82 gettime (&r);
83 return r;
84 }
85
86 /* Return the result of adding A to B, or of subtracting B from A.
87 On overflow, store an extremal value: ergo, if time_t is unsigned,
88 return 0 if the true answer would be negative.
89
90 WARNING: These are NOT general-purpose macros for adding or
91 subtracting arbitrary time values! They are generally intended to
92 be used with their first argument an absolute time since the epoch
93 and the second argument a non-negative offset. Do NOT use them for
94 anything else. */
95 static inline EMACS_TIME
96 add_emacs_time (EMACS_TIME a, EMACS_TIME b)
97 {
98 return timespec_add (a, b);
99 }
100 static inline EMACS_TIME
101 sub_emacs_time (EMACS_TIME a, EMACS_TIME b)
102 {
103 return timespec_sub (a, b);
104 }
105
106 /* Return the sign of the valid time stamp TIME, either -1, 0, or 1.
107 Note: this can only return a negative value if time_t is a signed
108 data type. */
109 static inline int
110 EMACS_TIME_SIGN (EMACS_TIME t)
111 {
112 return timespec_sign (t);
113 }
114
115 /* Return 1 if TIME is a valid time stamp. */
116 static inline int
117 EMACS_TIME_VALID_P (EMACS_TIME t)
118 {
119 return 0 <= t.tv_nsec;
120 }
121
122 /* Convert the double D to the greatest EMACS_TIME not greater than D.
123 On overflow, return an extremal value; in particular, if time_t is
124 an unsigned data type and D is negative, return zero. Return the
125 minimum EMACS_TIME if D is not a number. */
126 static inline EMACS_TIME
127 EMACS_TIME_FROM_DOUBLE (double d)
128 {
129 return dtotimespec (d);
130 }
131
132 /* Convert the Emacs time T to an approximate double value D. */
133 static inline double
134 EMACS_TIME_TO_DOUBLE (EMACS_TIME t)
135 {
136 return timespectod (t);
137 }
138
139 /* defined in sysdep.c */
140 extern int set_file_times (int, const char *, EMACS_TIME, EMACS_TIME);
141 extern struct timeval make_timeval (EMACS_TIME);
142
143 /* defined in keyboard.c */
144 extern void set_waiting_for_input (EMACS_TIME *);
145
146 /* When lisp.h is not included Lisp_Object is not defined (this can
147 happen when this files is used outside the src directory).
148 Use GCPRO1 to determine if lisp.h was included. */
149 #ifdef GCPRO1
150 /* defined in editfns.c */
151 extern Lisp_Object make_lisp_time (EMACS_TIME);
152 extern int decode_time_components (Lisp_Object, Lisp_Object, Lisp_Object,
153 Lisp_Object, EMACS_TIME *, double *);
154 extern EMACS_TIME lisp_time_argument (Lisp_Object);
155 #endif
156
157 /* Compare times T1 and T2 for equality, inequality etc. */
158 static inline int
159 EMACS_TIME_EQ (EMACS_TIME t1, EMACS_TIME t2)
160 {
161 return timespec_cmp (t1, t2) == 0;
162 }
163 static inline int
164 EMACS_TIME_NE (EMACS_TIME t1, EMACS_TIME t2)
165 {
166 return timespec_cmp (t1, t2) != 0;
167 }
168 static inline int
169 EMACS_TIME_GT (EMACS_TIME t1, EMACS_TIME t2)
170 {
171 return timespec_cmp (t1, t2) > 0;
172 }
173 static inline int
174 EMACS_TIME_GE (EMACS_TIME t1, EMACS_TIME t2)
175 {
176 return timespec_cmp (t1, t2) >= 0;
177 }
178 static inline int
179 EMACS_TIME_LT (EMACS_TIME t1, EMACS_TIME t2)
180 {
181 return timespec_cmp (t1, t2) < 0;
182 }
183 static inline int
184 EMACS_TIME_LE (EMACS_TIME t1, EMACS_TIME t2)
185 {
186 return timespec_cmp (t1, t2) <= 0;
187 }
188
189 #endif /* EMACS_SYSTIME_H */