Commit | Line | Data |
---|---|---|
e6e2e95a | 1 | /* Copyright (C) 1995,1996,1997,1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
1c299a6b | 2 | * |
0f2d19dd JB |
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. | |
1c299a6b | 7 | * |
0f2d19dd JB |
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. | |
1c299a6b | 12 | * |
0f2d19dd JB |
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 | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
47 | #include <stdio.h> | |
e6e2e95a MD |
48 | #include <errno.h> |
49 | ||
a0599745 MD |
50 | #include "libguile/_scm.h" |
51 | #include "libguile/feature.h" | |
52 | #include "libguile/strings.h" | |
53 | #include "libguile/vectors.h" | |
20e6290e | 54 | |
a0599745 MD |
55 | #include "libguile/validate.h" |
56 | #include "libguile/stime.h" | |
20e6290e | 57 | |
0f2d19dd JB |
58 | #ifdef HAVE_UNISTD_H |
59 | #include <unistd.h> | |
60 | #endif | |
61 | ||
62 | \f | |
63 | # ifdef HAVE_SYS_TYPES_H | |
64 | # include <sys/types.h> | |
65 | # endif | |
66 | ||
a15e6dcc MV |
67 | #ifdef HAVE_STRING_H |
68 | #include <string.h> | |
69 | #endif | |
70 | ||
0f2d19dd JB |
71 | # ifdef TIME_WITH_SYS_TIME |
72 | # include <sys/time.h> | |
73 | # include <time.h> | |
74 | # else | |
75 | # ifdef HAVE_SYS_TIME_H | |
76 | # include <sys/time.h> | |
77 | # else | |
78 | # ifdef HAVE_TIME_H | |
79 | # include <time.h> | |
80 | # endif | |
81 | # endif | |
82 | # endif | |
83 | ||
b1978258 GH |
84 | #ifdef HAVE_SYS_TIMES_H |
85 | # include <sys/times.h> | |
86 | #endif | |
87 | ||
88 | #ifdef HAVE_SYS_TIMEB_H | |
89 | # include <sys/timeb.h> | |
90 | #endif | |
0f2d19dd | 91 | |
b9525b92 GH |
92 | #ifndef tzname /* For SGI. */ |
93 | extern char *tzname[]; /* RS6000 and others reject char **tzname. */ | |
94 | #endif | |
95 | ||
4d3bacdd JB |
96 | #ifdef MISSING_STRPTIME_DECL |
97 | extern char *strptime (); | |
98 | #endif | |
99 | ||
cda55316 | 100 | /* This should be figured out by autoconf. */ |
a2fc27b5 JB |
101 | #if ! defined(CLKTCK) && defined(CLK_TCK) |
102 | # define CLKTCK CLK_TCK | |
103 | #endif | |
104 | #if ! defined(CLKTCK) && defined(CLOCKS_PER_SEC) | |
0f2d19dd | 105 | # define CLKTCK CLOCKS_PER_SEC |
a2fc27b5 JB |
106 | #endif |
107 | #if ! defined(CLKTCK) | |
0f2d19dd | 108 | # define CLKTCK 60 |
0f2d19dd JB |
109 | #endif |
110 | ||
0f2d19dd JB |
111 | #ifdef __STDC__ |
112 | # define timet time_t | |
113 | #else | |
114 | # define timet long | |
115 | #endif | |
116 | ||
117 | #ifdef HAVE_TIMES | |
0f2d19dd | 118 | static |
1c299a6b | 119 | timet mytime() |
0f2d19dd JB |
120 | { |
121 | struct tms time_buffer; | |
122 | times(&time_buffer); | |
123 | return time_buffer.tms_utime + time_buffer.tms_stime; | |
124 | } | |
125 | #else | |
126 | # ifdef LACK_CLOCK | |
127 | # define mytime() ((time((timet*)0) - scm_your_base) * CLKTCK) | |
128 | # else | |
129 | # define mytime clock | |
130 | # endif | |
131 | #endif | |
132 | ||
0f2d19dd | 133 | #ifdef HAVE_FTIME |
0f2d19dd | 134 | struct timeb scm_your_base = {0}; |
b450f070 GB |
135 | #else |
136 | timet scm_your_base = 0; | |
137 | #endif | |
1bbd0b84 | 138 | |
1c299a6b | 139 | SCM_DEFINE (scm_get_internal_real_time, "get-internal-real-time", 0, 0, 0, |
1bbd0b84 | 140 | (), |
1e6808ea MG |
141 | "Return the number of time units since the interpreter was\n" |
142 | "started.") | |
1bbd0b84 | 143 | #define FUNC_NAME s_scm_get_internal_real_time |
0f2d19dd | 144 | { |
b450f070 | 145 | #ifdef HAVE_FTIME |
0f2d19dd | 146 | struct timeb time_buffer; |
55c4d089 JB |
147 | |
148 | SCM tmp; | |
149 | ftime (&time_buffer); | |
0f2d19dd | 150 | time_buffer.time -= scm_your_base.time; |
55c4d089 JB |
151 | tmp = scm_long2num (time_buffer.millitm - scm_your_base.millitm); |
152 | tmp = scm_sum (tmp, | |
153 | scm_product (SCM_MAKINUM (1000), | |
154 | SCM_MAKINUM (time_buffer.time))); | |
155 | return scm_quotient (scm_product (tmp, SCM_MAKINUM (CLKTCK)), | |
156 | SCM_MAKINUM (1000)); | |
0f2d19dd | 157 | #else |
19468eff | 158 | return scm_long2num((time((timet*)0) - scm_your_base) * (int)CLKTCK); |
b450f070 | 159 | #endif /* HAVE_FTIME */ |
0f2d19dd | 160 | } |
1bbd0b84 GB |
161 | #undef FUNC_NAME |
162 | ||
0f2d19dd | 163 | |
f25f761d | 164 | #ifdef HAVE_TIMES |
1c299a6b | 165 | SCM_DEFINE (scm_times, "times", 0, 0, 0, |
1bbd0b84 | 166 | (void), |
1e6808ea MG |
167 | "Return an object with information about real and processor\n" |
168 | "time. The following procedures accept such an object as an\n" | |
169 | "argument and return a selected component:\n" | |
170 | "\n" | |
b380b885 MD |
171 | "@table @code\n" |
172 | "@item tms:clock\n" | |
173 | "The current real time, expressed as time units relative to an\n" | |
174 | "arbitrary base.\n" | |
175 | "@item tms:utime\n" | |
176 | "The CPU time units used by the calling process.\n" | |
177 | "@item tms:stime\n" | |
1e6808ea MG |
178 | "The CPU time units used by the system on behalf of the calling\n" |
179 | "process.\n" | |
b380b885 | 180 | "@item tms:cutime\n" |
1e6808ea MG |
181 | "The CPU time units used by terminated child processes of the\n" |
182 | "calling process, whose status has been collected (e.g., using\n" | |
183 | "@code{waitpid}).\n" | |
b380b885 | 184 | "@item tms:cstime\n" |
1e6808ea | 185 | "Similarly, the CPU times units used by the system on behalf of\n" |
b380b885 MD |
186 | "terminated child processes.\n" |
187 | "@end table") | |
1bbd0b84 | 188 | #define FUNC_NAME s_scm_times |
6afcd3b2 | 189 | { |
6afcd3b2 GH |
190 | struct tms t; |
191 | clock_t rv; | |
192 | ||
00ffa0e7 | 193 | SCM result = scm_c_make_vector (5, SCM_UNDEFINED); |
6afcd3b2 GH |
194 | rv = times (&t); |
195 | if (rv == -1) | |
1bbd0b84 | 196 | SCM_SYSERROR; |
6afcd3b2 GH |
197 | SCM_VELTS (result)[0] = scm_long2num (rv); |
198 | SCM_VELTS (result)[1] = scm_long2num (t.tms_utime); | |
199 | SCM_VELTS (result)[2] = scm_long2num (t.tms_stime); | |
200 | SCM_VELTS (result)[3] = scm_long2num (t.tms_cutime); | |
201 | SCM_VELTS (result)[4] = scm_long2num (t.tms_cstime); | |
202 | return result; | |
6afcd3b2 | 203 | } |
1bbd0b84 | 204 | #undef FUNC_NAME |
f25f761d | 205 | #endif /* HAVE_TIMES */ |
6afcd3b2 | 206 | |
0f2d19dd JB |
207 | static long scm_my_base = 0; |
208 | ||
1c299a6b ML |
209 | long |
210 | scm_c_get_internal_run_time () | |
211 | { | |
212 | return mytime () - scm_my_base; | |
213 | } | |
214 | ||
215 | SCM_DEFINE (scm_get_internal_run_time, "get-internal-run-time", 0, 0, 0, | |
1bbd0b84 | 216 | (void), |
1e6808ea MG |
217 | "Return the number of time units of processor time used by the\n" |
218 | "interpreter. Both @emph{system} and @emph{user} time are\n" | |
219 | "included but subprocesses are not.") | |
1bbd0b84 | 220 | #define FUNC_NAME s_scm_get_internal_run_time |
0f2d19dd | 221 | { |
1c299a6b | 222 | return scm_long2num (scm_c_get_internal_run_time ()); |
0f2d19dd | 223 | } |
1bbd0b84 | 224 | #undef FUNC_NAME |
0f2d19dd | 225 | |
1c299a6b | 226 | SCM_DEFINE (scm_current_time, "current-time", 0, 0, 0, |
1bbd0b84 | 227 | (void), |
1e6808ea MG |
228 | "Return the number of seconds since 1970-01-01 00:00:00 UTC,\n" |
229 | "excluding leap seconds.") | |
1bbd0b84 | 230 | #define FUNC_NAME s_scm_current_time |
0f2d19dd | 231 | { |
19468eff GH |
232 | timet timv; |
233 | ||
234 | SCM_DEFER_INTS; | |
235 | if ((timv = time (0)) == -1) | |
1bbd0b84 | 236 | SCM_SYSERROR; |
19468eff GH |
237 | SCM_ALLOW_INTS; |
238 | return scm_long2num((long) timv); | |
239 | } | |
1bbd0b84 | 240 | #undef FUNC_NAME |
19468eff | 241 | |
1c299a6b | 242 | SCM_DEFINE (scm_gettimeofday, "gettimeofday", 0, 0, 0, |
1bbd0b84 | 243 | (void), |
1e6808ea MG |
244 | "Return a pair containing the number of seconds and microseconds\n" |
245 | "since 1970-01-01 00:00:00 UTC, excluding leap seconds. Note:\n" | |
246 | "whether true microsecond resolution is available depends on the\n" | |
247 | "operating system.") | |
1bbd0b84 | 248 | #define FUNC_NAME s_scm_gettimeofday |
19468eff GH |
249 | { |
250 | #ifdef HAVE_GETTIMEOFDAY | |
251 | struct timeval time; | |
252 | ||
253 | SCM_DEFER_INTS; | |
254 | if (gettimeofday (&time, NULL) == -1) | |
1bbd0b84 | 255 | SCM_SYSERROR; |
19468eff GH |
256 | SCM_ALLOW_INTS; |
257 | return scm_cons (scm_long2num ((long) time.tv_sec), | |
258 | scm_long2num ((long) time.tv_usec)); | |
259 | #else | |
260 | # ifdef HAVE_FTIME | |
261 | struct timeb time; | |
262 | ||
263 | ftime(&time); | |
1c299a6b | 264 | return scm_cons (scm_long2num ((long) time.time), |
9a81afca | 265 | SCM_MAKINUM (time.millitm * 1000)); |
19468eff GH |
266 | # else |
267 | timet timv; | |
1c299a6b | 268 | |
19468eff GH |
269 | SCM_DEFER_INTS; |
270 | if ((timv = time (0)) == -1) | |
1bbd0b84 | 271 | SCM_SYSERROR; |
19468eff GH |
272 | SCM_ALLOW_INTS; |
273 | return scm_cons (scm_long2num (timv), SCM_MAKINUM (0)); | |
274 | # endif | |
275 | #endif | |
276 | } | |
1bbd0b84 | 277 | #undef FUNC_NAME |
19468eff GH |
278 | |
279 | static SCM | |
280 | filltime (struct tm *bd_time, int zoff, char *zname) | |
281 | { | |
00ffa0e7 | 282 | SCM result = scm_c_make_vector (11, SCM_UNDEFINED); |
19468eff GH |
283 | |
284 | SCM_VELTS (result)[0] = SCM_MAKINUM (bd_time->tm_sec); | |
285 | SCM_VELTS (result)[1] = SCM_MAKINUM (bd_time->tm_min); | |
286 | SCM_VELTS (result)[2] = SCM_MAKINUM (bd_time->tm_hour); | |
287 | SCM_VELTS (result)[3] = SCM_MAKINUM (bd_time->tm_mday); | |
288 | SCM_VELTS (result)[4] = SCM_MAKINUM (bd_time->tm_mon); | |
289 | SCM_VELTS (result)[5] = SCM_MAKINUM (bd_time->tm_year); | |
290 | SCM_VELTS (result)[6] = SCM_MAKINUM (bd_time->tm_wday); | |
291 | SCM_VELTS (result)[7] = SCM_MAKINUM (bd_time->tm_yday); | |
292 | SCM_VELTS (result)[8] = SCM_MAKINUM (bd_time->tm_isdst); | |
293 | SCM_VELTS (result)[9] = SCM_MAKINUM (zoff); | |
b9525b92 | 294 | SCM_VELTS (result)[10] = zname ? scm_makfrom0str (zname) : SCM_BOOL_F; |
19468eff GH |
295 | return result; |
296 | } | |
297 | ||
ef9ff3fd GH |
298 | static char tzvar[3] = "TZ"; |
299 | extern char ** environ; | |
300 | ||
38c1d3c4 GH |
301 | /* if zone is set, create a temporary environment with only a TZ |
302 | string. other threads or interrupt handlers shouldn't be allowed | |
303 | to run until the corresponding restorezone is called. hence the use | |
304 | of a static variable for tmpenv is no big deal. */ | |
ef9ff3fd | 305 | static char ** |
3eeba8d4 | 306 | setzone (SCM zone, int pos, const char *subr) |
b9525b92 | 307 | { |
ef9ff3fd | 308 | char **oldenv = 0; |
b9525b92 GH |
309 | |
310 | if (!SCM_UNBNDP (zone)) | |
311 | { | |
ef9ff3fd | 312 | static char *tmpenv[2]; |
b9525b92 GH |
313 | char *buf; |
314 | ||
9fd38a3d | 315 | SCM_ASSERT (SCM_STRINGP (zone), zone, pos, subr); |
a6d9e5ab | 316 | SCM_STRING_COERCE_0TERMINATION_X (zone); |
9fd38a3d | 317 | buf = scm_must_malloc (SCM_STRING_LENGTH (zone) + sizeof (tzvar) + 1, subr); |
a6d9e5ab | 318 | sprintf (buf, "%s=%s", tzvar, SCM_STRING_CHARS (zone)); |
ef9ff3fd GH |
319 | oldenv = environ; |
320 | tmpenv[0] = buf; | |
321 | tmpenv[1] = 0; | |
322 | environ = tmpenv; | |
b9525b92 | 323 | } |
ef9ff3fd | 324 | return oldenv; |
b9525b92 GH |
325 | } |
326 | ||
327 | static void | |
3eeba8d4 | 328 | restorezone (SCM zone, char **oldenv, const char *subr) |
b9525b92 GH |
329 | { |
330 | if (!SCM_UNBNDP (zone)) | |
331 | { | |
ef9ff3fd GH |
332 | scm_must_free (environ[0]); |
333 | environ = oldenv; | |
38c1d3c4 GH |
334 | #ifdef HAVE_TZSET |
335 | /* for the possible benefit of user code linked with libguile. */ | |
b9525b92 | 336 | tzset(); |
38c1d3c4 | 337 | #endif |
b9525b92 GH |
338 | } |
339 | } | |
340 | ||
1c299a6b | 341 | SCM_DEFINE (scm_localtime, "localtime", 1, 1, 0, |
1bbd0b84 | 342 | (SCM time, SCM zone), |
1e6808ea MG |
343 | "Return an object representing the broken down components of\n" |
344 | "@var{time}, an integer like the one returned by\n" | |
345 | "@code{current-time}. The time zone for the calculation is\n" | |
346 | "optionally specified by @var{zone} (a string), otherwise the\n" | |
347 | "@code{TZ} environment variable or the system default is used.") | |
1bbd0b84 | 348 | #define FUNC_NAME s_scm_localtime |
19468eff GH |
349 | { |
350 | timet itime; | |
4edc089c | 351 | struct tm *ltptr, lt, *utc; |
19468eff GH |
352 | SCM result; |
353 | int zoff; | |
354 | char *zname = 0; | |
ef9ff3fd | 355 | char **oldenv; |
19468eff GH |
356 | int err; |
357 | ||
1bbd0b84 | 358 | itime = SCM_NUM2LONG (1,time); |
38c1d3c4 GH |
359 | |
360 | /* deferring interupts is essential since a) setzone may install a temporary | |
361 | environment b) localtime uses a static buffer. */ | |
19468eff | 362 | SCM_DEFER_INTS; |
1bbd0b84 | 363 | oldenv = setzone (zone, SCM_ARG2, FUNC_NAME); |
38c1d3c4 GH |
364 | #ifdef LOCALTIME_CACHE |
365 | tzset (); | |
366 | #endif | |
4edc089c | 367 | ltptr = localtime (&itime); |
19468eff | 368 | err = errno; |
4edc089c | 369 | if (ltptr) |
19468eff | 370 | { |
4d3bacdd | 371 | const char *ptr; |
ef9ff3fd | 372 | |
43ff3170 GH |
373 | /* copy zone name before calling gmtime or restoring zone. */ |
374 | #if defined (HAVE_TM_ZONE) | |
ef9ff3fd | 375 | ptr = ltptr->tm_zone; |
43ff3170 | 376 | #elif defined (HAVE_TZNAME) |
ef9ff3fd | 377 | ptr = tzname[ (ltptr->tm_isdst == 1) ? 1 : 0 ]; |
43ff3170 GH |
378 | #else |
379 | ptr = ""; | |
b9525b92 | 380 | #endif |
1bbd0b84 | 381 | zname = SCM_MUST_MALLOC (strlen (ptr) + 1); |
ef9ff3fd | 382 | strcpy (zname, ptr); |
19468eff | 383 | } |
ef9ff3fd GH |
384 | /* the struct is copied in case localtime and gmtime share a buffer. */ |
385 | if (ltptr) | |
386 | lt = *ltptr; | |
387 | utc = gmtime (&itime); | |
388 | if (utc == NULL) | |
389 | err = errno; | |
1bbd0b84 | 390 | restorezone (zone, oldenv, FUNC_NAME); |
b9525b92 | 391 | /* delayed until zone has been restored. */ |
19468eff | 392 | errno = err; |
4edc089c | 393 | if (utc == NULL || ltptr == NULL) |
1bbd0b84 | 394 | SCM_SYSERROR; |
19468eff GH |
395 | |
396 | /* calculate timezone offset in seconds west of UTC. */ | |
4edc089c GH |
397 | zoff = (utc->tm_hour - lt.tm_hour) * 3600 + (utc->tm_min - lt.tm_min) * 60 |
398 | + utc->tm_sec - lt.tm_sec; | |
399 | if (utc->tm_year < lt.tm_year) | |
19468eff | 400 | zoff -= 24 * 60 * 60; |
4edc089c | 401 | else if (utc->tm_year > lt.tm_year) |
19468eff | 402 | zoff += 24 * 60 * 60; |
4edc089c | 403 | else if (utc->tm_yday < lt.tm_yday) |
19468eff | 404 | zoff -= 24 * 60 * 60; |
4edc089c | 405 | else if (utc->tm_yday > lt.tm_yday) |
19468eff | 406 | zoff += 24 * 60 * 60; |
1c299a6b | 407 | |
4edc089c | 408 | result = filltime (<, zoff, zname); |
19468eff | 409 | SCM_ALLOW_INTS; |
ef9ff3fd | 410 | scm_must_free (zname); |
19468eff GH |
411 | return result; |
412 | } | |
1bbd0b84 | 413 | #undef FUNC_NAME |
19468eff | 414 | |
1c299a6b | 415 | SCM_DEFINE (scm_gmtime, "gmtime", 1, 0, 0, |
1bbd0b84 | 416 | (SCM time), |
1e6808ea MG |
417 | "Return an object representing the broken down components of\n" |
418 | "@var{time}, an integer like the one returned by\n" | |
419 | "@code{current-time}. The values are calculated for UTC.") | |
1bbd0b84 | 420 | #define FUNC_NAME s_scm_gmtime |
19468eff GH |
421 | { |
422 | timet itime; | |
423 | struct tm *bd_time; | |
424 | SCM result; | |
425 | ||
1bbd0b84 | 426 | itime = SCM_NUM2LONG (1,time); |
19468eff GH |
427 | SCM_DEFER_INTS; |
428 | bd_time = gmtime (&itime); | |
429 | if (bd_time == NULL) | |
1bbd0b84 | 430 | SCM_SYSERROR; |
19468eff GH |
431 | result = filltime (bd_time, 0, "GMT"); |
432 | SCM_ALLOW_INTS; | |
433 | return result; | |
434 | } | |
1bbd0b84 | 435 | #undef FUNC_NAME |
19468eff | 436 | |
b9525b92 GH |
437 | /* copy time components from a Scheme object to a struct tm. */ |
438 | static void | |
3eeba8d4 | 439 | bdtime2c (SCM sbd_time, struct tm *lt, int pos, const char *subr) |
19468eff | 440 | { |
55a7fc62 GH |
441 | SCM *velts; |
442 | int i; | |
443 | ||
0c95b57d | 444 | SCM_ASSERT (SCM_VECTORP (sbd_time) |
9fd38a3d | 445 | && SCM_VECTOR_LENGTH (sbd_time) == 11, |
b9525b92 | 446 | sbd_time, pos, subr); |
55a7fc62 GH |
447 | velts = SCM_VELTS (sbd_time); |
448 | for (i = 0; i < 10; i++) | |
449 | { | |
450 | SCM_ASSERT (SCM_INUMP (velts[i]), sbd_time, pos, subr); | |
451 | } | |
0c95b57d | 452 | SCM_ASSERT (SCM_FALSEP (velts[10]) || SCM_STRINGP (velts[10]), |
55a7fc62 GH |
453 | sbd_time, pos, subr); |
454 | ||
455 | lt->tm_sec = SCM_INUM (velts[0]); | |
456 | lt->tm_min = SCM_INUM (velts[1]); | |
457 | lt->tm_hour = SCM_INUM (velts[2]); | |
458 | lt->tm_mday = SCM_INUM (velts[3]); | |
459 | lt->tm_mon = SCM_INUM (velts[4]); | |
460 | lt->tm_year = SCM_INUM (velts[5]); | |
461 | lt->tm_wday = SCM_INUM (velts[6]); | |
462 | lt->tm_yday = SCM_INUM (velts[7]); | |
463 | lt->tm_isdst = SCM_INUM (velts[8]); | |
c7abe4f3 | 464 | #ifdef HAVE_TM_ZONE |
55a7fc62 GH |
465 | lt->tm_gmtoff = SCM_INUM (velts[9]); |
466 | if (SCM_FALSEP (velts[10])) | |
467 | lt->tm_zone = NULL; | |
468 | else | |
a002f1a2 | 469 | lt->tm_zone = SCM_STRING_CHARS (velts[10]); |
c7abe4f3 | 470 | #endif |
b9525b92 GH |
471 | } |
472 | ||
1c299a6b | 473 | SCM_DEFINE (scm_mktime, "mktime", 1, 1, 0, |
1bbd0b84 | 474 | (SCM sbd_time, SCM zone), |
a3c8b9fc MD |
475 | "@var{bd-time} is an object representing broken down time and @code{zone}\n" |
476 | "is an optional time zone specifier (otherwise the TZ environment variable\n" | |
477 | "or the system default is used).\n\n" | |
a8eac221 | 478 | "Returns a pair: the car is a corresponding\n" |
a3c8b9fc | 479 | "integer time value like that returned\n" |
a8eac221 | 480 | "by @code{current-time}; the cdr is a broken down time object, similar to\n" |
a3c8b9fc | 481 | "as @var{bd-time} but with normalized values.") |
1bbd0b84 | 482 | #define FUNC_NAME s_scm_mktime |
b9525b92 GH |
483 | { |
484 | timet itime; | |
485 | struct tm lt, *utc; | |
486 | SCM result; | |
487 | int zoff; | |
488 | char *zname = 0; | |
ef9ff3fd | 489 | char **oldenv; |
b9525b92 GH |
490 | int err; |
491 | ||
1bbd0b84 | 492 | bdtime2c (sbd_time, <, SCM_ARG1, FUNC_NAME); |
19468eff GH |
493 | |
494 | SCM_DEFER_INTS; | |
1bbd0b84 | 495 | oldenv = setzone (zone, SCM_ARG2, FUNC_NAME); |
38c1d3c4 GH |
496 | #ifdef LOCALTIME_CACHE |
497 | tzset (); | |
498 | #endif | |
19468eff | 499 | itime = mktime (<); |
b9525b92 | 500 | err = errno; |
19468eff | 501 | |
b9525b92 GH |
502 | if (itime != -1) |
503 | { | |
4d3bacdd | 504 | const char *ptr; |
ef9ff3fd | 505 | |
43ff3170 GH |
506 | /* copy zone name before calling gmtime or restoring the zone. */ |
507 | #if defined (HAVE_TM_ZONE) | |
ef9ff3fd | 508 | ptr = lt.tm_zone; |
43ff3170 | 509 | #elif defined (HAVE_TZNAME) |
ef9ff3fd | 510 | ptr = tzname[ (lt.tm_isdst == 1) ? 1 : 0 ]; |
43ff3170 GH |
511 | #else |
512 | ptr = ""; | |
b9525b92 | 513 | #endif |
1bbd0b84 | 514 | zname = SCM_MUST_MALLOC (strlen (ptr) + 1); |
ef9ff3fd | 515 | strcpy (zname, ptr); |
b9525b92 | 516 | } |
ef9ff3fd GH |
517 | |
518 | /* get timezone offset in seconds west of UTC. */ | |
519 | utc = gmtime (&itime); | |
520 | if (utc == NULL) | |
521 | err = errno; | |
522 | ||
1bbd0b84 | 523 | restorezone (zone, oldenv, FUNC_NAME); |
b9525b92 GH |
524 | /* delayed until zone has been restored. */ |
525 | errno = err; | |
526 | if (utc == NULL || itime == -1) | |
1bbd0b84 | 527 | SCM_SYSERROR; |
b9525b92 | 528 | |
19468eff GH |
529 | zoff = (utc->tm_hour - lt.tm_hour) * 3600 + (utc->tm_min - lt.tm_min) * 60 |
530 | + utc->tm_sec - lt.tm_sec; | |
531 | if (utc->tm_year < lt.tm_year) | |
532 | zoff -= 24 * 60 * 60; | |
533 | else if (utc->tm_year > lt.tm_year) | |
534 | zoff += 24 * 60 * 60; | |
535 | else if (utc->tm_yday < lt.tm_yday) | |
536 | zoff -= 24 * 60 * 60; | |
537 | else if (utc->tm_yday > lt.tm_yday) | |
538 | zoff += 24 * 60 * 60; | |
539 | ||
19468eff GH |
540 | result = scm_cons (scm_long2num ((long) itime), |
541 | filltime (<, zoff, zname)); | |
542 | SCM_ALLOW_INTS; | |
ef9ff3fd | 543 | scm_must_free (zname); |
19468eff | 544 | return result; |
0f2d19dd | 545 | } |
1bbd0b84 | 546 | #undef FUNC_NAME |
0f2d19dd | 547 | |
38c1d3c4 | 548 | #ifdef HAVE_TZSET |
1c299a6b | 549 | SCM_DEFINE (scm_tzset, "tzset", 0, 0, 0, |
1bbd0b84 | 550 | (void), |
a3c8b9fc MD |
551 | "Initialize the timezone from the TZ environment variable\n" |
552 | "or the system default. It's not usually necessary to call this procedure\n" | |
553 | "since it's done automatically by other procedures that depend on the\n" | |
554 | "timezone.") | |
1bbd0b84 | 555 | #define FUNC_NAME s_scm_tzset |
0f2d19dd | 556 | { |
19468eff GH |
557 | tzset(); |
558 | return SCM_UNSPECIFIED; | |
0f2d19dd | 559 | } |
1bbd0b84 | 560 | #undef FUNC_NAME |
38c1d3c4 | 561 | #endif /* HAVE_TZSET */ |
0f2d19dd | 562 | |
a1ec6916 | 563 | SCM_DEFINE (scm_strftime, "strftime", 2, 0, 0, |
1bbd0b84 | 564 | (SCM format, SCM stime), |
a3c8b9fc MD |
565 | "Formats a time specification @var{time} using @var{template}. @var{time}\n" |
566 | "is an object with time components in the form returned by @code{localtime}\n" | |
567 | "or @code{gmtime}. @var{template} is a string which can include formatting\n" | |
568 | "specifications introduced by a @code{%} character. The formatting of\n" | |
569 | "month and day names is dependent on the current locale. The value returned\n" | |
570 | "is the formatted string.\n" | |
571 | "@xref{Formatting Date and Time, , , libc, The GNU C Library Reference Manual}.)") | |
1bbd0b84 | 572 | #define FUNC_NAME s_scm_strftime |
b9525b92 GH |
573 | { |
574 | struct tm t; | |
575 | ||
576 | char *tbuf; | |
577 | int size = 50; | |
a15e6dcc | 578 | char *fmt, *myfmt; |
b9525b92 | 579 | int len; |
ef9ff3fd | 580 | SCM result; |
b9525b92 | 581 | |
a6d9e5ab | 582 | SCM_VALIDATE_STRING (1, format); |
1bbd0b84 | 583 | bdtime2c (stime, &t, SCM_ARG2, FUNC_NAME); |
b9525b92 | 584 | |
a6d9e5ab DH |
585 | SCM_STRING_COERCE_0TERMINATION_X (format); |
586 | fmt = SCM_STRING_CHARS (format); | |
587 | len = SCM_STRING_LENGTH (format); | |
b9525b92 | 588 | |
a15e6dcc MV |
589 | /* Ugly hack: strftime can return 0 if its buffer is too small, |
590 | but some valid time strings (e.g. "%p") can sometimes produce | |
591 | a zero-byte output string! Workaround is to prepend a junk | |
592 | character to the format string, so that valid returns are always | |
593 | nonzero. */ | |
594 | myfmt = SCM_MUST_MALLOC (len+2); | |
595 | *myfmt = 'x'; | |
596 | strncpy(myfmt+1, fmt, len); | |
597 | myfmt[len+1] = 0; | |
598 | ||
1bbd0b84 | 599 | tbuf = SCM_MUST_MALLOC (size); |
b8a1b29b GH |
600 | { |
601 | #if !defined (HAVE_TM_ZONE) | |
602 | /* it seems the only way to tell non-GNU versions of strftime what | |
603 | zone to use (for the %Z format) is to set TZ in the | |
604 | environment. interrupts and thread switching must be deferred | |
605 | until TZ is restored. */ | |
606 | char **oldenv = NULL; | |
607 | SCM *velts = SCM_VELTS (stime); | |
e652b54f | 608 | int have_zone = 0; |
b8a1b29b | 609 | |
a002f1a2 | 610 | if (SCM_NFALSEP (velts[10]) && *SCM_STRING_CHARS (velts[10]) != 0) |
b8a1b29b GH |
611 | { |
612 | /* it's not required that the TZ setting be correct, just that | |
e652b54f GH |
613 | it has the right name. so try something like TZ=EST0. |
614 | using only TZ=EST would be simpler but it doesn't work on | |
615 | some OSs, e.g., Solaris. */ | |
616 | SCM zone = | |
617 | scm_string_append (scm_cons (velts[10], | |
618 | scm_cons (scm_makfrom0str ("0"), | |
619 | SCM_EOL))); | |
1c299a6b | 620 | |
e652b54f | 621 | have_zone = 1; |
b8a1b29b | 622 | SCM_DEFER_INTS; |
e652b54f | 623 | oldenv = setzone (zone, SCM_ARG2, FUNC_NAME); |
b8a1b29b GH |
624 | } |
625 | #endif | |
626 | ||
38c1d3c4 | 627 | #ifdef LOCALTIME_CACHE |
b8a1b29b GH |
628 | tzset (); |
629 | #endif | |
630 | ||
a15e6dcc MV |
631 | /* POSIX says strftime returns 0 on buffer overrun, but old |
632 | systems (i.e. libc 4 on GNU/Linux) might return `size' in that | |
633 | case. */ | |
634 | while ((len = strftime (tbuf, size, myfmt, &t)) == 0 || len == size) | |
b8a1b29b GH |
635 | { |
636 | scm_must_free (tbuf); | |
637 | size *= 2; | |
638 | tbuf = SCM_MUST_MALLOC (size); | |
639 | } | |
640 | ||
641 | #if !defined (HAVE_TM_ZONE) | |
e652b54f | 642 | if (have_zone) |
b8a1b29b GH |
643 | { |
644 | restorezone (velts[10], oldenv, FUNC_NAME); | |
645 | SCM_ALLOW_INTS; | |
646 | } | |
38c1d3c4 | 647 | #endif |
b9525b92 | 648 | } |
b8a1b29b | 649 | |
a15e6dcc | 650 | result = scm_makfromstr (tbuf+1, len-1, 0); |
ef9ff3fd | 651 | scm_must_free (tbuf); |
a15e6dcc | 652 | scm_must_free(myfmt); |
ef9ff3fd | 653 | return result; |
b9525b92 | 654 | } |
1bbd0b84 | 655 | #undef FUNC_NAME |
b9525b92 | 656 | |
f25f761d | 657 | #ifdef HAVE_STRPTIME |
a1ec6916 | 658 | SCM_DEFINE (scm_strptime, "strptime", 2, 0, 0, |
1bbd0b84 | 659 | (SCM format, SCM string), |
a8eac221 MG |
660 | "Performs the reverse action to @code{strftime}, parsing\n" |
661 | "@var{string} according to the specification supplied in\n" | |
662 | "@var{template}. The interpretation of month and day names is\n" | |
663 | "dependent on the current locale. The value returned is a pair.\n" | |
664 | "The car has an object with time components\n" | |
a3c8b9fc MD |
665 | "in the form returned by @code{localtime} or @code{gmtime},\n" |
666 | "but the time zone components\n" | |
667 | "are not usefully set.\n" | |
a8eac221 MG |
668 | "The cdr reports the number of characters from @var{string}\n" |
669 | "which were used for the conversion.") | |
1bbd0b84 | 670 | #define FUNC_NAME s_scm_strptime |
b9525b92 | 671 | { |
b9525b92 GH |
672 | struct tm t; |
673 | char *fmt, *str, *rest; | |
674 | ||
a6d9e5ab DH |
675 | SCM_VALIDATE_STRING (1, format); |
676 | SCM_VALIDATE_STRING (2, string); | |
b9525b92 | 677 | |
a6d9e5ab DH |
678 | SCM_STRING_COERCE_0TERMINATION_X (format); |
679 | SCM_STRING_COERCE_0TERMINATION_X (string); | |
680 | fmt = SCM_STRING_CHARS (format); | |
681 | str = SCM_STRING_CHARS (string); | |
b9525b92 GH |
682 | |
683 | /* initialize the struct tm */ | |
684 | #define tm_init(field) t.field = 0 | |
685 | tm_init (tm_sec); | |
686 | tm_init (tm_min); | |
687 | tm_init (tm_hour); | |
688 | tm_init (tm_mday); | |
689 | tm_init (tm_mon); | |
690 | tm_init (tm_year); | |
691 | tm_init (tm_wday); | |
692 | tm_init (tm_yday); | |
693 | #undef tm_init | |
694 | ||
695 | t.tm_isdst = -1; | |
696 | SCM_DEFER_INTS; | |
697 | if ((rest = strptime (str, fmt, &t)) == NULL) | |
1bbd0b84 | 698 | SCM_SYSERROR; |
b9525b92 GH |
699 | |
700 | SCM_ALLOW_INTS; | |
701 | return scm_cons (filltime (&t, 0, NULL), SCM_MAKINUM (rest - str)); | |
b9525b92 | 702 | } |
1bbd0b84 | 703 | #undef FUNC_NAME |
f25f761d | 704 | #endif /* HAVE_STRPTIME */ |
b9525b92 | 705 | |
0f2d19dd JB |
706 | void |
707 | scm_init_stime() | |
0f2d19dd JB |
708 | { |
709 | scm_sysintern("internal-time-units-per-second", | |
19468eff | 710 | scm_long2num((long)CLKTCK)); |
0f2d19dd JB |
711 | |
712 | #ifdef HAVE_FTIME | |
713 | if (!scm_your_base.time) ftime(&scm_your_base); | |
714 | #else | |
715 | if (!scm_your_base) time(&scm_your_base); | |
716 | #endif | |
717 | ||
718 | if (!scm_my_base) scm_my_base = mytime(); | |
719 | ||
876c87ce | 720 | scm_add_feature ("current-time"); |
8dc9439f | 721 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 722 | #include "libguile/stime.x" |
8dc9439f | 723 | #endif |
0f2d19dd JB |
724 | } |
725 | ||
89e00824 ML |
726 | |
727 | /* | |
728 | Local Variables: | |
729 | c-file-style: "gnu" | |
730 | End: | |
731 | */ |