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#L73GitHub
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
Here's a reference to mktime: https://en.cppreference.com/w/cpp/chrono/c/mktime
And here's my implementation of the
tm
struct: https://github.com/sa-/mo-time/blob/02cf8d1ccdd92ab1ca0863f1538d342d48b18f59/mo_time/ctypes.mojo#L37 which works fine with gmtime
which is called over here: https://github.com/sa-/mo-time/blob/02cf8d1ccdd92ab1ca0863f1538d342d48b18f59/mo_time/ctypes.mojo#L67GitHub
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.
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.
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 fineThanks for looking into it either way!
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 shimsThank you very much for having a look!
Gonna test this out
Since I cannot buy you a beer, please accept this e-beer instead 🍺