M
Modular10mo ago
Aamir

Help Needed with Accessing High-Resolution Time via FFI in Mojo

Hello Mojo Community, I am currently working on developing a comprehensive datetime library for the Mojo platform, aimed at enhancing time-related functionalities. As part of this effort, I've encountered a limitation with the built-in time.now() function, which only returns a monotonic timestamp, providing elapsed time rather than the current wall clock time. To obtain the epoch timestamp, I successfully used the system's time function with the following Mojo code:
from sys.ffi import external_call

external_call["time", Int64]().to_int()
from sys.ffi import external_call

external_call["time", Int64]().to_int()
This works perfectly for obtaining the current time in seconds since the Unix epoch. However, my project requires time information with higher resolution, specifically, I need to access time in milliseconds or nanoseconds. To achieve this, I understand that I need to use Unix's clock_gettime function, which can provide higher resolution timestamps. However, I am relatively new to Mojo and still strengthening my basics, particularly in working with the FFI module. I am struggling to figure out how to correctly invoke clock_gettime using Mojo's FFI. Could anyone provide guidance or examples on how to properly call clock_gettime from within Mojo using the FFI module? Any tips or example code snippets would be greatly appreciated! Thank you in advance for your help and support!
2 Replies
gabrieldemarmiesse
Hi, you can find time.time_ns() implemented here: https://github.com/gabrieldemarmiesse/mojo-stdlib-extensions I hope it helps!
GitHub
GitHub - gabrieldemarmiesse/mojo-stdlib-extensions: A replica of py...
A replica of python's stdlib in mojo. Contribute to gabrieldemarmiesse/mojo-stdlib-extensions development by creating an account on GitHub.
Aamir
AamirOP10mo ago
Wow, it worked! Thanks for a quick response! 🙏🏼
Want results from more Discord servers?
Add your server