Possible to get a meaningful time value?
Hi all. I'm wondering if it's possible to get a more meaningful time value than the existing time.now(), without going through Python or C.
From my research it seems like you would have to interface with the OS, but I'm not sure Mojo has the necessary functionality to do that yet. Is it possible to get something like a Unix time? If so, could someone point me in the right direction?
10 Replies
It's quite possible to interface with the OS to a good extent, through
external_call
(https://docs.modular.com/mojo/stdlib/sys/intrinsics.html#external_call). I think https://github.com/sa-/mo-time might have what you're looking for.GitHub
GitHub - sa-/mo-time: It's time for a time pkg in 🔥 Mojo
It's time for a time pkg in 🔥 Mojo. Contribute to sa-/mo-time development by creating an account on GitHub.
Congrats @Stole, you just advanced to level 9!
Ah that's bang on the money, thank you! If you don't mind me picking your brain a little more... I thought
external_call
might be what I was looking for, but I was unsure what exactly the docs meant by 'external function'. Is there a way of knowing what information to pass to external_call
and what you'll receive from it? For example, the repo you linked calls clock_gettime
. How does Mojo know where to find this function?
I believe the repo belongs to @sa-code, perhaps you can explain?Sure, "external call" means you're calling C. The documentation here gives you a clue but assumes that you know a few conventions https://docs.modular.com/mojo/stdlib/sys/intrinsics.html#external_call
You can pass in the name of any function, and you have to specify the types of of parameters and return types. If you've never done this sort of thing before I would recommend doing this exercise in Julia first! There is likely to be a lot more documentation there
Right now it supports the stdlib in C. If you google
clock_gettime
you will find out all about itThat's perfect, I'll have a look more into it now I know where to look. Thank you!
Also I just updated https://github.com/sa-/mo-time/tree/main
GitHub
GitHub - sa-/mo-time: It's time for a time pkg in 🔥 Mojo
It's time for a time pkg in 🔥 Mojo. Contribute to sa-/mo-time development by creating an account on GitHub.
You can use
DateTimeLocal.now_utc()
to get what you want 🙂
I did it so you don't have toWhoops, sorry I didn't see this till now. sa-code's got it, though 😉
I've also just added
DateTimeLocal.now()
which will get your system's local time