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