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 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. */ |
64 | extern 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 |
71 | extern 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 |
80 | extern 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 | 91 | static |
1c299a6b | 92 | timet 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 | 107 | struct timeb scm_your_base = {0}; |
b450f070 GB |
108 | #else |
109 | timet scm_your_base = 0; | |
110 | #endif | |
1bbd0b84 | 111 | |
1c299a6b | 112 | SCM_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 | 138 | SCM_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 |
180 | static long scm_my_base = 0; |
181 | ||
1c299a6b ML |
182 | long |
183 | scm_c_get_internal_run_time () | |
184 | { | |
185 | return mytime () - scm_my_base; | |
186 | } | |
187 | ||
188 | SCM_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 | 206 | SCM_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 | 222 | SCM_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 | |
259 | static SCM | |
d6d9e957 | 260 | filltime (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 | 278 | static 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 | 284 | static char ** |
3eeba8d4 | 285 | setzone (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 | ||
305 | static void | |
e81d98ec | 306 | restorezone (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 | 319 | SCM_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 (<, 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 | |
45198ffb KR |
400 | /* tm_zone is normally a pointer, not an array within struct tm, so we might |
401 | have to worry about the lifespan of what it points to. The posix specs | |
402 | don't seem to say anything about this, let's assume here that tm_zone | |
403 | will be a constant and therefore no protection or anything is needed | |
404 | until we copy it in filltime(). */ | |
405 | ||
1c299a6b | 406 | SCM_DEFINE (scm_gmtime, "gmtime", 1, 0, 0, |
1bbd0b84 | 407 | (SCM time), |
1e6808ea MG |
408 | "Return an object representing the broken down components of\n" |
409 | "@var{time}, an integer like the one returned by\n" | |
410 | "@code{current-time}. The values are calculated for UTC.") | |
1bbd0b84 | 411 | #define FUNC_NAME s_scm_gmtime |
19468eff GH |
412 | { |
413 | timet itime; | |
45198ffb | 414 | struct tm bd_buf, *bd_time; |
d6d9e957 | 415 | const char *zname; |
19468eff | 416 | |
e4b265d8 | 417 | itime = SCM_NUM2LONG (1, time); |
45198ffb | 418 | |
73ae3b4c KR |
419 | /* POSIX says gmtime sets errno, but C99 doesn't say that. |
420 | Give a sensible default value in case gmtime doesn't set it. */ | |
421 | errno = EINVAL; | |
45198ffb KR |
422 | |
423 | #if HAVE_GMTIME_R | |
424 | bd_time = gmtime_r (&itime, &bd_buf); | |
425 | #else | |
426 | SCM_DEFER_INTS; | |
19468eff | 427 | bd_time = gmtime (&itime); |
45198ffb KR |
428 | if (bd_time != NULL) |
429 | bd_buf = *bd_time; | |
430 | SCM_ALLOW_INTS; | |
431 | #endif | |
19468eff | 432 | if (bd_time == NULL) |
1bbd0b84 | 433 | SCM_SYSERROR; |
45198ffb | 434 | |
d6d9e957 | 435 | #if HAVE_STRUCT_TM_TM_ZONE |
45198ffb | 436 | zname = bd_buf.tm_zone; |
d6d9e957 KR |
437 | #else |
438 | zname = "GMT"; | |
439 | #endif | |
45198ffb | 440 | return filltime (&bd_buf, 0, zname); |
19468eff | 441 | } |
1bbd0b84 | 442 | #undef FUNC_NAME |
19468eff | 443 | |
b9525b92 GH |
444 | /* copy time components from a Scheme object to a struct tm. */ |
445 | static void | |
3eeba8d4 | 446 | bdtime2c (SCM sbd_time, struct tm *lt, int pos, const char *subr) |
19468eff | 447 | { |
34d19ef6 | 448 | SCM const *velts; |
55a7fc62 GH |
449 | int i; |
450 | ||
0c95b57d | 451 | SCM_ASSERT (SCM_VECTORP (sbd_time) |
9fd38a3d | 452 | && SCM_VECTOR_LENGTH (sbd_time) == 11, |
b9525b92 | 453 | sbd_time, pos, subr); |
55a7fc62 GH |
454 | velts = SCM_VELTS (sbd_time); |
455 | for (i = 0; i < 10; i++) | |
456 | { | |
457 | SCM_ASSERT (SCM_INUMP (velts[i]), sbd_time, pos, subr); | |
458 | } | |
0c95b57d | 459 | SCM_ASSERT (SCM_FALSEP (velts[10]) || SCM_STRINGP (velts[10]), |
55a7fc62 GH |
460 | sbd_time, pos, subr); |
461 | ||
462 | lt->tm_sec = SCM_INUM (velts[0]); | |
463 | lt->tm_min = SCM_INUM (velts[1]); | |
464 | lt->tm_hour = SCM_INUM (velts[2]); | |
465 | lt->tm_mday = SCM_INUM (velts[3]); | |
466 | lt->tm_mon = SCM_INUM (velts[4]); | |
467 | lt->tm_year = SCM_INUM (velts[5]); | |
468 | lt->tm_wday = SCM_INUM (velts[6]); | |
469 | lt->tm_yday = SCM_INUM (velts[7]); | |
470 | lt->tm_isdst = SCM_INUM (velts[8]); | |
c7abe4f3 | 471 | #ifdef HAVE_TM_ZONE |
55a7fc62 GH |
472 | lt->tm_gmtoff = SCM_INUM (velts[9]); |
473 | if (SCM_FALSEP (velts[10])) | |
474 | lt->tm_zone = NULL; | |
475 | else | |
a002f1a2 | 476 | lt->tm_zone = SCM_STRING_CHARS (velts[10]); |
c7abe4f3 | 477 | #endif |
b9525b92 GH |
478 | } |
479 | ||
1c299a6b | 480 | SCM_DEFINE (scm_mktime, "mktime", 1, 1, 0, |
1bbd0b84 | 481 | (SCM sbd_time, SCM zone), |
a3c8b9fc MD |
482 | "@var{bd-time} is an object representing broken down time and @code{zone}\n" |
483 | "is an optional time zone specifier (otherwise the TZ environment variable\n" | |
484 | "or the system default is used).\n\n" | |
a8eac221 | 485 | "Returns a pair: the car is a corresponding\n" |
a3c8b9fc | 486 | "integer time value like that returned\n" |
a8eac221 | 487 | "by @code{current-time}; the cdr is a broken down time object, similar to\n" |
a3c8b9fc | 488 | "as @var{bd-time} but with normalized values.") |
1bbd0b84 | 489 | #define FUNC_NAME s_scm_mktime |
b9525b92 GH |
490 | { |
491 | timet itime; | |
492 | struct tm lt, *utc; | |
493 | SCM result; | |
494 | int zoff; | |
495 | char *zname = 0; | |
ef9ff3fd | 496 | char **oldenv; |
b9525b92 GH |
497 | int err; |
498 | ||
1bbd0b84 | 499 | bdtime2c (sbd_time, <, SCM_ARG1, FUNC_NAME); |
19468eff GH |
500 | |
501 | SCM_DEFER_INTS; | |
1bbd0b84 | 502 | oldenv = setzone (zone, SCM_ARG2, FUNC_NAME); |
38c1d3c4 GH |
503 | #ifdef LOCALTIME_CACHE |
504 | tzset (); | |
505 | #endif | |
19468eff | 506 | itime = mktime (<); |
73ae3b4c KR |
507 | /* POSIX doesn't say mktime sets errno, and on glibc 2.3.2 for instance it |
508 | doesn't. Force a sensible value for our error message. */ | |
509 | err = EINVAL; | |
19468eff | 510 | |
b9525b92 GH |
511 | if (itime != -1) |
512 | { | |
4d3bacdd | 513 | const char *ptr; |
ef9ff3fd | 514 | |
43ff3170 GH |
515 | /* copy zone name before calling gmtime or restoring the zone. */ |
516 | #if defined (HAVE_TM_ZONE) | |
ef9ff3fd | 517 | ptr = lt.tm_zone; |
43ff3170 | 518 | #elif defined (HAVE_TZNAME) |
ef9ff3fd | 519 | ptr = tzname[ (lt.tm_isdst == 1) ? 1 : 0 ]; |
43ff3170 GH |
520 | #else |
521 | ptr = ""; | |
b9525b92 | 522 | #endif |
4c9419ac | 523 | zname = scm_malloc (strlen (ptr) + 1); |
ef9ff3fd | 524 | strcpy (zname, ptr); |
b9525b92 | 525 | } |
ef9ff3fd GH |
526 | |
527 | /* get timezone offset in seconds west of UTC. */ | |
73ae3b4c KR |
528 | /* POSIX says gmtime sets errno, but C99 doesn't say that. |
529 | Give a sensible default value in case gmtime doesn't set it. */ | |
ef9ff3fd GH |
530 | utc = gmtime (&itime); |
531 | if (utc == NULL) | |
532 | err = errno; | |
533 | ||
1bbd0b84 | 534 | restorezone (zone, oldenv, FUNC_NAME); |
b9525b92 GH |
535 | /* delayed until zone has been restored. */ |
536 | errno = err; | |
537 | if (utc == NULL || itime == -1) | |
1bbd0b84 | 538 | SCM_SYSERROR; |
b9525b92 | 539 | |
19468eff GH |
540 | zoff = (utc->tm_hour - lt.tm_hour) * 3600 + (utc->tm_min - lt.tm_min) * 60 |
541 | + utc->tm_sec - lt.tm_sec; | |
542 | if (utc->tm_year < lt.tm_year) | |
543 | zoff -= 24 * 60 * 60; | |
544 | else if (utc->tm_year > lt.tm_year) | |
545 | zoff += 24 * 60 * 60; | |
546 | else if (utc->tm_yday < lt.tm_yday) | |
547 | zoff -= 24 * 60 * 60; | |
548 | else if (utc->tm_yday > lt.tm_yday) | |
549 | zoff += 24 * 60 * 60; | |
550 | ||
19468eff GH |
551 | result = scm_cons (scm_long2num ((long) itime), |
552 | filltime (<, zoff, zname)); | |
553 | SCM_ALLOW_INTS; | |
4c9419ac MV |
554 | if (zname) |
555 | free (zname); | |
19468eff | 556 | return result; |
0f2d19dd | 557 | } |
1bbd0b84 | 558 | #undef FUNC_NAME |
0f2d19dd | 559 | |
38c1d3c4 | 560 | #ifdef HAVE_TZSET |
1c299a6b | 561 | SCM_DEFINE (scm_tzset, "tzset", 0, 0, 0, |
1bbd0b84 | 562 | (void), |
a3c8b9fc MD |
563 | "Initialize the timezone from the TZ environment variable\n" |
564 | "or the system default. It's not usually necessary to call this procedure\n" | |
565 | "since it's done automatically by other procedures that depend on the\n" | |
566 | "timezone.") | |
1bbd0b84 | 567 | #define FUNC_NAME s_scm_tzset |
0f2d19dd | 568 | { |
19468eff GH |
569 | tzset(); |
570 | return SCM_UNSPECIFIED; | |
0f2d19dd | 571 | } |
1bbd0b84 | 572 | #undef FUNC_NAME |
38c1d3c4 | 573 | #endif /* HAVE_TZSET */ |
0f2d19dd | 574 | |
a1ec6916 | 575 | SCM_DEFINE (scm_strftime, "strftime", 2, 0, 0, |
1bbd0b84 | 576 | (SCM format, SCM stime), |
a3c8b9fc MD |
577 | "Formats a time specification @var{time} using @var{template}. @var{time}\n" |
578 | "is an object with time components in the form returned by @code{localtime}\n" | |
579 | "or @code{gmtime}. @var{template} is a string which can include formatting\n" | |
580 | "specifications introduced by a @code{%} character. The formatting of\n" | |
581 | "month and day names is dependent on the current locale. The value returned\n" | |
582 | "is the formatted string.\n" | |
583 | "@xref{Formatting Date and Time, , , libc, The GNU C Library Reference Manual}.)") | |
1bbd0b84 | 584 | #define FUNC_NAME s_scm_strftime |
b9525b92 GH |
585 | { |
586 | struct tm t; | |
587 | ||
588 | char *tbuf; | |
589 | int size = 50; | |
a15e6dcc | 590 | char *fmt, *myfmt; |
b9525b92 | 591 | int len; |
ef9ff3fd | 592 | SCM result; |
b9525b92 | 593 | |
a6d9e5ab | 594 | SCM_VALIDATE_STRING (1, format); |
1bbd0b84 | 595 | bdtime2c (stime, &t, SCM_ARG2, FUNC_NAME); |
b9525b92 | 596 | |
a6d9e5ab DH |
597 | fmt = SCM_STRING_CHARS (format); |
598 | len = SCM_STRING_LENGTH (format); | |
b9525b92 | 599 | |
a15e6dcc MV |
600 | /* Ugly hack: strftime can return 0 if its buffer is too small, |
601 | but some valid time strings (e.g. "%p") can sometimes produce | |
602 | a zero-byte output string! Workaround is to prepend a junk | |
603 | character to the format string, so that valid returns are always | |
604 | nonzero. */ | |
4c9419ac | 605 | myfmt = scm_malloc (len+2); |
a15e6dcc MV |
606 | *myfmt = 'x'; |
607 | strncpy(myfmt+1, fmt, len); | |
608 | myfmt[len+1] = 0; | |
609 | ||
4c9419ac | 610 | tbuf = scm_malloc (size); |
b8a1b29b GH |
611 | { |
612 | #if !defined (HAVE_TM_ZONE) | |
613 | /* it seems the only way to tell non-GNU versions of strftime what | |
614 | zone to use (for the %Z format) is to set TZ in the | |
615 | environment. interrupts and thread switching must be deferred | |
616 | until TZ is restored. */ | |
617 | char **oldenv = NULL; | |
2e945bcc | 618 | SCM *velts = (SCM *) SCM_VELTS (stime); |
e652b54f | 619 | int have_zone = 0; |
b8a1b29b | 620 | |
36284627 | 621 | if (!SCM_FALSEP (velts[10]) && *SCM_STRING_CHARS (velts[10]) != 0) |
b8a1b29b GH |
622 | { |
623 | /* it's not required that the TZ setting be correct, just that | |
e652b54f GH |
624 | it has the right name. so try something like TZ=EST0. |
625 | using only TZ=EST would be simpler but it doesn't work on | |
626 | some OSs, e.g., Solaris. */ | |
627 | SCM zone = | |
628 | scm_string_append (scm_cons (velts[10], | |
629 | scm_cons (scm_makfrom0str ("0"), | |
630 | SCM_EOL))); | |
1c299a6b | 631 | |
e652b54f | 632 | have_zone = 1; |
b8a1b29b | 633 | SCM_DEFER_INTS; |
e652b54f | 634 | oldenv = setzone (zone, SCM_ARG2, FUNC_NAME); |
b8a1b29b GH |
635 | } |
636 | #endif | |
637 | ||
38c1d3c4 | 638 | #ifdef LOCALTIME_CACHE |
b8a1b29b GH |
639 | tzset (); |
640 | #endif | |
641 | ||
a15e6dcc MV |
642 | /* POSIX says strftime returns 0 on buffer overrun, but old |
643 | systems (i.e. libc 4 on GNU/Linux) might return `size' in that | |
644 | case. */ | |
645 | while ((len = strftime (tbuf, size, myfmt, &t)) == 0 || len == size) | |
b8a1b29b | 646 | { |
4c9419ac | 647 | free (tbuf); |
b8a1b29b | 648 | size *= 2; |
4c9419ac | 649 | tbuf = scm_malloc (size); |
b8a1b29b GH |
650 | } |
651 | ||
652 | #if !defined (HAVE_TM_ZONE) | |
e652b54f | 653 | if (have_zone) |
b8a1b29b GH |
654 | { |
655 | restorezone (velts[10], oldenv, FUNC_NAME); | |
656 | SCM_ALLOW_INTS; | |
657 | } | |
38c1d3c4 | 658 | #endif |
b9525b92 | 659 | } |
b8a1b29b | 660 | |
36284627 | 661 | result = scm_mem2string (tbuf + 1, len - 1); |
4c9419ac MV |
662 | free (tbuf); |
663 | free (myfmt); | |
ef9ff3fd | 664 | return result; |
b9525b92 | 665 | } |
1bbd0b84 | 666 | #undef FUNC_NAME |
b9525b92 | 667 | |
f25f761d | 668 | #ifdef HAVE_STRPTIME |
a1ec6916 | 669 | SCM_DEFINE (scm_strptime, "strptime", 2, 0, 0, |
1bbd0b84 | 670 | (SCM format, SCM string), |
a8eac221 MG |
671 | "Performs the reverse action to @code{strftime}, parsing\n" |
672 | "@var{string} according to the specification supplied in\n" | |
673 | "@var{template}. The interpretation of month and day names is\n" | |
674 | "dependent on the current locale. The value returned is a pair.\n" | |
675 | "The car has an object with time components\n" | |
a3c8b9fc MD |
676 | "in the form returned by @code{localtime} or @code{gmtime},\n" |
677 | "but the time zone components\n" | |
678 | "are not usefully set.\n" | |
a8eac221 MG |
679 | "The cdr reports the number of characters from @var{string}\n" |
680 | "which were used for the conversion.") | |
1bbd0b84 | 681 | #define FUNC_NAME s_scm_strptime |
b9525b92 | 682 | { |
b9525b92 GH |
683 | struct tm t; |
684 | char *fmt, *str, *rest; | |
685 | ||
a6d9e5ab DH |
686 | SCM_VALIDATE_STRING (1, format); |
687 | SCM_VALIDATE_STRING (2, string); | |
b9525b92 | 688 | |
a6d9e5ab DH |
689 | fmt = SCM_STRING_CHARS (format); |
690 | str = SCM_STRING_CHARS (string); | |
b9525b92 GH |
691 | |
692 | /* initialize the struct tm */ | |
693 | #define tm_init(field) t.field = 0 | |
694 | tm_init (tm_sec); | |
695 | tm_init (tm_min); | |
696 | tm_init (tm_hour); | |
697 | tm_init (tm_mday); | |
698 | tm_init (tm_mon); | |
699 | tm_init (tm_year); | |
700 | tm_init (tm_wday); | |
701 | tm_init (tm_yday); | |
702 | #undef tm_init | |
703 | ||
03b79aa3 KR |
704 | /* GNU glibc strptime() "%s" is affected by the current timezone, since it |
705 | reads a UTC time_t value and converts with localtime_r() to set the tm | |
706 | fields, hence the use of SCM_DEFER_INTS. */ | |
b9525b92 GH |
707 | t.tm_isdst = -1; |
708 | SCM_DEFER_INTS; | |
709 | if ((rest = strptime (str, fmt, &t)) == NULL) | |
73ae3b4c KR |
710 | { |
711 | /* POSIX doesn't say strptime sets errno, and on glibc 2.3.2 for | |
712 | instance it doesn't. Force a sensible value for our error | |
713 | message. */ | |
714 | errno = EINVAL; | |
715 | SCM_SYSERROR; | |
716 | } | |
b9525b92 GH |
717 | |
718 | SCM_ALLOW_INTS; | |
719 | return scm_cons (filltime (&t, 0, NULL), SCM_MAKINUM (rest - str)); | |
b9525b92 | 720 | } |
1bbd0b84 | 721 | #undef FUNC_NAME |
f25f761d | 722 | #endif /* HAVE_STRPTIME */ |
b9525b92 | 723 | |
0f2d19dd JB |
724 | void |
725 | scm_init_stime() | |
0f2d19dd | 726 | { |
86d31dfe | 727 | scm_c_define ("internal-time-units-per-second", |
756414cf | 728 | scm_long2num((long) SCM_TIME_UNITS_PER_SECOND)); |
0f2d19dd JB |
729 | |
730 | #ifdef HAVE_FTIME | |
731 | if (!scm_your_base.time) ftime(&scm_your_base); | |
732 | #else | |
733 | if (!scm_your_base) time(&scm_your_base); | |
734 | #endif | |
735 | ||
736 | if (!scm_my_base) scm_my_base = mytime(); | |
737 | ||
876c87ce | 738 | scm_add_feature ("current-time"); |
a0599745 | 739 | #include "libguile/stime.x" |
0f2d19dd JB |
740 | } |
741 | ||
89e00824 ML |
742 | |
743 | /* | |
744 | Local Variables: | |
745 | c-file-style: "gnu" | |
746 | End: | |
747 | */ |