| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 | 
							- #include <errno.h>
 
- #include <stdio.h>
 
- #include <string.h>
 
- #include <time.h>
 
- #include "libraries/Time.h"
 
- #include "utils/Functions.h"
 
- static void lGetMillis(Script* sc) {
 
-     struct timespec time;
 
-     if(clock_gettime(CLOCK_REALTIME, &time)) {
 
-         sError(sc, "cannot get clock time: %s", strerror(errno));
 
-         return;
 
-     }
 
-     Value* v = sPopStructPointer(sc, VT_INT);
 
-     if(v == NULL) {
 
-         return;
 
-     }
 
-     v[0].data.intValue = time.tv_sec;
 
-     v[1].data.intValue = (time.tv_nsec / 1000000) * 1000000;
 
- }
 
- static void lGetNanos(Script* sc) {
 
-     struct timespec time;
 
-     if(clock_gettime(CLOCK_BOOTTIME, &time)) {
 
-         sError(sc, "cannot get clock time: %s", strerror(errno));
 
-         return;
 
-     }
 
-     Value* v = sPopStructPointer(sc, VT_INT);
 
-     if(v == NULL) {
 
-         return;
 
-     }
 
-     v[0].data.intValue = time.tv_sec;
 
-     v[1].data.intValue = time.tv_nsec;
 
- }
 
- static void lToTime(Script* sc) {
 
-     Value* time = sPopStructPointer(sc, VT_INT);
 
-     Value* timer = sPopStructPointer(sc, VT_INT);
 
-     if(time == NULL || timer == NULL) {
 
-         return;
 
-     }
 
-     long millis =
 
-         timer[0].data.intValue * 1000l + timer[1].data.intValue / 1000000l;
 
-     time_t t = millis / 1000l;
 
-     struct tm* local = localtime(&t);
 
-     if(local == NULL) {
 
-         sError(sc, "cannot get local time: %s", strerror(errno));
 
-         return;
 
-     }
 
-     time[0].data.intValue = local->tm_year + 1900;
 
-     time[1].data.intValue = local->tm_mon;
 
-     time[2].data.intValue = local->tm_mday;
 
-     time[3].data.intValue = local->tm_hour;
 
-     time[4].data.intValue = local->tm_min;
 
-     time[5].data.intValue = local->tm_sec;
 
-     time[6].data.intValue = millis % 1000;
 
-     time[7].data.intValue = local->tm_isdst;
 
- }
 
- static void lToMillis(Script* sc) {
 
-     Value* timer = sPopStructPointer(sc, VT_INT);
 
-     Value* time = sPopStructPointer(sc, VT_INT);
 
-     if(time == NULL || timer == NULL) {
 
-         return;
 
-     }
 
-     struct tm local;
 
-     local.tm_year = time[0].data.intValue - 1900;
 
-     local.tm_mon = time[1].data.intValue;
 
-     local.tm_mday = time[2].data.intValue;
 
-     local.tm_hour = time[3].data.intValue;
 
-     local.tm_min = time[4].data.intValue;
 
-     local.tm_sec = time[5].data.intValue;
 
-     local.tm_isdst = time[7].data.intValue;
 
-     int64 seconds = mktime(&local);
 
-     if(seconds == -1) {
 
-         sError(sc, "cannot convert time: %s", strerror(errno));
 
-         return;
 
-     }
 
-     timer[0].data.intValue = seconds;
 
-     timer[1].data.intValue = time[6].data.intValue * 1000000;
 
- }
 
- void lTimeRegister() {
 
-     Struct* timer = gstsAdd("Timer");
 
-     stAddVariable(timer, "seconds", dtInt());
 
-     stAddVariable(timer, "nanos", dtInt());
 
-     DataType timerType = dtToPointer(dtStruct(timer));
 
-     Function f;
 
-     gfInit(&f, "getMillis", dtVoid(), lGetMillis);
 
-     gfAddArgument(&f, timerType);
 
-     gfsAdd(&f);
 
-     gfInit(&f, "getNanos", dtVoid(), lGetNanos);
 
-     gfAddArgument(&f, timerType);
 
-     gfsAdd(&f);
 
-     Struct* time = gstsAdd("Time");
 
-     stAddVariable(time, "year", dtInt());
 
-     stAddVariable(time, "month", dtInt());
 
-     stAddVariable(time, "day", dtInt());
 
-     stAddVariable(time, "hours", dtInt());
 
-     stAddVariable(time, "minutes", dtInt());
 
-     stAddVariable(time, "seconds", dtInt());
 
-     stAddVariable(time, "millis", dtInt());
 
-     stAddVariable(time, "dst", dtInt());
 
-     DataType timeType = dtToPointer(dtStruct(time));
 
-     gfInit(&f, "toTime", dtVoid(), lToTime);
 
-     gfAddArgument(&f, timerType);
 
-     gfAddArgument(&f, timeType);
 
-     gfsAdd(&f);
 
-     gfInit(&f, "toMillis", dtVoid(), lToMillis);
 
-     gfAddArgument(&f, timeType);
 
-     gfAddArgument(&f, timerType);
 
-     gfsAdd(&f);
 
- }
 
 
  |