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