Fix from Ken Raeburn <raeburn@raeburn.org>:
[bpt/guile.git] / libguile / stime.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
0f2d19dd
JB
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
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. */
0f2d19dd
JB
41\f
42
43#include <stdio.h>
44#include "_scm.h"
876c87ce 45#include "feature.h"
20e6290e
JB
46
47#include "stime.h"
48
0f2d19dd
JB
49#ifdef HAVE_UNISTD_H
50#include <unistd.h>
51#endif
52
53\f
54# ifdef HAVE_SYS_TYPES_H
55# include <sys/types.h>
56# endif
57
58# ifdef TIME_WITH_SYS_TIME
59# include <sys/time.h>
60# include <time.h>
61# else
62# ifdef HAVE_SYS_TIME_H
63# include <sys/time.h>
64# else
65# ifdef HAVE_TIME_H
66# include <time.h>
67# endif
68# endif
69# endif
70
b1978258
GH
71#ifdef HAVE_SYS_TIMES_H
72# include <sys/times.h>
73#endif
74
75#ifdef HAVE_SYS_TIMEB_H
76# include <sys/timeb.h>
77#endif
0f2d19dd 78
b9525b92
GH
79#ifndef tzname /* For SGI. */
80extern char *tzname[]; /* RS6000 and others reject char **tzname. */
81#endif
82
4d3bacdd
JB
83#ifdef MISSING_STRPTIME_DECL
84extern char *strptime ();
85#endif
86
cda55316 87/* This should be figured out by autoconf. */
a2fc27b5
JB
88#if ! defined(CLKTCK) && defined(CLK_TCK)
89# define CLKTCK CLK_TCK
90#endif
91#if ! defined(CLKTCK) && defined(CLOCKS_PER_SEC)
0f2d19dd 92# define CLKTCK CLOCKS_PER_SEC
a2fc27b5
JB
93#endif
94#if ! defined(CLKTCK)
0f2d19dd 95# define CLKTCK 60
0f2d19dd
JB
96#endif
97
a2fc27b5 98
0f2d19dd
JB
99#ifdef __STDC__
100# define timet time_t
101#else
102# define timet long
103#endif
104
105#ifdef HAVE_TIMES
0f2d19dd
JB
106static
107long mytime()
0f2d19dd
JB
108{
109 struct tms time_buffer;
110 times(&time_buffer);
111 return time_buffer.tms_utime + time_buffer.tms_stime;
112}
113#else
114# ifdef LACK_CLOCK
115# define mytime() ((time((timet*)0) - scm_your_base) * CLKTCK)
116# else
117# define mytime clock
118# endif
119#endif
120
19468eff 121extern int errno;
0f2d19dd
JB
122
123#ifdef HAVE_FTIME
124
23858ad1
MD
125extern int ftime (struct timeb *);
126
0f2d19dd
JB
127struct timeb scm_your_base = {0};
128SCM_PROC(s_get_internal_real_time, "get-internal-real-time", 0, 0, 0, scm_get_internal_real_time);
0f2d19dd
JB
129SCM
130scm_get_internal_real_time()
0f2d19dd
JB
131{
132 struct timeb time_buffer;
55c4d089
JB
133
134 SCM tmp;
135 ftime (&time_buffer);
0f2d19dd 136 time_buffer.time -= scm_your_base.time;
55c4d089
JB
137 tmp = scm_long2num (time_buffer.millitm - scm_your_base.millitm);
138 tmp = scm_sum (tmp,
139 scm_product (SCM_MAKINUM (1000),
140 SCM_MAKINUM (time_buffer.time)));
141 return scm_quotient (scm_product (tmp, SCM_MAKINUM (CLKTCK)),
142 SCM_MAKINUM (1000));
143};
0f2d19dd
JB
144
145#else
146
147timet scm_your_base = 0;
148SCM_PROC(s_get_internal_real_time, "get-internal-real-time", 0, 0, 0, scm_get_internal_real_time);
0f2d19dd
JB
149SCM
150scm_get_internal_real_time()
0f2d19dd 151{
19468eff 152 return scm_long2num((time((timet*)0) - scm_your_base) * (int)CLKTCK);
0f2d19dd
JB
153}
154#endif
155
6afcd3b2
GH
156SCM_PROC (s_times, "times", 0, 0, 0, scm_times);
157SCM
158scm_times (void)
159{
160#ifdef HAVE_TIMES
161 struct tms t;
162 clock_t rv;
163
a8741caa 164 SCM result = scm_make_vector (SCM_MAKINUM(5), SCM_UNDEFINED);
6afcd3b2
GH
165 rv = times (&t);
166 if (rv == -1)
167 scm_syserror (s_times);
168 SCM_VELTS (result)[0] = scm_long2num (rv);
169 SCM_VELTS (result)[1] = scm_long2num (t.tms_utime);
170 SCM_VELTS (result)[2] = scm_long2num (t.tms_stime);
171 SCM_VELTS (result)[3] = scm_long2num (t.tms_cutime);
172 SCM_VELTS (result)[4] = scm_long2num (t.tms_cstime);
173 return result;
174#else
175 scm_sysmissing (s_times);
176#endif
177}
178
0e958795
JB
179#ifndef HAVE_TZSET
180/* GNU-WIN32's cygwin.dll doesn't have this. */
181#define tzset()
182#endif
0f2d19dd
JB
183
184
185static long scm_my_base = 0;
186
187SCM_PROC(s_get_internal_run_time, "get-internal-run-time", 0, 0, 0, scm_get_internal_run_time);
0f2d19dd
JB
188SCM
189scm_get_internal_run_time()
0f2d19dd 190{
19468eff 191 return scm_long2num(mytime()-scm_my_base);
0f2d19dd
JB
192}
193
194SCM_PROC(s_current_time, "current-time", 0, 0, 0, scm_current_time);
0f2d19dd
JB
195SCM
196scm_current_time()
0f2d19dd 197{
19468eff
GH
198 timet timv;
199
200 SCM_DEFER_INTS;
201 if ((timv = time (0)) == -1)
202 scm_syserror (s_current_time);
203 SCM_ALLOW_INTS;
204 return scm_long2num((long) timv);
205}
206
1bf9865d 207SCM_PROC (s_gettimeofday, "gettimeofday", 0, 0, 0, scm_gettimeofday);
19468eff 208SCM
1bf9865d 209scm_gettimeofday (void)
19468eff
GH
210{
211#ifdef HAVE_GETTIMEOFDAY
212 struct timeval time;
213
214 SCM_DEFER_INTS;
215 if (gettimeofday (&time, NULL) == -1)
1bf9865d 216 scm_syserror (s_gettimeofday);
19468eff
GH
217 SCM_ALLOW_INTS;
218 return scm_cons (scm_long2num ((long) time.tv_sec),
219 scm_long2num ((long) time.tv_usec));
220#else
221# ifdef HAVE_FTIME
222 struct timeb time;
223
224 ftime(&time);
225 return scm_cons (scm_long2num ((long) time.time),
9a81afca 226 SCM_MAKINUM (time.millitm * 1000));
19468eff
GH
227# else
228 timet timv;
229
230 SCM_DEFER_INTS;
231 if ((timv = time (0)) == -1)
1bf9865d 232 scm_syserror (s_gettimeofday);
19468eff
GH
233 SCM_ALLOW_INTS;
234 return scm_cons (scm_long2num (timv), SCM_MAKINUM (0));
235# endif
236#endif
237}
238
239static SCM
240filltime (struct tm *bd_time, int zoff, char *zname)
241{
a8741caa 242 SCM result = scm_make_vector (SCM_MAKINUM(11), SCM_UNDEFINED);
19468eff
GH
243
244 SCM_VELTS (result)[0] = SCM_MAKINUM (bd_time->tm_sec);
245 SCM_VELTS (result)[1] = SCM_MAKINUM (bd_time->tm_min);
246 SCM_VELTS (result)[2] = SCM_MAKINUM (bd_time->tm_hour);
247 SCM_VELTS (result)[3] = SCM_MAKINUM (bd_time->tm_mday);
248 SCM_VELTS (result)[4] = SCM_MAKINUM (bd_time->tm_mon);
249 SCM_VELTS (result)[5] = SCM_MAKINUM (bd_time->tm_year);
250 SCM_VELTS (result)[6] = SCM_MAKINUM (bd_time->tm_wday);
251 SCM_VELTS (result)[7] = SCM_MAKINUM (bd_time->tm_yday);
252 SCM_VELTS (result)[8] = SCM_MAKINUM (bd_time->tm_isdst);
253 SCM_VELTS (result)[9] = SCM_MAKINUM (zoff);
b9525b92 254 SCM_VELTS (result)[10] = zname ? scm_makfrom0str (zname) : SCM_BOOL_F;
19468eff
GH
255 return result;
256}
257
ef9ff3fd
GH
258static char tzvar[3] = "TZ";
259extern char ** environ;
260
261static char **
3eeba8d4 262setzone (SCM zone, int pos, const char *subr)
b9525b92 263{
ef9ff3fd 264 char **oldenv = 0;
b9525b92
GH
265
266 if (!SCM_UNBNDP (zone))
267 {
ef9ff3fd 268 static char *tmpenv[2];
b9525b92
GH
269 char *buf;
270
ef9ff3fd 271 /* if zone was supplied, set the environment temporarily. */
89958ad0
JB
272 SCM_ASSERT (SCM_NIMP (zone) && SCM_ROSTRINGP (zone), zone, pos, subr);
273 SCM_COERCE_SUBSTR (zone);
ef9ff3fd
GH
274 buf = scm_must_malloc (SCM_LENGTH (zone) + sizeof (tzvar) + 1,
275 subr);
276 sprintf (buf, "%s=%s", tzvar, SCM_ROCHARS (zone));
277 oldenv = environ;
278 tmpenv[0] = buf;
279 tmpenv[1] = 0;
280 environ = tmpenv;
b9525b92
GH
281 tzset();
282 }
ef9ff3fd 283 return oldenv;
b9525b92
GH
284}
285
286static void
3eeba8d4 287restorezone (SCM zone, char **oldenv, const char *subr)
b9525b92
GH
288{
289 if (!SCM_UNBNDP (zone))
290 {
ef9ff3fd
GH
291 scm_must_free (environ[0]);
292 environ = oldenv;
b9525b92
GH
293 tzset();
294 }
295}
296
297
19468eff
GH
298SCM_PROC (s_localtime, "localtime", 1, 1, 0, scm_localtime);
299SCM
300scm_localtime (SCM time, SCM zone)
301{
302 timet itime;
4edc089c 303 struct tm *ltptr, lt, *utc;
19468eff
GH
304 SCM result;
305 int zoff;
306 char *zname = 0;
ef9ff3fd 307 char **oldenv;
19468eff
GH
308 int err;
309
310 itime = scm_num2long (time, (char *) SCM_ARG1, s_localtime);
311 SCM_DEFER_INTS;
ef9ff3fd 312 oldenv = setzone (zone, SCM_ARG2, s_localtime);
4edc089c 313 ltptr = localtime (&itime);
19468eff 314 err = errno;
4edc089c 315 if (ltptr)
19468eff 316 {
4d3bacdd 317 const char *ptr;
ef9ff3fd
GH
318
319 /* copy zone name before calling gmtime or tzset. */
b9525b92 320#ifdef HAVE_TM_ZONE
ef9ff3fd 321 ptr = ltptr->tm_zone;
b9525b92
GH
322#else
323# ifdef HAVE_TZNAME
ef9ff3fd 324 ptr = tzname[ (ltptr->tm_isdst == 1) ? 1 : 0 ];
4edc089c
GH
325# else
326 scm_misc_error (s_localtime, "Not fully implemented on this platform",
da5b3eb1 327 SCM_EOL);
4edc089c 328# endif
b9525b92 329#endif
ef9ff3fd
GH
330 zname = scm_must_malloc (strlen (ptr) + 1, s_localtime);
331 strcpy (zname, ptr);
19468eff 332 }
ef9ff3fd
GH
333 /* the struct is copied in case localtime and gmtime share a buffer. */
334 if (ltptr)
335 lt = *ltptr;
336 utc = gmtime (&itime);
337 if (utc == NULL)
338 err = errno;
339 restorezone (zone, oldenv, s_localtime);
b9525b92 340 /* delayed until zone has been restored. */
19468eff 341 errno = err;
4edc089c 342 if (utc == NULL || ltptr == NULL)
19468eff
GH
343 scm_syserror (s_localtime);
344
345 /* calculate timezone offset in seconds west of UTC. */
4edc089c
GH
346 zoff = (utc->tm_hour - lt.tm_hour) * 3600 + (utc->tm_min - lt.tm_min) * 60
347 + utc->tm_sec - lt.tm_sec;
348 if (utc->tm_year < lt.tm_year)
19468eff 349 zoff -= 24 * 60 * 60;
4edc089c 350 else if (utc->tm_year > lt.tm_year)
19468eff 351 zoff += 24 * 60 * 60;
4edc089c 352 else if (utc->tm_yday < lt.tm_yday)
19468eff 353 zoff -= 24 * 60 * 60;
4edc089c 354 else if (utc->tm_yday > lt.tm_yday)
19468eff
GH
355 zoff += 24 * 60 * 60;
356
4edc089c 357 result = filltime (&lt, zoff, zname);
19468eff 358 SCM_ALLOW_INTS;
ef9ff3fd 359 scm_must_free (zname);
19468eff
GH
360 return result;
361}
19468eff
GH
362
363SCM_PROC (s_gmtime, "gmtime", 1, 0, 0, scm_gmtime);
364SCM
365scm_gmtime (SCM time)
366{
367 timet itime;
368 struct tm *bd_time;
369 SCM result;
370
371 itime = scm_num2long (time, (char *) SCM_ARG1, s_gmtime);
372 SCM_DEFER_INTS;
373 bd_time = gmtime (&itime);
374 if (bd_time == NULL)
375 scm_syserror (s_gmtime);
376 result = filltime (bd_time, 0, "GMT");
377 SCM_ALLOW_INTS;
378 return result;
379}
380
b9525b92
GH
381/* copy time components from a Scheme object to a struct tm. */
382static void
3eeba8d4 383bdtime2c (SCM sbd_time, struct tm *lt, int pos, const char *subr)
19468eff 384{
b9525b92 385 SCM_ASSERT (SCM_NIMP (sbd_time) && SCM_VECTORP (sbd_time)
3cef7514 386 && SCM_LENGTH (sbd_time) == 11
b9525b92 387 && SCM_INUMP (SCM_VELTS (sbd_time)[0])
19468eff
GH
388 && SCM_INUMP (SCM_VELTS (sbd_time)[1])
389 && SCM_INUMP (SCM_VELTS (sbd_time)[2])
390 && SCM_INUMP (SCM_VELTS (sbd_time)[3])
391 && SCM_INUMP (SCM_VELTS (sbd_time)[4])
392 && SCM_INUMP (SCM_VELTS (sbd_time)[5])
b9525b92
GH
393 && SCM_INUMP (SCM_VELTS (sbd_time)[6])
394 && SCM_INUMP (SCM_VELTS (sbd_time)[7])
19468eff 395 && SCM_INUMP (SCM_VELTS (sbd_time)[8]),
b9525b92
GH
396 sbd_time, pos, subr);
397 lt->tm_sec = SCM_INUM (SCM_VELTS (sbd_time)[0]);
398 lt->tm_min = SCM_INUM (SCM_VELTS (sbd_time)[1]);
399 lt->tm_hour = SCM_INUM (SCM_VELTS (sbd_time)[2]);
400 lt->tm_mday = SCM_INUM (SCM_VELTS (sbd_time)[3]);
401 lt->tm_mon = SCM_INUM (SCM_VELTS (sbd_time)[4]);
402 lt->tm_year = SCM_INUM (SCM_VELTS (sbd_time)[5]);
403 lt->tm_wday = SCM_INUM (SCM_VELTS (sbd_time)[6]);
404 lt->tm_yday = SCM_INUM (SCM_VELTS (sbd_time)[7]);
405 lt->tm_isdst = SCM_INUM (SCM_VELTS (sbd_time)[8]);
406}
407
408SCM_PROC (s_mktime, "mktime", 1, 1, 0, scm_mktime);
409SCM
410scm_mktime (SCM sbd_time, SCM zone)
411{
412 timet itime;
413 struct tm lt, *utc;
414 SCM result;
415 int zoff;
416 char *zname = 0;
ef9ff3fd 417 char **oldenv;
b9525b92
GH
418 int err;
419
420 SCM_ASSERT (SCM_NIMP (sbd_time) && SCM_VECTORP (sbd_time), sbd_time,
421 SCM_ARG1, s_mktime);
422 bdtime2c (sbd_time, &lt, SCM_ARG1, s_mktime);
19468eff
GH
423
424 SCM_DEFER_INTS;
ef9ff3fd 425 oldenv = setzone (zone, SCM_ARG2, s_mktime);
19468eff 426 itime = mktime (&lt);
b9525b92 427 err = errno;
19468eff 428
b9525b92
GH
429 if (itime != -1)
430 {
4d3bacdd 431 const char *ptr;
ef9ff3fd
GH
432
433 /* copy zone name before calling gmtime or tzset. */
b9525b92 434#ifdef HAVE_TM_ZONE
ef9ff3fd 435 ptr = lt.tm_zone;
b9525b92
GH
436#else
437# ifdef HAVE_TZNAME
ef9ff3fd 438 ptr = tzname[ (lt.tm_isdst == 1) ? 1 : 0 ];
4edc089c 439# else
ef9ff3fd 440 scm_misc_error (s_mktime, "Not fully implemented on this platform",
da5b3eb1 441 SCM_EOL);
4edc089c 442# endif
b9525b92 443#endif
ef9ff3fd
GH
444 zname = scm_must_malloc (strlen (ptr) + 1, s_mktime);
445 strcpy (zname, ptr);
b9525b92 446 }
ef9ff3fd
GH
447
448 /* get timezone offset in seconds west of UTC. */
449 utc = gmtime (&itime);
450 if (utc == NULL)
451 err = errno;
452
453 restorezone (zone, oldenv, s_mktime);
b9525b92
GH
454 /* delayed until zone has been restored. */
455 errno = err;
456 if (utc == NULL || itime == -1)
457 scm_syserror (s_mktime);
458
19468eff
GH
459 zoff = (utc->tm_hour - lt.tm_hour) * 3600 + (utc->tm_min - lt.tm_min) * 60
460 + utc->tm_sec - lt.tm_sec;
461 if (utc->tm_year < lt.tm_year)
462 zoff -= 24 * 60 * 60;
463 else if (utc->tm_year > lt.tm_year)
464 zoff += 24 * 60 * 60;
465 else if (utc->tm_yday < lt.tm_yday)
466 zoff -= 24 * 60 * 60;
467 else if (utc->tm_yday > lt.tm_yday)
468 zoff += 24 * 60 * 60;
469
19468eff
GH
470 result = scm_cons (scm_long2num ((long) itime),
471 filltime (&lt, zoff, zname));
472 SCM_ALLOW_INTS;
ef9ff3fd 473 scm_must_free (zname);
19468eff 474 return result;
0f2d19dd
JB
475}
476
19468eff
GH
477SCM_PROC (s_tzset, "tzset", 0, 0, 0, scm_tzset);
478SCM
479scm_tzset (void)
0f2d19dd 480{
19468eff
GH
481 tzset();
482 return SCM_UNSPECIFIED;
0f2d19dd
JB
483}
484
b9525b92
GH
485SCM_PROC (s_strftime, "strftime", 2, 0, 0, scm_strftime);
486
487SCM
488scm_strftime (format, stime)
489 SCM format;
490 SCM stime;
491{
492 struct tm t;
493
494 char *tbuf;
495 int size = 50;
496 char *fmt;
497 int len;
ef9ff3fd 498 SCM result;
b9525b92 499
89958ad0 500 SCM_ASSERT (SCM_NIMP (format) && SCM_ROSTRINGP (format), format, SCM_ARG1,
b9525b92
GH
501 s_strftime);
502 bdtime2c (stime, &t, SCM_ARG2, s_strftime);
503
89958ad0 504 SCM_COERCE_SUBSTR (format);
b9525b92
GH
505 fmt = SCM_ROCHARS (format);
506 len = SCM_ROLENGTH (format);
507
508 tbuf = scm_must_malloc (size, s_strftime);
509 while ((len = strftime (tbuf, size, fmt, &t)) == size)
510 {
511 scm_must_free (tbuf);
512 size *= 2;
513 tbuf = scm_must_malloc (size, s_strftime);
514 }
ef9ff3fd
GH
515 result = scm_makfromstr (tbuf, len, 0);
516 scm_must_free (tbuf);
517 return result;
b9525b92
GH
518}
519
520SCM_PROC (s_strptime, "strptime", 2, 0, 0, scm_strptime);
521
522SCM
523scm_strptime (format, string)
524 SCM format;
525 SCM string;
526{
527#ifdef HAVE_STRPTIME
528 struct tm t;
529 char *fmt, *str, *rest;
530
531 SCM_ASSERT (SCM_NIMP (format) && SCM_ROSTRINGP (format), format, SCM_ARG1,
532 s_strptime);
533 SCM_ASSERT (SCM_NIMP (string) && SCM_ROSTRINGP (string), string, SCM_ARG2,
534 s_strptime);
535
89958ad0
JB
536 SCM_COERCE_SUBSTR (format);
537 SCM_COERCE_SUBSTR (string);
b9525b92
GH
538 fmt = SCM_ROCHARS (format);
539 str = SCM_ROCHARS (string);
540
541 /* initialize the struct tm */
542#define tm_init(field) t.field = 0
543 tm_init (tm_sec);
544 tm_init (tm_min);
545 tm_init (tm_hour);
546 tm_init (tm_mday);
547 tm_init (tm_mon);
548 tm_init (tm_year);
549 tm_init (tm_wday);
550 tm_init (tm_yday);
551#undef tm_init
552
553 t.tm_isdst = -1;
554 SCM_DEFER_INTS;
555 if ((rest = strptime (str, fmt, &t)) == NULL)
556 scm_syserror (s_strptime);
557
558 SCM_ALLOW_INTS;
559 return scm_cons (filltime (&t, 0, NULL), SCM_MAKINUM (rest - str));
560
561#else
562 scm_sysmissing (s_strptime);
563#endif
564}
565
0f2d19dd
JB
566void
567scm_init_stime()
0f2d19dd
JB
568{
569 scm_sysintern("internal-time-units-per-second",
19468eff 570 scm_long2num((long)CLKTCK));
0f2d19dd
JB
571
572#ifdef HAVE_FTIME
573 if (!scm_your_base.time) ftime(&scm_your_base);
574#else
575 if (!scm_your_base) time(&scm_your_base);
576#endif
577
578 if (!scm_my_base) scm_my_base = mytime();
579
876c87ce 580 scm_add_feature ("current-time");
0f2d19dd
JB
581#include "stime.x"
582}
583