Time.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 lGetMillis(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. Value* v = sPopStructPointer(sc, VT_INT);
  14. if(v == NULL) {
  15. return;
  16. }
  17. v[0].data.intValue = (int)time.tv_sec;
  18. v[1].data.intValue = (int)((time.tv_nsec / 1000000) * 1000000);
  19. }
  20. static void lGetNanos(Script* sc) {
  21. struct timespec time;
  22. if(clock_gettime(CLOCK_BOOTTIME, &time)) {
  23. sError(sc, "cannot get clock time: %s", strerror(errno));
  24. return;
  25. }
  26. Value* v = sPopStructPointer(sc, VT_INT);
  27. if(v == NULL) {
  28. return;
  29. }
  30. v[0].data.intValue = (int)time.tv_sec;
  31. v[1].data.intValue = (int)time.tv_nsec;
  32. }
  33. static void lToTime(Script* sc) {
  34. Value* time = sPopStructPointer(sc, VT_INT);
  35. Value* timer = sPopStructPointer(sc, VT_INT);
  36. if(time == NULL || timer == NULL) {
  37. return;
  38. }
  39. long millis =
  40. timer[0].data.intValue * 1000l + timer[1].data.intValue / 1000000l;
  41. time_t t = millis / 1000l;
  42. struct tm* local = localtime(&t);
  43. if(local == NULL) {
  44. sError(sc, "cannot get local time: %s", strerror(errno));
  45. return;
  46. }
  47. time[0].data.intValue = local->tm_year + 1900;
  48. time[1].data.intValue = local->tm_mon;
  49. time[2].data.intValue = local->tm_mday;
  50. time[3].data.intValue = local->tm_hour;
  51. time[4].data.intValue = local->tm_min;
  52. time[5].data.intValue = local->tm_sec;
  53. time[6].data.intValue = (int)(millis % 1000);
  54. time[7].data.intValue = local->tm_isdst;
  55. }
  56. static void lToMillis(Script* sc) {
  57. Value* timer = sPopStructPointer(sc, VT_INT);
  58. Value* time = sPopStructPointer(sc, VT_INT);
  59. if(time == NULL || timer == NULL) {
  60. return;
  61. }
  62. struct tm local;
  63. local.tm_year = time[0].data.intValue - 1900;
  64. local.tm_mon = time[1].data.intValue;
  65. local.tm_mday = time[2].data.intValue;
  66. local.tm_hour = time[3].data.intValue;
  67. local.tm_min = time[4].data.intValue;
  68. local.tm_sec = time[5].data.intValue;
  69. local.tm_isdst = time[7].data.intValue;
  70. int64 seconds = mktime(&local);
  71. if(seconds == -1) {
  72. sError(sc, "cannot convert time: %s", strerror(errno));
  73. return;
  74. }
  75. timer[0].data.intValue = (int)seconds;
  76. timer[1].data.intValue = time[6].data.intValue * 1000000;
  77. }
  78. void lTimeRegister(void) {
  79. Struct* timer = gstsAdd("Timer");
  80. stAddVariable(timer, "seconds", dtInt());
  81. stAddVariable(timer, "nanos", dtInt());
  82. DataType timerType = dtToPointer(dtStruct(timer));
  83. Function f;
  84. gfInit(&f, "getMillis", dtVoid(), lGetMillis);
  85. gfAddArgument(&f, timerType);
  86. gfsAdd(&f);
  87. gfInit(&f, "getNanos", dtVoid(), lGetNanos);
  88. gfAddArgument(&f, timerType);
  89. gfsAdd(&f);
  90. Struct* time = gstsAdd("Time");
  91. stAddVariable(time, "year", dtInt());
  92. stAddVariable(time, "month", dtInt());
  93. stAddVariable(time, "day", dtInt());
  94. stAddVariable(time, "hours", dtInt());
  95. stAddVariable(time, "minutes", dtInt());
  96. stAddVariable(time, "seconds", dtInt());
  97. stAddVariable(time, "millis", dtInt());
  98. stAddVariable(time, "dst", dtInt());
  99. DataType timeType = dtToPointer(dtStruct(time));
  100. gfInit(&f, "toTime", dtVoid(), lToTime);
  101. gfAddArgument(&f, timerType);
  102. gfAddArgument(&f, timeType);
  103. gfsAdd(&f);
  104. gfInit(&f, "toMillis", dtVoid(), lToMillis);
  105. gfAddArgument(&f, timeType);
  106. gfAddArgument(&f, timerType);
  107. gfsAdd(&f);
  108. }