76 lines
2.1 KiB
C
76 lines
2.1 KiB
C
// time_playdate.c - Time module for Playdate
|
|
|
|
#include "cell.h"
|
|
#include "pd_api.h"
|
|
|
|
// Global Playdate API pointers - defined in main_playdate.c
|
|
extern const struct playdate_sys *pd_sys;
|
|
|
|
/* ---------------------------------------------------------------- *\
|
|
Helpers
|
|
\* ---------------------------------------------------------------- */
|
|
|
|
static inline double playdate_now(void)
|
|
{
|
|
if (pd_sys) {
|
|
unsigned int ms = 0;
|
|
unsigned int secs = pd_sys->getSecondsSinceEpoch(&ms);
|
|
return (double)secs + ms / 1000.0;
|
|
}
|
|
return 0.0;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- *\
|
|
JS bindings
|
|
\* ---------------------------------------------------------------- */
|
|
|
|
static JSValue
|
|
js_time_now(JSContext *ctx, JSValueConst this_val,
|
|
int argc, JSValueConst *argv)
|
|
{
|
|
return JS_NewFloat64(ctx, playdate_now());
|
|
}
|
|
|
|
static JSValue
|
|
js_time_computer_dst(JSContext *ctx, JSValueConst this_val,
|
|
int argc, JSValueConst *argv)
|
|
{
|
|
// Playdate doesn't provide DST info directly
|
|
return JS_NewBool(ctx, 0);
|
|
}
|
|
|
|
static JSValue
|
|
js_time_computer_zone(JSContext *ctx, JSValueConst this_val,
|
|
int argc, JSValueConst *argv)
|
|
{
|
|
#if TARGET_EXTENSION
|
|
if (pd_sys) {
|
|
int32_t offset = pd_sys->getTimezoneOffset();
|
|
// Playdate returns offset in seconds, convert to hours
|
|
return JS_NewFloat64(ctx, (double)offset / 3600.0);
|
|
}
|
|
#endif
|
|
return JS_NewFloat64(ctx, 0.0);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- *\
|
|
registration
|
|
\* ---------------------------------------------------------------- */
|
|
|
|
static const JSCFunctionListEntry js_time_funcs[] = {
|
|
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;
|
|
}
|