M
Modular14mo ago
sa-code

Calling mktime from mojo

I'm trying to call C's mktime from mojo but I seem to get a negative number. Here's the code, is the external call correct? mktime returns a time_t which should be a positive int, but I see a negative value. https://github.com/sa-/mo-time/blob/02cf8d1ccdd92ab1ca0863f1538d342d48b18f59/mo_time/date_time.mojo#L73
GitHub
mo-time/mo_time/date_time.mojo at 02cf8d1ccdd92ab1ca0863f1538d342d4...
It's time for a time pkg in 🔥 Mojo. Contribute to sa-/mo-time development by creating an account on GitHub.
9 Replies
sa-code
sa-codeOP14mo ago
GitHub
mo-time/mo_time/ctypes.mojo at 02cf8d1ccdd92ab1ca0863f1538d342d48b1...
It's time for a time pkg in 🔥 Mojo. Contribute to sa-/mo-time development by creating an account on GitHub.
Stole
Stole14mo ago
I haven't wrestled enough with interp, TBH. Definitely seems to be bad memory, the value changes every time. I think @lukas would be better equipped to help.
sa-code
sa-codeOP14mo ago
Right, I remember reading that register passable types don't have stable pointers. But on the other hand calling gmtime while passing a pointer to the register_passable type C_tm seems to work fine
sa-code
sa-codeOP14mo ago
Thanks for looking into it either way!
lukas
lukas14mo ago
Gist
mktime.mojo
GitHub Gist: instantly share code, notes, and snippets.
lukas
lukas14mo ago
It is gnarly, but the printed result is the same as the C version (modulo a compiler error that pops up after) Also makes use of the new kwargs that just dropped Might have to swap i32 for i64 hmmm. Well, feel free to experiment The biggest sharp edge right now with CAPIs is making sure the object of your pointer lives long enough considering eager destruction You can see how I get around it with my do_nothing methods in shims
sa-code
sa-codeOP14mo ago
Thank you very much for having a look! Gonna test this out Since I cannot buy you a beer, please accept this e-beer instead 🍺
Want results from more Discord servers?
Add your server