Update README on using libraries in non-standard locations
[bpt/guile.git] / libguile / stime.c
CommitLineData
417566eb 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
1c299a6b 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
1c299a6b 7 *
73be1d9e 8 * This library is distributed in the hope that it will be useful,
0f2d19dd 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
1c299a6b 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd
JB
19\f
20
464ee095
KR
21/* _POSIX_C_SOURCE is not defined always, because it causes problems on some
22 systems, notably
23
24 - FreeBSD loses all BSD and XOPEN defines.
25 - glibc loses some things like CLK_TCK.
26 - On MINGW it conflicts with the pthread headers.
27
28 But on HP-UX _POSIX_C_SOURCE is needed, as noted, for gmtime_r.
29
30 Perhaps a configure test could figure out what _POSIX_C_SOURCE gives and
31 what it takes away, and decide from that whether to use it, instead of
32 hard coding __hpux. */
33
2baec6a9
LC
34#ifndef _REENTRANT
35# define _REENTRANT /* ask solaris for gmtime_r prototype */
36#endif
464ee095 37#ifdef __hpux
95f31774 38#define _POSIX_C_SOURCE 199506L /* for gmtime_r prototype */
edea856c 39#endif
3c61e80f 40
dbb605f5 41#ifdef HAVE_CONFIG_H
3cf2f52d
RB
42# include <config.h>
43#endif
44
0f2d19dd 45#include <stdio.h>
e6e2e95a 46#include <errno.h>
69f23174 47#include <strftime.h>
e6e2e95a 48
a0599745 49#include "libguile/_scm.h"
4e047c3e 50#include "libguile/async.h"
a0599745
MD
51#include "libguile/feature.h"
52#include "libguile/strings.h"
53#include "libguile/vectors.h"
cc95e00a 54#include "libguile/dynwind.h"
20e6290e 55
a0599745
MD
56#include "libguile/validate.h"
57#include "libguile/stime.h"
20e6290e 58
0f2d19dd
JB
59#ifdef HAVE_UNISTD_H
60#include <unistd.h>
61#endif
62
63\f
64# ifdef HAVE_SYS_TYPES_H
65# include <sys/types.h>
66# endif
67
a15e6dcc
MV
68#ifdef HAVE_STRING_H
69#include <string.h>
70#endif
71
b1978258
GH
72#ifdef HAVE_SYS_TIMES_H
73# include <sys/times.h>
74#endif
75
76#ifdef HAVE_SYS_TIMEB_H
77# include <sys/timeb.h>
78#endif
0f2d19dd 79
b9525b92
GH
80#ifndef tzname /* For SGI. */
81extern char *tzname[]; /* RS6000 and others reject char **tzname. */
82#endif
79dcdf51
MV
83#if defined (__MINGW32__)
84# define tzname _tzname
85#endif
b9525b92 86
3c61e80f 87#if ! HAVE_DECL_STRPTIME
4d3bacdd
JB
88extern char *strptime ();
89#endif
90
0f2d19dd
JB
91#ifdef __STDC__
92# define timet time_t
93#else
94# define timet long
95#endif
96
eb7e1603 97
0f2d19dd 98#ifdef HAVE_TIMES
0f2d19dd 99static
1c299a6b 100timet mytime()
0f2d19dd
JB
101{
102 struct tms time_buffer;
103 times(&time_buffer);
104 return time_buffer.tms_utime + time_buffer.tms_stime;
105}
106#else
107# ifdef LACK_CLOCK
756414cf 108# define mytime() ((time((timet*)0) - scm_your_base) * SCM_TIME_UNITS_PER_SECOND)
0f2d19dd
JB
109# else
110# define mytime clock
111# endif
112#endif
113
0f2d19dd 114#ifdef HAVE_FTIME
0f2d19dd 115struct timeb scm_your_base = {0};
b450f070
GB
116#else
117timet scm_your_base = 0;
118#endif
1bbd0b84 119
1c299a6b 120SCM_DEFINE (scm_get_internal_real_time, "get-internal-real-time", 0, 0, 0,
1bbd0b84 121 (),
1e6808ea
MG
122 "Return the number of time units since the interpreter was\n"
123 "started.")
1bbd0b84 124#define FUNC_NAME s_scm_get_internal_real_time
0f2d19dd 125{
b450f070 126#ifdef HAVE_FTIME
0f2d19dd 127 struct timeb time_buffer;
55c4d089
JB
128
129 SCM tmp;
130 ftime (&time_buffer);
0f2d19dd 131 time_buffer.time -= scm_your_base.time;
b9bd8526 132 tmp = scm_from_long (time_buffer.millitm - scm_your_base.millitm);
55c4d089 133 tmp = scm_sum (tmp,
e11e83f3
MV
134 scm_product (scm_from_int (1000),
135 scm_from_int (time_buffer.time)));
b9bd8526
MV
136 return scm_quotient (scm_product (tmp,
137 scm_from_int (SCM_TIME_UNITS_PER_SECOND)),
e11e83f3 138 scm_from_int (1000));
0f2d19dd 139#else
b9bd8526
MV
140 return scm_from_long ((time((timet*)0) - scm_your_base)
141 * (int)SCM_TIME_UNITS_PER_SECOND);
b450f070 142#endif /* HAVE_FTIME */
0f2d19dd 143}
1bbd0b84
GB
144#undef FUNC_NAME
145
0f2d19dd 146
f25f761d 147#ifdef HAVE_TIMES
1c299a6b 148SCM_DEFINE (scm_times, "times", 0, 0, 0,
1bbd0b84 149 (void),
1e6808ea
MG
150 "Return an object with information about real and processor\n"
151 "time. The following procedures accept such an object as an\n"
152 "argument and return a selected component:\n"
153 "\n"
b380b885
MD
154 "@table @code\n"
155 "@item tms:clock\n"
156 "The current real time, expressed as time units relative to an\n"
157 "arbitrary base.\n"
158 "@item tms:utime\n"
159 "The CPU time units used by the calling process.\n"
160 "@item tms:stime\n"
1e6808ea
MG
161 "The CPU time units used by the system on behalf of the calling\n"
162 "process.\n"
b380b885 163 "@item tms:cutime\n"
1e6808ea
MG
164 "The CPU time units used by terminated child processes of the\n"
165 "calling process, whose status has been collected (e.g., using\n"
166 "@code{waitpid}).\n"
b380b885 167 "@item tms:cstime\n"
1e6808ea 168 "Similarly, the CPU times units used by the system on behalf of\n"
b380b885
MD
169 "terminated child processes.\n"
170 "@end table")
1bbd0b84 171#define FUNC_NAME s_scm_times
6afcd3b2 172{
6afcd3b2
GH
173 struct tms t;
174 clock_t rv;
175
00ffa0e7 176 SCM result = scm_c_make_vector (5, SCM_UNDEFINED);
6afcd3b2
GH
177 rv = times (&t);
178 if (rv == -1)
1bbd0b84 179 SCM_SYSERROR;
4057a3e0
MV
180 SCM_SIMPLE_VECTOR_SET (result, 0, scm_from_long (rv));
181 SCM_SIMPLE_VECTOR_SET (result, 1, scm_from_long (t.tms_utime));
182 SCM_SIMPLE_VECTOR_SET (result, 2, scm_from_long (t.tms_stime));
183 SCM_SIMPLE_VECTOR_SET (result ,3, scm_from_long (t.tms_cutime));
184 SCM_SIMPLE_VECTOR_SET (result, 4, scm_from_long (t.tms_cstime));
6afcd3b2 185 return result;
6afcd3b2 186}
1bbd0b84 187#undef FUNC_NAME
f25f761d 188#endif /* HAVE_TIMES */
6afcd3b2 189
0f2d19dd
JB
190static long scm_my_base = 0;
191
1c299a6b
ML
192long
193scm_c_get_internal_run_time ()
194{
195 return mytime () - scm_my_base;
196}
197
198SCM_DEFINE (scm_get_internal_run_time, "get-internal-run-time", 0, 0, 0,
1bbd0b84 199 (void),
1e6808ea
MG
200 "Return the number of time units of processor time used by the\n"
201 "interpreter. Both @emph{system} and @emph{user} time are\n"
202 "included but subprocesses are not.")
1bbd0b84 203#define FUNC_NAME s_scm_get_internal_run_time
0f2d19dd 204{
b9bd8526 205 return scm_from_long (scm_c_get_internal_run_time ());
0f2d19dd 206}
1bbd0b84 207#undef FUNC_NAME
0f2d19dd 208
f40771d8
KR
209/* For reference, note that current-time and gettimeofday both should be
210 protected against setzone/restorezone changes in another thread, since on
211 DOS the system time is normally kept as local time, which means TZ
212 affects the return from current-time and gettimeofday. Not sure if DJGPP
213 etc actually has concurrent multi-threading, but it seems prudent not to
214 make assumptions about this. */
215
1c299a6b 216SCM_DEFINE (scm_current_time, "current-time", 0, 0, 0,
1bbd0b84 217 (void),
1e6808ea
MG
218 "Return the number of seconds since 1970-01-01 00:00:00 UTC,\n"
219 "excluding leap seconds.")
1bbd0b84 220#define FUNC_NAME s_scm_current_time
0f2d19dd 221{
19468eff
GH
222 timet timv;
223
9de87eea 224 SCM_CRITICAL_SECTION_START;
763313a2 225 timv = time (NULL);
9de87eea 226 SCM_CRITICAL_SECTION_END;
763313a2
KR
227 if (timv == -1)
228 SCM_MISC_ERROR ("current time not available", SCM_EOL);
b9bd8526 229 return scm_from_long (timv);
19468eff 230}
1bbd0b84 231#undef FUNC_NAME
19468eff 232
1c299a6b 233SCM_DEFINE (scm_gettimeofday, "gettimeofday", 0, 0, 0,
1bbd0b84 234 (void),
1e6808ea
MG
235 "Return a pair containing the number of seconds and microseconds\n"
236 "since 1970-01-01 00:00:00 UTC, excluding leap seconds. Note:\n"
237 "whether true microsecond resolution is available depends on the\n"
238 "operating system.")
1bbd0b84 239#define FUNC_NAME s_scm_gettimeofday
19468eff
GH
240{
241#ifdef HAVE_GETTIMEOFDAY
242 struct timeval time;
763313a2 243 int ret, err;
19468eff 244
9de87eea 245 SCM_CRITICAL_SECTION_START;
763313a2
KR
246 ret = gettimeofday (&time, NULL);
247 err = errno;
9de87eea 248 SCM_CRITICAL_SECTION_END;
763313a2
KR
249 if (ret == -1)
250 {
251 errno = err;
252 SCM_SYSERROR;
253 }
b9bd8526
MV
254 return scm_cons (scm_from_long (time.tv_sec),
255 scm_from_long (time.tv_usec));
19468eff
GH
256#else
257# ifdef HAVE_FTIME
258 struct timeb time;
259
260 ftime(&time);
b9bd8526 261 return scm_cons (scm_from_long (time.time),
e11e83f3 262 scm_from_int (time.millitm * 1000));
19468eff
GH
263# else
264 timet timv;
763313a2 265 int err;
1c299a6b 266
9de87eea 267 SCM_CRITICAL_SECTION_START;
763313a2
KR
268 timv = time (NULL);
269 err = errno;
9de87eea 270 SCM_CRITICAL_SECTION_END;
763313a2
KR
271 if (timv == -1)
272 {
273 errno = err;
274 SCM_SYSERROR;
275 }
b9bd8526 276 return scm_cons (scm_from_long (timv), scm_from_int (0));
19468eff
GH
277# endif
278#endif
279}
1bbd0b84 280#undef FUNC_NAME
19468eff
GH
281
282static SCM
d6d9e957 283filltime (struct tm *bd_time, int zoff, const char *zname)
19468eff 284{
00ffa0e7 285 SCM result = scm_c_make_vector (11, SCM_UNDEFINED);
19468eff 286
4057a3e0
MV
287 SCM_SIMPLE_VECTOR_SET (result,0, scm_from_int (bd_time->tm_sec));
288 SCM_SIMPLE_VECTOR_SET (result,1, scm_from_int (bd_time->tm_min));
289 SCM_SIMPLE_VECTOR_SET (result,2, scm_from_int (bd_time->tm_hour));
290 SCM_SIMPLE_VECTOR_SET (result,3, scm_from_int (bd_time->tm_mday));
291 SCM_SIMPLE_VECTOR_SET (result,4, scm_from_int (bd_time->tm_mon));
292 SCM_SIMPLE_VECTOR_SET (result,5, scm_from_int (bd_time->tm_year));
293 SCM_SIMPLE_VECTOR_SET (result,6, scm_from_int (bd_time->tm_wday));
294 SCM_SIMPLE_VECTOR_SET (result,7, scm_from_int (bd_time->tm_yday));
295 SCM_SIMPLE_VECTOR_SET (result,8, scm_from_int (bd_time->tm_isdst));
296 SCM_SIMPLE_VECTOR_SET (result,9, scm_from_int (zoff));
297 SCM_SIMPLE_VECTOR_SET (result,10, (zname
298 ? scm_from_locale_string (zname)
299 : SCM_BOOL_F));
19468eff
GH
300 return result;
301}
302
ef9ff3fd 303static char tzvar[3] = "TZ";
ef9ff3fd 304
38c1d3c4
GH
305/* if zone is set, create a temporary environment with only a TZ
306 string. other threads or interrupt handlers shouldn't be allowed
307 to run until the corresponding restorezone is called. hence the use
308 of a static variable for tmpenv is no big deal. */
ef9ff3fd 309static char **
3eeba8d4 310setzone (SCM zone, int pos, const char *subr)
b9525b92 311{
ef9ff3fd 312 char **oldenv = 0;
b9525b92
GH
313
314 if (!SCM_UNBNDP (zone))
315 {
ef9ff3fd 316 static char *tmpenv[2];
b9525b92 317 char *buf;
7f9994d9
MV
318 size_t zone_len;
319
320 zone_len = scm_to_locale_stringbuf (zone, NULL, 0);
321 buf = scm_malloc (zone_len + sizeof (tzvar) + 1);
322 strcpy (buf, tzvar);
323 buf[sizeof(tzvar)-1] = '=';
324 scm_to_locale_stringbuf (zone, buf+sizeof(tzvar), zone_len);
325 buf[sizeof(tzvar)+zone_len] = '\0';
ef9ff3fd
GH
326 oldenv = environ;
327 tmpenv[0] = buf;
328 tmpenv[1] = 0;
329 environ = tmpenv;
b9525b92 330 }
ef9ff3fd 331 return oldenv;
b9525b92
GH
332}
333
334static void
e81d98ec 335restorezone (SCM zone, char **oldenv, const char *subr SCM_UNUSED)
b9525b92
GH
336{
337 if (!SCM_UNBNDP (zone))
338 {
4c9419ac 339 free (environ[0]);
ef9ff3fd 340 environ = oldenv;
38c1d3c4
GH
341#ifdef HAVE_TZSET
342 /* for the possible benefit of user code linked with libguile. */
b9525b92 343 tzset();
38c1d3c4 344#endif
b9525b92
GH
345 }
346}
347
1c299a6b 348SCM_DEFINE (scm_localtime, "localtime", 1, 1, 0,
1bbd0b84 349 (SCM time, SCM zone),
1e6808ea
MG
350 "Return an object representing the broken down components of\n"
351 "@var{time}, an integer like the one returned by\n"
352 "@code{current-time}. The time zone for the calculation is\n"
353 "optionally specified by @var{zone} (a string), otherwise the\n"
354 "@code{TZ} environment variable or the system default is used.")
1bbd0b84 355#define FUNC_NAME s_scm_localtime
19468eff
GH
356{
357 timet itime;
4edc089c 358 struct tm *ltptr, lt, *utc;
19468eff
GH
359 SCM result;
360 int zoff;
361 char *zname = 0;
ef9ff3fd 362 char **oldenv;
19468eff
GH
363 int err;
364
e4b265d8 365 itime = SCM_NUM2LONG (1, time);
38c1d3c4
GH
366
367 /* deferring interupts is essential since a) setzone may install a temporary
368 environment b) localtime uses a static buffer. */
9de87eea 369 SCM_CRITICAL_SECTION_START;
1bbd0b84 370 oldenv = setzone (zone, SCM_ARG2, FUNC_NAME);
38c1d3c4
GH
371#ifdef LOCALTIME_CACHE
372 tzset ();
373#endif
73ae3b4c
KR
374 /* POSIX says localtime sets errno, but C99 doesn't say that.
375 Give a sensible default value in case localtime doesn't set it. */
376 errno = EINVAL;
4edc089c 377 ltptr = localtime (&itime);
19468eff 378 err = errno;
4edc089c 379 if (ltptr)
19468eff 380 {
4d3bacdd 381 const char *ptr;
ef9ff3fd 382
43ff3170
GH
383 /* copy zone name before calling gmtime or restoring zone. */
384#if defined (HAVE_TM_ZONE)
ef9ff3fd 385 ptr = ltptr->tm_zone;
43ff3170 386#elif defined (HAVE_TZNAME)
ef9ff3fd 387 ptr = tzname[ (ltptr->tm_isdst == 1) ? 1 : 0 ];
43ff3170
GH
388#else
389 ptr = "";
b9525b92 390#endif
4c9419ac 391 zname = scm_malloc (strlen (ptr) + 1);
ef9ff3fd 392 strcpy (zname, ptr);
19468eff 393 }
ef9ff3fd
GH
394 /* the struct is copied in case localtime and gmtime share a buffer. */
395 if (ltptr)
396 lt = *ltptr;
73ae3b4c
KR
397 /* POSIX says gmtime sets errno, but C99 doesn't say that.
398 Give a sensible default value in case gmtime doesn't set it. */
399 errno = EINVAL;
ef9ff3fd
GH
400 utc = gmtime (&itime);
401 if (utc == NULL)
402 err = errno;
1bbd0b84 403 restorezone (zone, oldenv, FUNC_NAME);
b9525b92 404 /* delayed until zone has been restored. */
19468eff 405 errno = err;
4edc089c 406 if (utc == NULL || ltptr == NULL)
1bbd0b84 407 SCM_SYSERROR;
19468eff
GH
408
409 /* calculate timezone offset in seconds west of UTC. */
4edc089c
GH
410 zoff = (utc->tm_hour - lt.tm_hour) * 3600 + (utc->tm_min - lt.tm_min) * 60
411 + utc->tm_sec - lt.tm_sec;
412 if (utc->tm_year < lt.tm_year)
19468eff 413 zoff -= 24 * 60 * 60;
4edc089c 414 else if (utc->tm_year > lt.tm_year)
19468eff 415 zoff += 24 * 60 * 60;
4edc089c 416 else if (utc->tm_yday < lt.tm_yday)
19468eff 417 zoff -= 24 * 60 * 60;
4edc089c 418 else if (utc->tm_yday > lt.tm_yday)
19468eff 419 zoff += 24 * 60 * 60;
1c299a6b 420
4edc089c 421 result = filltime (&lt, zoff, zname);
9de87eea 422 SCM_CRITICAL_SECTION_END;
4c9419ac
MV
423 if (zname)
424 free (zname);
19468eff
GH
425 return result;
426}
1bbd0b84 427#undef FUNC_NAME
19468eff 428
45198ffb
KR
429/* tm_zone is normally a pointer, not an array within struct tm, so we might
430 have to worry about the lifespan of what it points to. The posix specs
431 don't seem to say anything about this, let's assume here that tm_zone
432 will be a constant and therefore no protection or anything is needed
433 until we copy it in filltime(). */
434
1c299a6b 435SCM_DEFINE (scm_gmtime, "gmtime", 1, 0, 0,
1bbd0b84 436 (SCM time),
1e6808ea
MG
437 "Return an object representing the broken down components of\n"
438 "@var{time}, an integer like the one returned by\n"
439 "@code{current-time}. The values are calculated for UTC.")
1bbd0b84 440#define FUNC_NAME s_scm_gmtime
19468eff
GH
441{
442 timet itime;
45198ffb 443 struct tm bd_buf, *bd_time;
d6d9e957 444 const char *zname;
19468eff 445
e4b265d8 446 itime = SCM_NUM2LONG (1, time);
45198ffb 447
73ae3b4c
KR
448 /* POSIX says gmtime sets errno, but C99 doesn't say that.
449 Give a sensible default value in case gmtime doesn't set it. */
450 errno = EINVAL;
45198ffb
KR
451
452#if HAVE_GMTIME_R
453 bd_time = gmtime_r (&itime, &bd_buf);
454#else
9de87eea 455 SCM_CRITICAL_SECTION_START;
19468eff 456 bd_time = gmtime (&itime);
45198ffb
KR
457 if (bd_time != NULL)
458 bd_buf = *bd_time;
9de87eea 459 SCM_CRITICAL_SECTION_END;
45198ffb 460#endif
19468eff 461 if (bd_time == NULL)
1bbd0b84 462 SCM_SYSERROR;
45198ffb 463
d6d9e957 464#if HAVE_STRUCT_TM_TM_ZONE
45198ffb 465 zname = bd_buf.tm_zone;
d6d9e957
KR
466#else
467 zname = "GMT";
468#endif
45198ffb 469 return filltime (&bd_buf, 0, zname);
19468eff 470}
1bbd0b84 471#undef FUNC_NAME
19468eff 472
b9525b92
GH
473/* copy time components from a Scheme object to a struct tm. */
474static void
3eeba8d4 475bdtime2c (SCM sbd_time, struct tm *lt, int pos, const char *subr)
19468eff 476{
4057a3e0
MV
477 SCM_ASSERT (scm_is_simple_vector (sbd_time)
478 && SCM_SIMPLE_VECTOR_LENGTH (sbd_time) == 11,
55a7fc62
GH
479 sbd_time, pos, subr);
480
4057a3e0
MV
481 lt->tm_sec = scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 0));
482 lt->tm_min = scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 1));
483 lt->tm_hour = scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 2));
484 lt->tm_mday = scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 3));
485 lt->tm_mon = scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 4));
486 lt->tm_year = scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 5));
487 lt->tm_wday = scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 6));
488 lt->tm_yday = scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 7));
489 lt->tm_isdst = scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 8));
8ab3d8a0
KR
490#if HAVE_STRUCT_TM_TM_GMTOFF
491 lt->tm_gmtoff = - scm_to_int (SCM_SIMPLE_VECTOR_REF (sbd_time, 9));
492#endif
c7abe4f3 493#ifdef HAVE_TM_ZONE
4057a3e0 494 if (scm_is_false (SCM_SIMPLE_VECTOR_REF (sbd_time, 10)))
55a7fc62
GH
495 lt->tm_zone = NULL;
496 else
4057a3e0 497 lt->tm_zone = scm_to_locale_string (SCM_SIMPLE_VECTOR_REF (sbd_time, 10));
c7abe4f3 498#endif
b9525b92
GH
499}
500
1c299a6b 501SCM_DEFINE (scm_mktime, "mktime", 1, 1, 0,
1bbd0b84 502 (SCM sbd_time, SCM zone),
a3c8b9fc
MD
503 "@var{bd-time} is an object representing broken down time and @code{zone}\n"
504 "is an optional time zone specifier (otherwise the TZ environment variable\n"
505 "or the system default is used).\n\n"
a8eac221 506 "Returns a pair: the car is a corresponding\n"
a3c8b9fc 507 "integer time value like that returned\n"
a8eac221 508 "by @code{current-time}; the cdr is a broken down time object, similar to\n"
a3c8b9fc 509 "as @var{bd-time} but with normalized values.")
1bbd0b84 510#define FUNC_NAME s_scm_mktime
b9525b92
GH
511{
512 timet itime;
513 struct tm lt, *utc;
514 SCM result;
515 int zoff;
516 char *zname = 0;
ef9ff3fd 517 char **oldenv;
b9525b92
GH
518 int err;
519
661ae7ab 520 scm_dynwind_begin (0);
cc95e00a 521
1bbd0b84 522 bdtime2c (sbd_time, &lt, SCM_ARG1, FUNC_NAME);
edea856c 523#if HAVE_STRUCT_TM_TM_ZONE
661ae7ab 524 scm_dynwind_free ((char *)lt.tm_zone);
edea856c 525#endif
19468eff 526
661ae7ab 527 scm_dynwind_critical_section (SCM_BOOL_F);
9291479f 528
1bbd0b84 529 oldenv = setzone (zone, SCM_ARG2, FUNC_NAME);
38c1d3c4
GH
530#ifdef LOCALTIME_CACHE
531 tzset ();
532#endif
19468eff 533 itime = mktime (&lt);
73ae3b4c
KR
534 /* POSIX doesn't say mktime sets errno, and on glibc 2.3.2 for instance it
535 doesn't. Force a sensible value for our error message. */
536 err = EINVAL;
19468eff 537
b9525b92
GH
538 if (itime != -1)
539 {
4d3bacdd 540 const char *ptr;
ef9ff3fd 541
43ff3170
GH
542 /* copy zone name before calling gmtime or restoring the zone. */
543#if defined (HAVE_TM_ZONE)
ef9ff3fd 544 ptr = lt.tm_zone;
43ff3170 545#elif defined (HAVE_TZNAME)
ef9ff3fd 546 ptr = tzname[ (lt.tm_isdst == 1) ? 1 : 0 ];
43ff3170
GH
547#else
548 ptr = "";
b9525b92 549#endif
4c9419ac 550 zname = scm_malloc (strlen (ptr) + 1);
ef9ff3fd 551 strcpy (zname, ptr);
b9525b92 552 }
ef9ff3fd
GH
553
554 /* get timezone offset in seconds west of UTC. */
73ae3b4c
KR
555 /* POSIX says gmtime sets errno, but C99 doesn't say that.
556 Give a sensible default value in case gmtime doesn't set it. */
fa0198bf 557 errno = EINVAL;
ef9ff3fd
GH
558 utc = gmtime (&itime);
559 if (utc == NULL)
560 err = errno;
561
1bbd0b84 562 restorezone (zone, oldenv, FUNC_NAME);
b9525b92
GH
563 /* delayed until zone has been restored. */
564 errno = err;
565 if (utc == NULL || itime == -1)
1bbd0b84 566 SCM_SYSERROR;
b9525b92 567
19468eff
GH
568 zoff = (utc->tm_hour - lt.tm_hour) * 3600 + (utc->tm_min - lt.tm_min) * 60
569 + utc->tm_sec - lt.tm_sec;
570 if (utc->tm_year < lt.tm_year)
571 zoff -= 24 * 60 * 60;
572 else if (utc->tm_year > lt.tm_year)
573 zoff += 24 * 60 * 60;
574 else if (utc->tm_yday < lt.tm_yday)
575 zoff -= 24 * 60 * 60;
576 else if (utc->tm_yday > lt.tm_yday)
577 zoff += 24 * 60 * 60;
578
b9bd8526 579 result = scm_cons (scm_from_long (itime),
19468eff 580 filltime (&lt, zoff, zname));
4c9419ac
MV
581 if (zname)
582 free (zname);
cc95e00a 583
661ae7ab 584 scm_dynwind_end ();
19468eff 585 return result;
0f2d19dd 586}
1bbd0b84 587#undef FUNC_NAME
0f2d19dd 588
38c1d3c4 589#ifdef HAVE_TZSET
1c299a6b 590SCM_DEFINE (scm_tzset, "tzset", 0, 0, 0,
1bbd0b84 591 (void),
a3c8b9fc
MD
592 "Initialize the timezone from the TZ environment variable\n"
593 "or the system default. It's not usually necessary to call this procedure\n"
594 "since it's done automatically by other procedures that depend on the\n"
595 "timezone.")
1bbd0b84 596#define FUNC_NAME s_scm_tzset
0f2d19dd 597{
19468eff
GH
598 tzset();
599 return SCM_UNSPECIFIED;
0f2d19dd 600}
1bbd0b84 601#undef FUNC_NAME
38c1d3c4 602#endif /* HAVE_TZSET */
0f2d19dd 603
a1ec6916 604SCM_DEFINE (scm_strftime, "strftime", 2, 0, 0,
1bbd0b84 605 (SCM format, SCM stime),
5cd87f55
KR
606 "Return a string which is broken-down time structure @var{stime}\n"
607 "formatted according to the given @var{format} string.\n"
608 "\n"
609 "@var{format} contains field specifications introduced by a\n"
610 "@samp{%} character. See @ref{Formatting Calendar Time,,, libc,\n"
611 "The GNU C Library Reference Manual}, or @samp{man 3 strftime},\n"
612 "for the available formatting.\n"
613 "\n"
614 "@lisp\n"
615 "(strftime \"%c\" (localtime (current-time)))\n"
616 "@result{} \"Mon Mar 11 20:17:43 2002\"\n"
617 "@end lisp\n"
618 "\n"
619 "If @code{setlocale} has been called (@pxref{Locales}), month\n"
620 "and day names are from the current locale and in the locale\n"
621 "character set.")
1bbd0b84 622#define FUNC_NAME s_scm_strftime
b9525b92
GH
623{
624 struct tm t;
625
626 char *tbuf;
627 int size = 50;
cc95e00a
MV
628 const char *fmt;
629 char *myfmt;
b9525b92 630 int len;
ef9ff3fd 631 SCM result;
b9525b92 632
a6d9e5ab 633 SCM_VALIDATE_STRING (1, format);
1bbd0b84 634 bdtime2c (stime, &t, SCM_ARG2, FUNC_NAME);
b9525b92 635
cc95e00a
MV
636 fmt = scm_i_string_chars (format);
637 len = scm_i_string_length (format);
b9525b92 638
a15e6dcc
MV
639 /* Ugly hack: strftime can return 0 if its buffer is too small,
640 but some valid time strings (e.g. "%p") can sometimes produce
641 a zero-byte output string! Workaround is to prepend a junk
642 character to the format string, so that valid returns are always
643 nonzero. */
4c9419ac 644 myfmt = scm_malloc (len+2);
a15e6dcc
MV
645 *myfmt = 'x';
646 strncpy(myfmt+1, fmt, len);
647 myfmt[len+1] = 0;
648
4c9419ac 649 tbuf = scm_malloc (size);
b8a1b29b
GH
650 {
651#if !defined (HAVE_TM_ZONE)
652 /* it seems the only way to tell non-GNU versions of strftime what
653 zone to use (for the %Z format) is to set TZ in the
654 environment. interrupts and thread switching must be deferred
655 until TZ is restored. */
656 char **oldenv = NULL;
ecc9f40f 657 SCM zone_spec = SCM_SIMPLE_VECTOR_REF (stime, 10);
e652b54f 658 int have_zone = 0;
b8a1b29b 659
ecc9f40f 660 if (scm_is_true (zone_spec) && scm_c_string_length (zone_spec) > 0)
b8a1b29b
GH
661 {
662 /* it's not required that the TZ setting be correct, just that
e652b54f
GH
663 it has the right name. so try something like TZ=EST0.
664 using only TZ=EST would be simpler but it doesn't work on
665 some OSs, e.g., Solaris. */
666 SCM zone =
ecc9f40f
MV
667 scm_string_append (scm_list_2 (zone_spec,
668 scm_from_locale_string ("0")));
1c299a6b 669
e652b54f 670 have_zone = 1;
9de87eea 671 SCM_CRITICAL_SECTION_START;
e652b54f 672 oldenv = setzone (zone, SCM_ARG2, FUNC_NAME);
b8a1b29b
GH
673 }
674#endif
675
38c1d3c4 676#ifdef LOCALTIME_CACHE
b8a1b29b
GH
677 tzset ();
678#endif
679
69f23174
LC
680 /* Use `nstrftime ()' from Gnulib, which supports all GNU extensions
681 supported by glibc. */
682 while ((len = nstrftime (tbuf, size, myfmt, &t, 0, 0)) == 0)
b8a1b29b 683 {
4c9419ac 684 free (tbuf);
b8a1b29b 685 size *= 2;
4c9419ac 686 tbuf = scm_malloc (size);
b8a1b29b
GH
687 }
688
689#if !defined (HAVE_TM_ZONE)
e652b54f 690 if (have_zone)
b8a1b29b 691 {
ecc9f40f 692 restorezone (zone_spec, oldenv, FUNC_NAME);
9de87eea 693 SCM_CRITICAL_SECTION_END;
b8a1b29b 694 }
38c1d3c4 695#endif
b9525b92 696 }
b8a1b29b 697
cc95e00a 698 result = scm_from_locale_stringn (tbuf + 1, len - 1);
4c9419ac
MV
699 free (tbuf);
700 free (myfmt);
168e958c
KR
701#if HAVE_STRUCT_TM_TM_ZONE
702 free ((char *) t.tm_zone);
703#endif
ef9ff3fd 704 return result;
b9525b92 705}
1bbd0b84 706#undef FUNC_NAME
b9525b92 707
f25f761d 708#ifdef HAVE_STRPTIME
a1ec6916 709SCM_DEFINE (scm_strptime, "strptime", 2, 0, 0,
1bbd0b84 710 (SCM format, SCM string),
a8eac221
MG
711 "Performs the reverse action to @code{strftime}, parsing\n"
712 "@var{string} according to the specification supplied in\n"
713 "@var{template}. The interpretation of month and day names is\n"
714 "dependent on the current locale. The value returned is a pair.\n"
715 "The car has an object with time components\n"
a3c8b9fc
MD
716 "in the form returned by @code{localtime} or @code{gmtime},\n"
717 "but the time zone components\n"
718 "are not usefully set.\n"
a8eac221
MG
719 "The cdr reports the number of characters from @var{string}\n"
720 "which were used for the conversion.")
1bbd0b84 721#define FUNC_NAME s_scm_strptime
b9525b92 722{
b9525b92 723 struct tm t;
cc95e00a 724 const char *fmt, *str, *rest;
8ab3d8a0 725 long zoff;
b9525b92 726
a6d9e5ab
DH
727 SCM_VALIDATE_STRING (1, format);
728 SCM_VALIDATE_STRING (2, string);
b9525b92 729
cc95e00a
MV
730 fmt = scm_i_string_chars (format);
731 str = scm_i_string_chars (string);
b9525b92
GH
732
733 /* initialize the struct tm */
734#define tm_init(field) t.field = 0
735 tm_init (tm_sec);
736 tm_init (tm_min);
737 tm_init (tm_hour);
738 tm_init (tm_mday);
739 tm_init (tm_mon);
740 tm_init (tm_year);
741 tm_init (tm_wday);
742 tm_init (tm_yday);
8ab3d8a0
KR
743#if HAVE_STRUCT_TM_TM_GMTOFF
744 tm_init (tm_gmtoff);
745#endif
b9525b92
GH
746#undef tm_init
747
03b79aa3
KR
748 /* GNU glibc strptime() "%s" is affected by the current timezone, since it
749 reads a UTC time_t value and converts with localtime_r() to set the tm
9de87eea 750 fields, hence the use of SCM_CRITICAL_SECTION_START. */
b9525b92 751 t.tm_isdst = -1;
9de87eea 752 SCM_CRITICAL_SECTION_START;
763313a2 753 rest = strptime (str, fmt, &t);
9de87eea 754 SCM_CRITICAL_SECTION_END;
763313a2 755 if (rest == NULL)
73ae3b4c
KR
756 {
757 /* POSIX doesn't say strptime sets errno, and on glibc 2.3.2 for
758 instance it doesn't. Force a sensible value for our error
759 message. */
760 errno = EINVAL;
761 SCM_SYSERROR;
762 }
b9525b92 763
8ab3d8a0
KR
764 /* tm_gmtoff is set by GNU glibc strptime "%s", so capture it when
765 available */
766#if HAVE_STRUCT_TM_TM_GMTOFF
767 zoff = - t.tm_gmtoff; /* seconds west, not east */
768#else
769 zoff = 0;
770#endif
771
772 return scm_cons (filltime (&t, zoff, NULL),
e11e83f3 773 scm_from_signed_integer (rest - str));
b9525b92 774}
1bbd0b84 775#undef FUNC_NAME
f25f761d 776#endif /* HAVE_STRPTIME */
b9525b92 777
0f2d19dd
JB
778void
779scm_init_stime()
0f2d19dd 780{
86d31dfe 781 scm_c_define ("internal-time-units-per-second",
b9bd8526 782 scm_from_long (SCM_TIME_UNITS_PER_SECOND));
0f2d19dd
JB
783
784#ifdef HAVE_FTIME
785 if (!scm_your_base.time) ftime(&scm_your_base);
786#else
787 if (!scm_your_base) time(&scm_your_base);
788#endif
789
790 if (!scm_my_base) scm_my_base = mytime();
791
876c87ce 792 scm_add_feature ("current-time");
a0599745 793#include "libguile/stime.x"
0f2d19dd
JB
794}
795
89e00824
ML
796
797/*
798 Local Variables:
799 c-file-style: "gnu"
800 End:
801*/