time.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Part of Scheme 48 1.9. See file COPYING for notices and license.
  3. *
  4. * Authors: Richard Kelsey, Jonathan Rees, Mike Sperber, Marcus Crestani,
  5. * Robert Ransom
  6. */
  7. /*
  8. * An interface to the POSIX time functionality.
  9. */
  10. #include <time.h>
  11. #include <stdlib.h>
  12. #include "scheme48.h"
  13. static s48_ref_t posix_ctime(s48_call_t call, s48_ref_t sch_time);
  14. static s48_ref_t posix_time(s48_call_t call);
  15. static s48_ref_t posix_asctime(s48_call_t call, s48_ref_t sch_t);
  16. static s48_ref_t posix_localtime(s48_call_t call, s48_ref_t sch_time);
  17. static s48_ref_t posix_gmtime(s48_call_t call, s48_ref_t sch_time);
  18. static s48_ref_t posix_mktime(s48_call_t call, s48_ref_t sch_t);
  19. static s48_ref_t posix_strftime(s48_call_t call, s48_ref_t sch_format, s48_ref_t sch_t);
  20. static s48_ref_t posix_time_type_binding;
  21. /*
  22. * Install all exported functions in Scheme48.
  23. */
  24. void
  25. s48_init_posix_time(void)
  26. {
  27. S48_EXPORT_FUNCTION(posix_ctime);
  28. S48_EXPORT_FUNCTION(posix_time);
  29. posix_time_type_binding =
  30. s48_get_imported_binding_2("posix-time-type");
  31. S48_EXPORT_FUNCTION(posix_asctime);
  32. S48_EXPORT_FUNCTION(posix_localtime);
  33. S48_EXPORT_FUNCTION(posix_gmtime);
  34. S48_EXPORT_FUNCTION(posix_mktime);
  35. S48_EXPORT_FUNCTION(posix_strftime);
  36. }
  37. /* ************************************************************ */
  38. /*
  39. * Convert a time_t into a Scheme time record.
  40. */
  41. s48_ref_t
  42. s48_posix_enter_time(s48_call_t call, time_t time)
  43. {
  44. s48_ref_t sch_time;
  45. s48_ref_t temp;
  46. sch_time = s48_make_record_2(call, posix_time_type_binding);
  47. /* Stashing the time value into temp before handing it off to
  48. S48_UNSAFE_RECORD_SET is necessary because its evaluation may
  49. cause GC; that GC could destroy the temporary holding the value
  50. of sch_time. */
  51. temp = s48_enter_long_2(call, time);
  52. s48_unsafe_record_set_2(call, sch_time, 0, temp);
  53. return sch_time;
  54. }
  55. /*
  56. * Convert a Scheme time record into a time_t.
  57. */
  58. static time_t
  59. extract_time(s48_call_t call, s48_ref_t time)
  60. {
  61. s48_check_record_type_2(call, time, posix_time_type_binding);
  62. return s48_extract_long_2(call, s48_unsafe_record_ref_2(call, time, 0));
  63. }
  64. /*
  65. * The posix ctime() procedure, which converts a time_t into a string, using
  66. * the local time zone.
  67. *
  68. * ENTER_STRING does a copy, which gets us out of ctime()'s static buffer.
  69. */
  70. static s48_ref_t
  71. posix_ctime(s48_call_t call, s48_ref_t sch_time)
  72. {
  73. time_t time;
  74. s48_check_record_type_2(call, sch_time, posix_time_type_binding);
  75. time = extract_time(call, sch_time);
  76. return s48_enter_byte_string_2(call, ctime(&time));
  77. }
  78. static s48_ref_t
  79. posix_time(s48_call_t call)
  80. {
  81. time_t the_time, status;
  82. if (time(&the_time) == -1)
  83. s48_assertion_violation_2(call, "posix_time", "unknown error calling time(3)", 0);
  84. return s48_posix_enter_time(call, the_time);
  85. }
  86. /*
  87. * Dates.
  88. *
  89. * POSIX timezone handling is f***ed beyond redemption:
  90. *
  91. * tzname, timezone and daylight are global variables that can be set
  92. * off the TZ environment variable via tzset(3). However, environment
  93. * variables cannot be set in a thread-safe manner ... Moreover, the
  94. * BSDs don't implement timezone and daylight.
  95. *
  96. * Olin's scsh code does various heroics to make timezone handling
  97. * work, but, again, that's not thread-safe. There's some hope in the
  98. * tm_zone and tm_gmtoff fields of struct tm that the BSDs and glibc
  99. * (with _BSD_SOURCE set) have, but we'll punt on this for now.
  100. */
  101. static s48_ref_t
  102. enter_tm(s48_call_t call, struct tm* t)
  103. {
  104. s48_ref_t vec = s48_make_vector_2(call, 9, s48_unspecific_2(call));
  105. s48_vector_set_2(call, vec, 0, s48_enter_long_as_fixnum_2(call, t->tm_sec));
  106. s48_vector_set_2(call, vec, 1, s48_enter_long_as_fixnum_2(call, t->tm_min));
  107. s48_vector_set_2(call, vec, 2, s48_enter_long_as_fixnum_2(call, t->tm_hour));
  108. s48_vector_set_2(call, vec, 3, s48_enter_long_as_fixnum_2(call, t->tm_mday));
  109. s48_vector_set_2(call, vec, 4, s48_enter_long_as_fixnum_2(call, t->tm_mon));
  110. s48_vector_set_2(call, vec, 5, s48_enter_long_as_fixnum_2(call, t->tm_year));
  111. s48_vector_set_2(call, vec, 6, s48_enter_long_as_fixnum_2(call, t->tm_wday));
  112. s48_vector_set_2(call, vec, 7, s48_enter_long_as_fixnum_2(call, t->tm_yday));
  113. s48_vector_set_2(call, vec, 8,
  114. (t->tm_isdst == 0)
  115. ? s48_false_2(call)
  116. : ((t->tm_isdst > 0)
  117. ? s48_true_2(call)
  118. : s48_unspecific_2(call)));
  119. return vec;
  120. }
  121. static void
  122. extract_tm(s48_call_t call, s48_ref_t sch_t, struct tm* t)
  123. {
  124. t->tm_sec = s48_extract_long_2(call, s48_vector_ref_2(call, sch_t, 0));
  125. t->tm_min = s48_extract_long_2(call, s48_vector_ref_2(call, sch_t, 1));
  126. t->tm_hour = s48_extract_long_2(call, s48_vector_ref_2(call, sch_t, 2));
  127. t->tm_mday = s48_extract_long_2(call, s48_vector_ref_2(call, sch_t, 3));
  128. t->tm_mon = s48_extract_long_2(call, s48_vector_ref_2(call, sch_t, 4));
  129. t->tm_year = s48_extract_long_2(call, s48_vector_ref_2(call, sch_t, 5));
  130. t->tm_wday = s48_extract_long_2(call, s48_vector_ref_2(call, sch_t, 6));
  131. t->tm_yday = s48_extract_long_2(call, s48_vector_ref_2(call, sch_t, 7));
  132. {
  133. s48_ref_t sch_isdst = s48_vector_ref_2(call, sch_t, 8);;
  134. if (s48_true_p_2(call, sch_isdst))
  135. t->tm_isdst = 1;
  136. else if (s48_false_p_2(call, sch_isdst))
  137. t->tm_isdst = 0;
  138. else
  139. t->tm_isdst = -1;
  140. }
  141. }
  142. static s48_ref_t
  143. posix_asctime(s48_call_t call, s48_ref_t sch_t)
  144. {
  145. struct tm t;
  146. extract_tm(call, sch_t, &t);
  147. char* text = asctime(&t);
  148. return s48_enter_byte_string_2(call, text);
  149. }
  150. static s48_ref_t
  151. posix_localtime(s48_call_t call, s48_ref_t sch_time)
  152. {
  153. time_t time = extract_time(call, sch_time);
  154. return enter_tm(call, localtime(&time));
  155. }
  156. static s48_ref_t
  157. posix_gmtime(s48_call_t call, s48_ref_t sch_time)
  158. {
  159. time_t time = extract_time(call, sch_time);
  160. return enter_tm(call, gmtime(&time));
  161. }
  162. static s48_ref_t
  163. posix_mktime(s48_call_t call, s48_ref_t sch_t)
  164. {
  165. struct tm t;
  166. time_t time;
  167. extract_tm(call, sch_t, &t);
  168. time = mktime(&t);
  169. if (time == -1)
  170. /* we feel your pain */
  171. s48_assertion_violation_2(call, "posix_mktime", "invalid time object", 1, sch_t);
  172. else
  173. return s48_posix_enter_time(call, time);
  174. }
  175. /* This is really ANSI C, but so is all of the above. */
  176. static s48_ref_t
  177. posix_strftime(s48_call_t call, s48_ref_t sch_format, s48_ref_t sch_t)
  178. {
  179. struct tm t;
  180. extract_tm(call, sch_t, &t);
  181. char local_buf[1024];
  182. char* buf = local_buf;
  183. size_t buf_size = 1024;
  184. size_t status;
  185. for (;;)
  186. {
  187. status = strftime(buf, buf_size, s48_extract_byte_vector_readonly_2(call, sch_format), &t);
  188. if (status > 0)
  189. {
  190. s48_ref_t result = s48_enter_byte_string_2(call, buf);
  191. if (buf != local_buf)
  192. free(buf);
  193. return result;
  194. }
  195. else
  196. {
  197. if (buf != local_buf)
  198. free(buf);
  199. buf_size *= 2;
  200. buf = malloc(buf_size * sizeof(char));
  201. if (buf == NULL)
  202. s48_out_of_memory_error_2(call);
  203. }
  204. }
  205. }