Time.c 889 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "libraries/Time.h"
  6. #include "utils/Functions.h"
  7. static void lTimeGetMillis(Script* sc) {
  8. struct timespec time;
  9. if(clock_gettime(CLOCK_REALTIME, &time)) {
  10. sError(sc, "cannot get clock time: %s", strerror(errno));
  11. return;
  12. }
  13. sPushLong(sc, time.tv_nsec / 1000000L + time.tv_sec * 1000L);
  14. }
  15. static void lTimeGetNanos(Script* sc) {
  16. struct timespec time;
  17. if(clock_gettime(CLOCK_BOOTTIME, &time)) {
  18. sError(sc, "cannot get clock time: %s", strerror(errno));
  19. return;
  20. }
  21. sPushLong(sc, time.tv_nsec + time.tv_sec * 1000000000L);
  22. }
  23. void lTimeRegister() {
  24. Structs sts;
  25. stsInit(&sts);
  26. Function f;
  27. gfInit(&f, "getMillis", dtLong(), lTimeGetMillis);
  28. gfsAdd(&f);
  29. gfInit(&f, "getNanos", dtLong(), lTimeGetNanos);
  30. gfsAdd(&f);
  31. }