Some checks failed
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / build-linux (push) Has been cancelled
46 lines
1.6 KiB
C
46 lines
1.6 KiB
C
#include "qjs_time.h"
|
|
#include "quickjs.h"
|
|
#include <time.h> // For time() calls, localtime, etc.
|
|
#include <sys/time.h> // For gettimeofday, if needed
|
|
|
|
// Example stubs for your time-related calls
|
|
|
|
static JSValue js_time_now(JSContext *ctx, JSValueConst this_val,
|
|
int argc, JSValueConst *argv) {
|
|
// time_now
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
double now = (double)tv.tv_sec + (tv.tv_usec / 1000000.0);
|
|
return JS_NewFloat64(ctx, now);
|
|
}
|
|
|
|
static JSValue js_time_computer_dst(JSContext *ctx, JSValueConst this_val,
|
|
int argc, JSValueConst *argv) {
|
|
time_t t = time(NULL);
|
|
struct tm *lt = localtime(&t);
|
|
int is_dst = (lt ? lt->tm_isdst : -1);
|
|
return JS_NewBool(ctx, (is_dst > 0));
|
|
}
|
|
|
|
static JSValue js_time_computer_zone(JSContext *ctx, JSValueConst this_val,
|
|
int argc, JSValueConst *argv) {
|
|
time_t t = time(NULL);
|
|
time_t local_t = mktime(localtime(&t));
|
|
double diff = difftime(t, local_t); // difference in seconds from local time
|
|
return JS_NewFloat64(ctx, diff / 3600.0);
|
|
}
|
|
|
|
static const JSCFunctionListEntry js_time_funcs[] = {
|
|
// name, prop flags, #args, etc.
|
|
JS_CFUNC_DEF("now", 0, js_time_now),
|
|
JS_CFUNC_DEF("computer_dst", 0, js_time_computer_dst),
|
|
JS_CFUNC_DEF("computer_zone", 0, js_time_computer_zone),
|
|
};
|
|
|
|
JSValue js_time_use(JSContext *ctx) {
|
|
JSValue obj = JS_NewObject(ctx);
|
|
JS_SetPropertyFunctionList(ctx, obj, js_time_funcs,
|
|
sizeof(js_time_funcs)/sizeof(js_time_funcs[0]));
|
|
return obj;
|
|
}
|