time.c 1.5 KB

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