Time.c 874 B

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