* systime.h (make_timeval): Declare as 'const'.
[bpt/emacs.git] / src / systime.h
CommitLineData
10a4cc63 1/* systime.h - System-dependent definitions for time manipulations.
ab422c4d 2 Copyright (C) 1993-1994, 2002-2013 Free Software Foundation, Inc.
f469625a
JB
3
4This file is part of GNU Emacs.
5
b9b1cc14 6GNU Emacs is free software: you can redistribute it and/or modify
f469625a 7it under the terms of the GNU General Public License as published by
b9b1cc14
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
f469625a
JB
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
b9b1cc14 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
f469625a 18
fb1b041d
DL
19#ifndef EMACS_SYSTIME_H
20#define EMACS_SYSTIME_H
482fa053 21
d35af63c 22#include <timespec.h>
7b89707c 23
f162bcc3
PE
24INLINE_HEADER_BEGIN
25#ifndef SYSTIME_INLINE
26# define SYSTIME_INLINE INLINE
27#endif
28
89d1bd22
PE
29#ifdef emacs
30# ifdef HAVE_X_WINDOWS
31# include <X11/X.h>
32# else
08dc5ae6 33typedef unsigned long Time;
89d1bd22 34# endif
08dc5ae6
PE
35#endif
36
71068d78
KH
37/* On some configurations (hpux8.0, X11R4), sys/time.h and X11/Xos.h
38 disagree about the name of the guard symbol. */
c810639a 39#ifdef HPUX
71068d78
KH
40#ifdef _STRUCT_TIMEVAL
41#ifndef __TIMEVAL__
42#define __TIMEVAL__
43#endif
44#endif
c810639a 45#endif
696056c2 46
696056c2 47#include <sys/time.h> /* for 'struct timeval' */
10a4cc63 48\f
d9bee437
EZ
49/* The type to use to represent non-negative temporal intervals. Its
50 address can be passed as the timeout argument to the pselect system
51 call. */
e9a9ae03 52typedef struct timespec EMACS_TIME;
f469625a 53
d35af63c
PE
54/* Resolution of EMACS_TIME time stamps (in units per second), and log
55 base 10 of the resolution. The log must be a positive integer. */
e9a9ae03
PE
56enum { EMACS_TIME_RESOLUTION = 1000000000 };
57enum { LOG10_EMACS_TIME_RESOLUTION = 9 };
58
59/* EMACS_SECS (TIME) is the seconds component of TIME.
60 EMACS_NSECS (TIME) is the nanoseconds component of TIME.
61 emacs_secs_addr (PTIME) is the address of *PTIME's seconds component. */
f162bcc3
PE
62SYSTIME_INLINE time_t EMACS_SECS (EMACS_TIME t) { return t.tv_sec; }
63SYSTIME_INLINE int EMACS_NSECS (EMACS_TIME t) { return t.tv_nsec; }
64SYSTIME_INLINE time_t *emacs_secs_addr (EMACS_TIME *t) { return &t->tv_sec; }
e9a9ae03
PE
65
66/* Return an Emacs time with seconds S and nanoseconds NS. */
f162bcc3 67SYSTIME_INLINE EMACS_TIME
e9a9ae03
PE
68make_emacs_time (time_t s, int ns)
69{
70 EMACS_TIME r = { s, ns };
71 return r;
72}
73
74/* Return an invalid Emacs time. */
f162bcc3 75SYSTIME_INLINE EMACS_TIME
e9a9ae03
PE
76invalid_emacs_time (void)
77{
78 EMACS_TIME r = { 0, -1 };
79 return r;
80}
81
82/* Return current system time. */
f162bcc3 83SYSTIME_INLINE EMACS_TIME
e9a9ae03
PE
84current_emacs_time (void)
85{
86 EMACS_TIME r;
87 gettime (&r);
88 return r;
89}
90
91/* Return the result of adding A to B, or of subtracting B from A.
92 On overflow, store an extremal value: ergo, if time_t is unsigned,
d9bee437
EZ
93 return 0 if the true answer would be negative.
94
95 WARNING: These are NOT general-purpose macros for adding or
96 subtracting arbitrary time values! They are generally intended to
97 be used with their first argument an absolute time since the epoch
98 and the second argument a non-negative offset. Do NOT use them for
99 anything else. */
f162bcc3 100SYSTIME_INLINE EMACS_TIME
e9a9ae03
PE
101add_emacs_time (EMACS_TIME a, EMACS_TIME b)
102{
103 return timespec_add (a, b);
104}
f162bcc3 105SYSTIME_INLINE EMACS_TIME
e9a9ae03
PE
106sub_emacs_time (EMACS_TIME a, EMACS_TIME b)
107{
108 return timespec_sub (a, b);
109}
f469625a 110
d9bee437
EZ
111/* Return the sign of the valid time stamp TIME, either -1, 0, or 1.
112 Note: this can only return a negative value if time_t is a signed
113 data type. */
f162bcc3 114SYSTIME_INLINE int
e9a9ae03
PE
115EMACS_TIME_SIGN (EMACS_TIME t)
116{
117 return timespec_sign (t);
118}
f469625a 119
d35af63c 120/* Return 1 if TIME is a valid time stamp. */
f162bcc3 121SYSTIME_INLINE int
e9a9ae03
PE
122EMACS_TIME_VALID_P (EMACS_TIME t)
123{
908589fd 124 return t.tv_nsec >= 0;
e9a9ae03 125}
722687f5 126
d35af63c 127/* Convert the double D to the greatest EMACS_TIME not greater than D.
d9bee437
EZ
128 On overflow, return an extremal value; in particular, if time_t is
129 an unsigned data type and D is negative, return zero. Return the
130 minimum EMACS_TIME if D is not a number. */
f162bcc3 131SYSTIME_INLINE EMACS_TIME
e9a9ae03
PE
132EMACS_TIME_FROM_DOUBLE (double d)
133{
134 return dtotimespec (d);
135}
f469625a 136
d35af63c 137/* Convert the Emacs time T to an approximate double value D. */
f162bcc3 138SYSTIME_INLINE double
e9a9ae03
PE
139EMACS_TIME_TO_DOUBLE (EMACS_TIME t)
140{
141 return timespectod (t);
142}
f469625a 143
d35af63c
PE
144/* defined in sysdep.c */
145extern int set_file_times (int, const char *, EMACS_TIME, EMACS_TIME);
b8dd59f7 146extern struct timeval make_timeval (EMACS_TIME) ATTRIBUTE_CONST;
c53a6701 147
43f15d4a 148/* defined in keyboard.c */
383e0970 149extern void set_waiting_for_input (EMACS_TIME *);
43f15d4a 150
fa8459a3
DN
151/* When lisp.h is not included Lisp_Object is not defined (this can
152 happen when this files is used outside the src directory).
153 Use GCPRO1 to determine if lisp.h was included. */
154#ifdef GCPRO1
d35af63c
PE
155/* defined in editfns.c */
156extern Lisp_Object make_lisp_time (EMACS_TIME);
a08d4ba7
PE
157extern bool decode_time_components (Lisp_Object, Lisp_Object, Lisp_Object,
158 Lisp_Object, EMACS_TIME *, double *);
31571fd7 159extern EMACS_TIME lisp_time_argument (Lisp_Object);
fa8459a3
DN
160#endif
161
c53a6701 162/* Compare times T1 and T2 for equality, inequality etc. */
f162bcc3 163SYSTIME_INLINE int
e9a9ae03
PE
164EMACS_TIME_EQ (EMACS_TIME t1, EMACS_TIME t2)
165{
166 return timespec_cmp (t1, t2) == 0;
167}
f162bcc3 168SYSTIME_INLINE int
e9a9ae03
PE
169EMACS_TIME_NE (EMACS_TIME t1, EMACS_TIME t2)
170{
171 return timespec_cmp (t1, t2) != 0;
172}
f162bcc3 173SYSTIME_INLINE int
e9a9ae03
PE
174EMACS_TIME_GT (EMACS_TIME t1, EMACS_TIME t2)
175{
176 return timespec_cmp (t1, t2) > 0;
177}
f162bcc3 178SYSTIME_INLINE int
e9a9ae03
PE
179EMACS_TIME_GE (EMACS_TIME t1, EMACS_TIME t2)
180{
181 return timespec_cmp (t1, t2) >= 0;
182}
f162bcc3 183SYSTIME_INLINE int
e9a9ae03
PE
184EMACS_TIME_LT (EMACS_TIME t1, EMACS_TIME t2)
185{
186 return timespec_cmp (t1, t2) < 0;
187}
f162bcc3 188SYSTIME_INLINE int
e9a9ae03
PE
189EMACS_TIME_LE (EMACS_TIME t1, EMACS_TIME t2)
190{
191 return timespec_cmp (t1, t2) <= 0;
192}
c53a6701 193
f162bcc3
PE
194INLINE_HEADER_END
195
fb1b041d 196#endif /* EMACS_SYSTIME_H */