#include #include #include #include #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 = (int)time.tv_sec; v[1].data.intValue = (int)((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 = (int)time.tv_sec; v[1].data.intValue = (int)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 = (int)(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 = (int)seconds; timer[1].data.intValue = time[6].data.intValue * 1000000; } void lTimeRegister(void) { 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); }