Time.c 3.0 KB

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