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