# Getting Timezone aware time in WASM

**URL:** <https://ziggit.dev/t/getting-timezone-aware-time-in-wasm/18067>\
**Category:** Help\
**Tags:** wasm\
**Created:** [October 23, 2026, 9:03pm UTC](https://ziggit.dev/t/getting-timezone-aware-time-in-wasm/18067 "2026-10-23T21:03:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Plebosaur](https://ziggit.dev/user_avatar/ziggit.dev/plebosaur/32/6857_2.png) [@Plebosaur](https://ziggit.dev/u/Plebosaur)\
**Post date:** [October 23, 2026, 9:03pm UTC](https://ziggit.dev/t/getting-timezone-aware-time-in-wasm/18067/1 "2026-10-23T21:03:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ScottRedig](https://ziggit.dev/user_avatar/ziggit.dev/scottredig/32/5880_2.png) [@ScottRedig](https://ziggit.dev/u/ScottRedig)\
**Post date:** [October 23, 2026, 9:21pm UTC](https://ziggit.dev/t/getting-timezone-aware-time-in-wasm/18067/2 "2026-10-23T21:21:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Plebosaur](https://ziggit.dev/user_avatar/ziggit.dev/plebosaur/32/6857_2.png) [@Plebosaur](https://ziggit.dev/u/Plebosaur)\
**Post date:** [October 23, 2026, 9:24pm UTC](https://ziggit.dev/t/getting-timezone-aware-time-in-wasm/18067/3 "2026-10-23T21:24:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Plebosaur](https://ziggit.dev/user_avatar/ziggit.dev/plebosaur/32/6857_2.png) [@Plebosaur](https://ziggit.dev/u/Plebosaur)\
**Post date:** [October 24, 2026, 4:29pm UTC](https://ziggit.dev/t/getting-timezone-aware-time-in-wasm/18067/4 "2026-10-24T16:29:29Z")

</div>

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.

```zig
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;
        }
    },
};

```
