M
Modular•14mo ago
Jake Brooks

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
Stole
Stole•14mo ago
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.
ModularBot
ModularBot•14mo ago
Congrats @Stole, you just advanced to level 9!
Jake Brooks
Jake BrooksOP•14mo ago
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?
sa-code
sa-code•14mo ago
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
sa-code
sa-code•14mo ago
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 it
Jake Brooks
Jake BrooksOP•14mo ago
That's perfect, I'll have a look more into it now I know where to look. Thank you!
sa-code
sa-code•14mo ago
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.
sa-code
sa-code•14mo ago
You can use DateTimeLocal.now_utc() to get what you want 🙂 I did it so you don't have to
Stole
Stole•14mo ago
Whoops, sorry I didn't see this till now. sa-code's got it, though 😉
sa-code
sa-code•14mo ago
I've also just added DateTimeLocal.now() which will get your system's local time
Want results from more Discord servers?
Add your server