Context is my Calorie tracker project that needs to compile to WASM.
It’s only dependency is Sokol, which means that it uses libc and emscripten.
For native targets I was able to use libc to get the timezone time (AFAIK there’s no way in the zig standard library for this, if there’s a clean zig way to do it then I would be very glad to here it, as this is the only translate-C code that my app uses at all, so it would be nice to eliminate that dependency)
Either way - I could not figure out how to get the libc method to work on WASM which is what lead me here, the place where no problem leaves unsolved.
Wasm is a bytecode format that requires an embedding environment. By itself wasm has no access to anything external. Is this a web page? Wasi? Something else?
The plan is that I will have a fully functional version of the application embedded in the website that will be the store.
I wish I could give a more precise answer - but I don’t know much about the web. I had the fortune so far of typing zig build run -Dtarget=wasm32-emscripten and have the app “just work” if that makes sense.
After more tinkering with libc, I managed to make that one to work. I will post it here in case people have a similar problem in the future, because this seems like a very natural use case.
So basically, the main difference is that before this I used translate C to generate the function declarations. With the “fixed” method I didn’t do that, instead I declared everything extern.
Just talking generally - this is something that has happened to me I think for the third time while working on these, failing to make it work by trying to find a c header and succeeding by just declaring things extern.
const c = switch (builtin.os.tag) {
.windows => struct {
const tm = extern struct {
tm_sec: c_int,
tm_min: c_int,
tm_hour: c_int,
tm_mday: c_int,
tm_mon: c_int,
tm_year: c_int,
tm_wday: c_int,
tm_yday: c_int,
tm_isdst: c_int,
tm_gmtoff: c_long,
tm_zone: ?[*:0]const u8,
};
extern fn _time64(t: *i64) i64;
extern fn _localtime64_s(out: *tm, t: *const i64) c_int;
fn getLocalTime_tm() tm {
var rawTime: i64 = undefined;
if (_time64(&rawTime) == -1) @panic("libc _time64() failed");
var localTime: tm = undefined;
if (_localtime64_s(&localTime, &rawTime) != 0) @panic("libc _localtime64_s() failed");
return localTime;
}
},
else => struct {
const time_t = std.c.time_t;
comptime {
if (@sizeOf(time_t) < 8) @compileError("failed sanity check time_t is not 64-bit on this target (year 2038 problem)");
}
const tm = extern struct {
tm_sec: c_int,
tm_min: c_int,
tm_hour: c_int,
tm_mday: c_int,
tm_mon: c_int,
tm_year: c_int,
tm_wday: c_int,
tm_yday: c_int,
tm_isdst: c_int,
};
extern fn time(t: *time_t) time_t;
extern fn localtime_r(t: *const time_t, out: *tm) ?*tm;
fn getLocalTime_tm() tm {
var rawTime: time_t = undefined;
if (time(&rawTime) == -1) @panic("libc time() failed");
var localTime: tm = undefined;
if (localtime_r(&rawTime, &localTime) == null) @panic("libc localtime_r() failed");
return localTime;
}
},
};