time.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees.
  2. See file COPYING. */
  3. #include <sys/timeb.h>
  4. #include <time.h>
  5. #include "scheme48.h"
  6. static s48_ref_t time_type_binding;
  7. /*
  8. * Install all exported functions in Scheme48.
  9. */
  10. void
  11. s48_init_time(void)
  12. {
  13. S48_EXPORT_FUNCTION(s48_get_current_time);
  14. S48_EXPORT_FUNCTION(s48_get_timezone);
  15. time_type_binding =
  16. s48_get_imported_binding_2("os-time-type");
  17. }
  18. /* ************************************************************ */
  19. /*
  20. * Convert a _timeb into a Scheme time record.
  21. */
  22. s48_ref_t
  23. s48_enter_time(s48_call_t call, struct _timeb *now)
  24. {
  25. s48_ref_t sch_time;
  26. sch_time = s48_make_record_2(call, time_type_binding);
  27. s48_unsafe_record_set_2(call, sch_time, 0, s48_enter_long_2(call, (long)now->time));
  28. s48_unsafe_record_set_2(call, sch_time, 1, s48_enter_long_2(call, now->millitm*1000));
  29. return sch_time;
  30. }
  31. /* returns a Scheme time record containing seconds since
  32. * midnight (00:00:00), January 1, 1970 (UTC) +
  33. * the fraction of a second in microseconds
  34. */
  35. s48_ref_t
  36. s48_get_current_time(s48_call_t call)
  37. {
  38. struct _timeb time;
  39. _ftime64_s(&time);
  40. return s48_enter_time(call, &time);
  41. }
  42. /* returns the difference in seconds between local time and UTC */
  43. s48_ref_t
  44. s48_get_timezone(s48_call_t call)
  45. {
  46. struct _timeb timebuffer;
  47. _ftime_s(&timebuffer);
  48. return s48_enter_long_2(call, -1 * (timebuffer.timezone -
  49. timebuffer.dstflag * 60) * 60);
  50. }