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