maintainer changed: was lord, now jimb; first import
[bpt/guile.git] / libguile / stime.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42
43#include <stdio.h>
44#include "_scm.h"
45#ifdef HAVE_UNISTD_H
46#include <unistd.h>
47#endif
48
49\f
50# ifdef HAVE_SYS_TYPES_H
51# include <sys/types.h>
52# endif
53
54# ifdef TIME_WITH_SYS_TIME
55# include <sys/time.h>
56# include <time.h>
57# else
58# ifdef HAVE_SYS_TIME_H
59# include <sys/time.h>
60# else
61# ifdef HAVE_TIME_H
62# include <time.h>
63# endif
64# endif
65# endif
66
67# ifdef HAVE_SYS_TIMES_H
68# include <sys/times.h>
69# else
70# ifdef HAVE_SYS_TIMEB_H
71# include <sys/timeb.h>
72# endif
73# endif
74
75#ifdef CLK_TCK
76# define CLKTCK CLK_TCK
77# ifdef CLOCKS_PER_SEC
78# ifdef unix
79# ifndef ARM_ULIB
80# include <sys/times.h>
81# endif
82# define LACK_CLOCK
83 /* This is because clock() might be POSIX rather than ANSI.
84 This occurs on HP-UX machines */
85# endif
86# endif
87#else
88# ifdef CLOCKS_PER_SEC
89# define CLKTCK CLOCKS_PER_SEC
90# else
91# define LACK_CLOCK
92# define CLKTCK 60
93# endif
94#endif
95
96
97# ifdef HAVE_FTIME
98# include <sys/timeb.h>
99# endif
100
101
102#ifdef __STDC__
103# define timet time_t
104#else
105# define timet long
106#endif
107
108#ifdef HAVE_TIMES
109#ifdef __STDC__
110static
111long mytime(void)
112#else
113static
114long mytime()
115#endif
116{
117 struct tms time_buffer;
118 times(&time_buffer);
119 return time_buffer.tms_utime + time_buffer.tms_stime;
120}
121#else
122# ifdef LACK_CLOCK
123# define mytime() ((time((timet*)0) - scm_your_base) * CLKTCK)
124# else
125# define mytime clock
126# endif
127#endif
128
129
130
131#ifdef HAVE_FTIME
132
133struct timeb scm_your_base = {0};
134SCM_PROC(s_get_internal_real_time, "get-internal-real-time", 0, 0, 0, scm_get_internal_real_time);
135#ifdef __STDC__
136SCM
137scm_get_internal_real_time(void)
138#else
139SCM
140scm_get_internal_real_time()
141#endif
142{
143 struct timeb time_buffer;
144 long tmp;
145 ftime(&time_buffer);
146 time_buffer.time -= scm_your_base.time;
147 tmp = time_buffer.millitm - scm_your_base.millitm;
148 tmp = time_buffer.time*1000L + tmp;
149 tmp *= CLKTCK;
150 tmp /= 1000;
151 return SCM_MAKINUM(tmp);
152}
153
154#else
155
156timet scm_your_base = 0;
157SCM_PROC(s_get_internal_real_time, "get-internal-real-time", 0, 0, 0, scm_get_internal_real_time);
158#ifdef __STDC__
159SCM
160scm_get_internal_real_time(void)
161#else
162SCM
163scm_get_internal_real_time()
164#endif
165{
166 return SCM_MAKINUM((time((timet*)0) - scm_your_base) * (int)CLKTCK);
167}
168#endif
169
170
171
172static long scm_my_base = 0;
173
174SCM_PROC(s_get_internal_run_time, "get-internal-run-time", 0, 0, 0, scm_get_internal_run_time);
175#ifdef __STDC__
176SCM
177scm_get_internal_run_time(void)
178#else
179SCM
180scm_get_internal_run_time()
181#endif
182{
183 return SCM_MAKINUM(mytime()-scm_my_base);
184}
185
186SCM_PROC(s_current_time, "current-time", 0, 0, 0, scm_current_time);
187#ifdef __STDC__
188SCM
189scm_current_time(void)
190#else
191SCM
192scm_current_time()
193#endif
194{
195 timet timv = time((timet*)0);
196 SCM ans;
197 ans = scm_ulong2num(timv);
198 return SCM_BOOL_F==ans ? SCM_MAKINUM(timv) : ans;
199}
200
201#ifdef __STDC__
202long
203scm_time_in_msec(long x)
204#else
205long
206scm_time_in_msec(x)
207 long x;
208#endif
209{
210 if (CLKTCK==60) return (x*50)/3;
211 else
212 return (CLKTCK < 1000 ? x*(1000L/(long)CLKTCK) : (x*1000L)/(long)CLKTCK);
213}
214
215#ifdef __STDC__
216void
217scm_init_stime(void)
218#else
219void
220scm_init_stime()
221#endif
222{
223 scm_sysintern("internal-time-units-per-second",
224 SCM_MAKINUM((long)CLKTCK));
225
226#ifdef HAVE_FTIME
227 if (!scm_your_base.time) ftime(&scm_your_base);
228#else
229 if (!scm_your_base) time(&scm_your_base);
230#endif
231
232 if (!scm_my_base) scm_my_base = mytime();
233
234#include "stime.x"
235}
236