123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #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;
- }
- sPushInt64(sc, time.tv_nsec / 1000000L + time.tv_sec * 1000L);
- }
- static void lGetNanos(Script* sc) {
- struct timespec time;
- if(clock_gettime(CLOCK_BOOTTIME, &time)) {
- sError(sc, "cannot get clock time: %s", strerror(errno));
- return;
- }
- sPushInt64(sc, time.tv_nsec + time.tv_sec * 1000000000L);
- }
- typedef struct {
- int32 year;
- int32 month;
- int32 day;
- int32 hours;
- int32 minutes;
- int32 seconds;
- int32 millis;
- int32 dst;
- } Time;
- static void lToTime(Script* sc) {
- int64 millis;
- Pointer p;
- if(!sPopInt64(sc, &millis) || !sPopPointer(sc, &p)) {
- return;
- }
- Time* time = sCheckAddress(sc, &p, sizeof(Time));
- if(time == NULL) {
- return;
- }
- time_t t = millis / 1000l;
- struct tm* local = localtime(&t);
- if(local == NULL) {
- sError(sc, "cannot get local time: %s", strerror(errno));
- return;
- }
- time->year = local->tm_year + 1900;
- time->month = local->tm_mon;
- time->day = local->tm_mday;
- time->hours = local->tm_hour;
- time->minutes = local->tm_min;
- time->seconds = local->tm_sec;
- time->millis = millis % 1000;
- time->dst = local->tm_isdst;
- }
- static void lToMillis(Script* sc) {
- Pointer p;
- if(!sPopPointer(sc, &p)) {
- return;
- }
- Time* time = sCheckAddress(sc, &p, sizeof(Time));
- if(time == NULL) {
- return;
- }
- struct tm local;
- local.tm_year = time->year - 1900;
- local.tm_mon = time->month;
- local.tm_mday = time->day;
- local.tm_hour = time->hours;
- local.tm_min = time->minutes;
- local.tm_sec = time->seconds;
- local.tm_isdst = time->dst;
- int64 seconds = mktime(&local);
- if(seconds == -1) {
- sError(sc, "cannot convert time: %s", strerror(errno));
- return;
- }
- sPushInt64(sc, seconds * 1000L + time->millis);
- }
- void lTimeRegister() {
- Function f;
- gfInit(&f, "getMillis", dtInt64(), lGetMillis);
- gfsAdd(&f);
- gfInit(&f, "getNanos", dtInt64(), lGetNanos);
- gfsAdd(&f);
- Struct* time = gstsAdd("Time");
- stAddVariable(time, "year", dtInt32());
- stAddVariable(time, "month", dtInt32());
- stAddVariable(time, "day", dtInt32());
- stAddVariable(time, "hours", dtInt32());
- stAddVariable(time, "minutes", dtInt32());
- stAddVariable(time, "seconds", dtInt32());
- stAddVariable(time, "millis", dtInt32());
- stAddVariable(time, "dst", dtInt32());
- DataType timeType = dtStruct(time);
- dtDereference(&timeType);
- gfInit(&f, "toTime", dtVoid(), lToTime);
- gfAddArgument(&f, timeType);
- gfAddArgument(&f, dtInt64());
- gfsAdd(&f);
- gfInit(&f, "toMillis", dtInt64(), lToMillis);
- gfAddArgument(&f, timeType);
- gfsAdd(&f);
- }
|