stime.c 23 KB

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