1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003 Free Software Foundation, Inc.
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.
8 * This library 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 GNU
11 * Lesser General Public License for more details.
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
28 #include "libguile/_scm.h"
29 #include "libguile/feature.h"
30 #include "libguile/strings.h"
31 #include "libguile/vectors.h"
33 #include "libguile/validate.h"
34 #include "libguile/stime.h"
41 # ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
49 #ifdef HAVE_SYS_TIMES_H
50 # include <sys/times.h>
53 #ifdef HAVE_SYS_TIMEB_H
54 # include <sys/timeb.h>
57 #ifndef tzname /* For SGI. */
58 extern char *tzname
[]; /* RS6000 and others reject char **tzname. */
60 #if defined (__MINGW32__)
61 # define tzname _tzname
64 #ifdef MISSING_STRPTIME_DECL
65 extern char *strptime ();
78 struct tms time_buffer
;
80 return time_buffer
.tms_utime
+ time_buffer
.tms_stime
;
84 # define mytime() ((time((timet*)0) - scm_your_base) * SCM_TIME_UNITS_PER_SECOND)
91 struct timeb scm_your_base
= {0};
93 timet scm_your_base
= 0;
96 SCM_DEFINE (scm_get_internal_real_time
, "get-internal-real-time", 0, 0, 0,
98 "Return the number of time units since the interpreter was\n"
100 #define FUNC_NAME s_scm_get_internal_real_time
103 struct timeb time_buffer
;
106 ftime (&time_buffer
);
107 time_buffer
.time
-= scm_your_base
.time
;
108 tmp
= scm_long2num (time_buffer
.millitm
- scm_your_base
.millitm
);
110 scm_product (SCM_MAKINUM (1000),
111 SCM_MAKINUM (time_buffer
.time
)));
112 return scm_quotient (scm_product (tmp
, SCM_MAKINUM (SCM_TIME_UNITS_PER_SECOND
)),
115 return scm_long2num((time((timet
*)0) - scm_your_base
) * (int)SCM_TIME_UNITS_PER_SECOND
);
116 #endif /* HAVE_FTIME */
122 SCM_DEFINE (scm_times
, "times", 0, 0, 0,
124 "Return an object with information about real and processor\n"
125 "time. The following procedures accept such an object as an\n"
126 "argument and return a selected component:\n"
130 "The current real time, expressed as time units relative to an\n"
133 "The CPU time units used by the calling process.\n"
135 "The CPU time units used by the system on behalf of the calling\n"
138 "The CPU time units used by terminated child processes of the\n"
139 "calling process, whose status has been collected (e.g., using\n"
142 "Similarly, the CPU times units used by the system on behalf of\n"
143 "terminated child processes.\n"
145 #define FUNC_NAME s_scm_times
150 SCM result
= scm_c_make_vector (5, SCM_UNDEFINED
);
154 SCM_VECTOR_SET (result
, 0, scm_long2num (rv
));
155 SCM_VECTOR_SET (result
, 1, scm_long2num (t
.tms_utime
));
156 SCM_VECTOR_SET (result
, 2, scm_long2num (t
.tms_stime
));
157 SCM_VECTOR_SET (result
,3, scm_long2num (t
.tms_cutime
));
158 SCM_VECTOR_SET (result
, 4, scm_long2num (t
.tms_cstime
));
162 #endif /* HAVE_TIMES */
164 static long scm_my_base
= 0;
167 scm_c_get_internal_run_time ()
169 return mytime () - scm_my_base
;
172 SCM_DEFINE (scm_get_internal_run_time
, "get-internal-run-time", 0, 0, 0,
174 "Return the number of time units of processor time used by the\n"
175 "interpreter. Both @emph{system} and @emph{user} time are\n"
176 "included but subprocesses are not.")
177 #define FUNC_NAME s_scm_get_internal_run_time
179 return scm_long2num (scm_c_get_internal_run_time ());
183 SCM_DEFINE (scm_current_time
, "current-time", 0, 0, 0,
185 "Return the number of seconds since 1970-01-01 00:00:00 UTC,\n"
186 "excluding leap seconds.")
187 #define FUNC_NAME s_scm_current_time
192 if ((timv
= time (0)) == -1)
195 return scm_long2num((long) timv
);
199 SCM_DEFINE (scm_gettimeofday
, "gettimeofday", 0, 0, 0,
201 "Return a pair containing the number of seconds and microseconds\n"
202 "since 1970-01-01 00:00:00 UTC, excluding leap seconds. Note:\n"
203 "whether true microsecond resolution is available depends on the\n"
205 #define FUNC_NAME s_scm_gettimeofday
207 #ifdef HAVE_GETTIMEOFDAY
211 if (gettimeofday (&time
, NULL
) == -1)
214 return scm_cons (scm_long2num ((long) time
.tv_sec
),
215 scm_long2num ((long) time
.tv_usec
));
221 return scm_cons (scm_long2num ((long) time
.time
),
222 SCM_MAKINUM (time
.millitm
* 1000));
227 if ((timv
= time (0)) == -1)
230 return scm_cons (scm_long2num (timv
), SCM_MAKINUM (0));
237 filltime (struct tm
*bd_time
, int zoff
, char *zname
)
239 SCM result
= scm_c_make_vector (11, SCM_UNDEFINED
);
241 SCM_VECTOR_SET (result
,0, SCM_MAKINUM (bd_time
->tm_sec
));
242 SCM_VECTOR_SET (result
,1, SCM_MAKINUM (bd_time
->tm_min
));
243 SCM_VECTOR_SET (result
,2, SCM_MAKINUM (bd_time
->tm_hour
));
244 SCM_VECTOR_SET (result
,3, SCM_MAKINUM (bd_time
->tm_mday
));
245 SCM_VECTOR_SET (result
,4, SCM_MAKINUM (bd_time
->tm_mon
));
246 SCM_VECTOR_SET (result
,5, SCM_MAKINUM (bd_time
->tm_year
));
247 SCM_VECTOR_SET (result
,6, SCM_MAKINUM (bd_time
->tm_wday
));
248 SCM_VECTOR_SET (result
,7, SCM_MAKINUM (bd_time
->tm_yday
));
249 SCM_VECTOR_SET (result
,8, SCM_MAKINUM (bd_time
->tm_isdst
));
250 SCM_VECTOR_SET (result
,9, SCM_MAKINUM (zoff
));
251 SCM_VECTOR_SET (result
,10, zname
? scm_makfrom0str (zname
) : SCM_BOOL_F
);
255 static char tzvar
[3] = "TZ";
256 extern char ** environ
;
258 /* if zone is set, create a temporary environment with only a TZ
259 string. other threads or interrupt handlers shouldn't be allowed
260 to run until the corresponding restorezone is called. hence the use
261 of a static variable for tmpenv is no big deal. */
263 setzone (SCM zone
, int pos
, const char *subr
)
267 if (!SCM_UNBNDP (zone
))
269 static char *tmpenv
[2];
272 SCM_ASSERT (SCM_STRINGP (zone
), zone
, pos
, subr
);
273 buf
= scm_malloc (SCM_STRING_LENGTH (zone
) + sizeof (tzvar
) + 1);
274 sprintf (buf
, "%s=%s", tzvar
, SCM_STRING_CHARS (zone
));
284 restorezone (SCM zone
, char **oldenv
, const char *subr SCM_UNUSED
)
286 if (!SCM_UNBNDP (zone
))
291 /* for the possible benefit of user code linked with libguile. */
297 SCM_DEFINE (scm_localtime
, "localtime", 1, 1, 0,
298 (SCM time
, SCM zone
),
299 "Return an object representing the broken down components of\n"
300 "@var{time}, an integer like the one returned by\n"
301 "@code{current-time}. The time zone for the calculation is\n"
302 "optionally specified by @var{zone} (a string), otherwise the\n"
303 "@code{TZ} environment variable or the system default is used.")
304 #define FUNC_NAME s_scm_localtime
307 struct tm
*ltptr
, lt
, *utc
;
314 itime
= SCM_NUM2LONG (1, time
);
316 /* deferring interupts is essential since a) setzone may install a temporary
317 environment b) localtime uses a static buffer. */
319 oldenv
= setzone (zone
, SCM_ARG2
, FUNC_NAME
);
320 #ifdef LOCALTIME_CACHE
323 ltptr
= localtime (&itime
);
329 /* copy zone name before calling gmtime or restoring zone. */
330 #if defined (HAVE_TM_ZONE)
331 ptr
= ltptr
->tm_zone
;
332 #elif defined (HAVE_TZNAME)
333 ptr
= tzname
[ (ltptr
->tm_isdst
== 1) ? 1 : 0 ];
337 zname
= scm_malloc (strlen (ptr
) + 1);
340 /* the struct is copied in case localtime and gmtime share a buffer. */
343 utc
= gmtime (&itime
);
346 restorezone (zone
, oldenv
, FUNC_NAME
);
347 /* delayed until zone has been restored. */
349 if (utc
== NULL
|| ltptr
== NULL
)
352 /* calculate timezone offset in seconds west of UTC. */
353 zoff
= (utc
->tm_hour
- lt
.tm_hour
) * 3600 + (utc
->tm_min
- lt
.tm_min
) * 60
354 + utc
->tm_sec
- lt
.tm_sec
;
355 if (utc
->tm_year
< lt
.tm_year
)
356 zoff
-= 24 * 60 * 60;
357 else if (utc
->tm_year
> lt
.tm_year
)
358 zoff
+= 24 * 60 * 60;
359 else if (utc
->tm_yday
< lt
.tm_yday
)
360 zoff
-= 24 * 60 * 60;
361 else if (utc
->tm_yday
> lt
.tm_yday
)
362 zoff
+= 24 * 60 * 60;
364 result
= filltime (<
, zoff
, zname
);
372 SCM_DEFINE (scm_gmtime
, "gmtime", 1, 0, 0,
374 "Return an object representing the broken down components of\n"
375 "@var{time}, an integer like the one returned by\n"
376 "@code{current-time}. The values are calculated for UTC.")
377 #define FUNC_NAME s_scm_gmtime
383 itime
= SCM_NUM2LONG (1, time
);
385 bd_time
= gmtime (&itime
);
388 result
= filltime (bd_time
, 0, "GMT");
394 /* copy time components from a Scheme object to a struct tm. */
396 bdtime2c (SCM sbd_time
, struct tm
*lt
, int pos
, const char *subr
)
401 SCM_ASSERT (SCM_VECTORP (sbd_time
)
402 && SCM_VECTOR_LENGTH (sbd_time
) == 11,
403 sbd_time
, pos
, subr
);
404 velts
= SCM_VELTS (sbd_time
);
405 for (i
= 0; i
< 10; i
++)
407 SCM_ASSERT (SCM_INUMP (velts
[i
]), sbd_time
, pos
, subr
);
409 SCM_ASSERT (SCM_FALSEP (velts
[10]) || SCM_STRINGP (velts
[10]),
410 sbd_time
, pos
, subr
);
412 lt
->tm_sec
= SCM_INUM (velts
[0]);
413 lt
->tm_min
= SCM_INUM (velts
[1]);
414 lt
->tm_hour
= SCM_INUM (velts
[2]);
415 lt
->tm_mday
= SCM_INUM (velts
[3]);
416 lt
->tm_mon
= SCM_INUM (velts
[4]);
417 lt
->tm_year
= SCM_INUM (velts
[5]);
418 lt
->tm_wday
= SCM_INUM (velts
[6]);
419 lt
->tm_yday
= SCM_INUM (velts
[7]);
420 lt
->tm_isdst
= SCM_INUM (velts
[8]);
422 lt
->tm_gmtoff
= SCM_INUM (velts
[9]);
423 if (SCM_FALSEP (velts
[10]))
426 lt
->tm_zone
= SCM_STRING_CHARS (velts
[10]);
430 SCM_DEFINE (scm_mktime
, "mktime", 1, 1, 0,
431 (SCM sbd_time
, SCM zone
),
432 "@var{bd-time} is an object representing broken down time and @code{zone}\n"
433 "is an optional time zone specifier (otherwise the TZ environment variable\n"
434 "or the system default is used).\n\n"
435 "Returns a pair: the car is a corresponding\n"
436 "integer time value like that returned\n"
437 "by @code{current-time}; the cdr is a broken down time object, similar to\n"
438 "as @var{bd-time} but with normalized values.")
439 #define FUNC_NAME s_scm_mktime
449 bdtime2c (sbd_time
, <
, SCM_ARG1
, FUNC_NAME
);
452 oldenv
= setzone (zone
, SCM_ARG2
, FUNC_NAME
);
453 #ifdef LOCALTIME_CACHE
456 itime
= mktime (<
);
463 /* copy zone name before calling gmtime or restoring the zone. */
464 #if defined (HAVE_TM_ZONE)
466 #elif defined (HAVE_TZNAME)
467 ptr
= tzname
[ (lt
.tm_isdst
== 1) ? 1 : 0 ];
471 zname
= scm_malloc (strlen (ptr
) + 1);
475 /* get timezone offset in seconds west of UTC. */
476 utc
= gmtime (&itime
);
480 restorezone (zone
, oldenv
, FUNC_NAME
);
481 /* delayed until zone has been restored. */
483 if (utc
== NULL
|| itime
== -1)
486 zoff
= (utc
->tm_hour
- lt
.tm_hour
) * 3600 + (utc
->tm_min
- lt
.tm_min
) * 60
487 + utc
->tm_sec
- lt
.tm_sec
;
488 if (utc
->tm_year
< lt
.tm_year
)
489 zoff
-= 24 * 60 * 60;
490 else if (utc
->tm_year
> lt
.tm_year
)
491 zoff
+= 24 * 60 * 60;
492 else if (utc
->tm_yday
< lt
.tm_yday
)
493 zoff
-= 24 * 60 * 60;
494 else if (utc
->tm_yday
> lt
.tm_yday
)
495 zoff
+= 24 * 60 * 60;
497 result
= scm_cons (scm_long2num ((long) itime
),
498 filltime (<
, zoff
, zname
));
507 SCM_DEFINE (scm_tzset
, "tzset", 0, 0, 0,
509 "Initialize the timezone from the TZ environment variable\n"
510 "or the system default. It's not usually necessary to call this procedure\n"
511 "since it's done automatically by other procedures that depend on the\n"
513 #define FUNC_NAME s_scm_tzset
516 return SCM_UNSPECIFIED
;
519 #endif /* HAVE_TZSET */
521 SCM_DEFINE (scm_strftime
, "strftime", 2, 0, 0,
522 (SCM format
, SCM stime
),
523 "Formats a time specification @var{time} using @var{template}. @var{time}\n"
524 "is an object with time components in the form returned by @code{localtime}\n"
525 "or @code{gmtime}. @var{template} is a string which can include formatting\n"
526 "specifications introduced by a @code{%} character. The formatting of\n"
527 "month and day names is dependent on the current locale. The value returned\n"
528 "is the formatted string.\n"
529 "@xref{Formatting Date and Time, , , libc, The GNU C Library Reference Manual}.)")
530 #define FUNC_NAME s_scm_strftime
540 SCM_VALIDATE_STRING (1, format
);
541 bdtime2c (stime
, &t
, SCM_ARG2
, FUNC_NAME
);
543 fmt
= SCM_STRING_CHARS (format
);
544 len
= SCM_STRING_LENGTH (format
);
546 /* Ugly hack: strftime can return 0 if its buffer is too small,
547 but some valid time strings (e.g. "%p") can sometimes produce
548 a zero-byte output string! Workaround is to prepend a junk
549 character to the format string, so that valid returns are always
551 myfmt
= scm_malloc (len
+2);
553 strncpy(myfmt
+1, fmt
, len
);
556 tbuf
= scm_malloc (size
);
558 #if !defined (HAVE_TM_ZONE)
559 /* it seems the only way to tell non-GNU versions of strftime what
560 zone to use (for the %Z format) is to set TZ in the
561 environment. interrupts and thread switching must be deferred
562 until TZ is restored. */
563 char **oldenv
= NULL
;
564 SCM
*velts
= (SCM
*) SCM_VELTS (stime
);
567 if (!SCM_FALSEP (velts
[10]) && *SCM_STRING_CHARS (velts
[10]) != 0)
569 /* it's not required that the TZ setting be correct, just that
570 it has the right name. so try something like TZ=EST0.
571 using only TZ=EST would be simpler but it doesn't work on
572 some OSs, e.g., Solaris. */
574 scm_string_append (scm_cons (velts
[10],
575 scm_cons (scm_makfrom0str ("0"),
580 oldenv
= setzone (zone
, SCM_ARG2
, FUNC_NAME
);
584 #ifdef LOCALTIME_CACHE
588 /* POSIX says strftime returns 0 on buffer overrun, but old
589 systems (i.e. libc 4 on GNU/Linux) might return `size' in that
591 while ((len
= strftime (tbuf
, size
, myfmt
, &t
)) == 0 || len
== size
)
595 tbuf
= scm_malloc (size
);
598 #if !defined (HAVE_TM_ZONE)
601 restorezone (velts
[10], oldenv
, FUNC_NAME
);
607 result
= scm_mem2string (tbuf
+ 1, len
- 1);
615 SCM_DEFINE (scm_strptime
, "strptime", 2, 0, 0,
616 (SCM format
, SCM string
),
617 "Performs the reverse action to @code{strftime}, parsing\n"
618 "@var{string} according to the specification supplied in\n"
619 "@var{template}. The interpretation of month and day names is\n"
620 "dependent on the current locale. The value returned is a pair.\n"
621 "The car has an object with time components\n"
622 "in the form returned by @code{localtime} or @code{gmtime},\n"
623 "but the time zone components\n"
624 "are not usefully set.\n"
625 "The cdr reports the number of characters from @var{string}\n"
626 "which were used for the conversion.")
627 #define FUNC_NAME s_scm_strptime
630 char *fmt
, *str
, *rest
;
632 SCM_VALIDATE_STRING (1, format
);
633 SCM_VALIDATE_STRING (2, string
);
635 fmt
= SCM_STRING_CHARS (format
);
636 str
= SCM_STRING_CHARS (string
);
638 /* initialize the struct tm */
639 #define tm_init(field) t.field = 0
650 /* GNU glibc strptime() "%s" is affected by the current timezone, since it
651 reads a UTC time_t value and converts with localtime_r() to set the tm
652 fields, hence the use of SCM_DEFER_INTS. */
655 if ((rest
= strptime (str
, fmt
, &t
)) == NULL
)
659 return scm_cons (filltime (&t
, 0, NULL
), SCM_MAKINUM (rest
- str
));
662 #endif /* HAVE_STRPTIME */
667 scm_c_define ("internal-time-units-per-second",
668 scm_long2num((long) SCM_TIME_UNITS_PER_SECOND
));
671 if (!scm_your_base
.time
) ftime(&scm_your_base
);
673 if (!scm_your_base
) time(&scm_your_base
);
676 if (!scm_my_base
) scm_my_base
= mytime();
678 scm_add_feature ("current-time");
679 #include "libguile/stime.x"