ModularM
Modular2y ago
2 replies
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()

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!
Was this page helpful?