time.c 6.6 KB

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