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