Time.c 3.1 KB

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