How to get unix timestamp?
Hi, is there currently currently any API to get the unix timestamp (since unix epoch)? I read the docs for
time.now()
and it seems it only measures the time from the system boot.13 Replies
The standard library does not seem to provide that yet. An alternative is to call Python https://docs.python.org/3/library/time.html#time.time_ns from Mojo.
Python documentation
time — Time access and conversions
This module provides various time-related functions. For related functionality, see also the datetime and calendar modules. Although this module is always available, not all functions are available...
thanks I'm utilizing this for now
time._realtime_nanoseconds returns the timestamp in nanoseconds, but it is subject to adjustment. If you are fine being Linux only, time._gettime_as_nsec_unix(11) is not subject to daylight savings.
linux only will be okay too as long as it's possible to provide a fallback mechanism for other OSes via a compile-time parameter check...
https://docs.modular.com/mojo/stdlib/sys/info/os_is_linux
https://docs.modular.com/mojo/stdlib/sys/info/os_is_macos
https://docs.modular.com/mojo/stdlib/sys/info/os_is_windows
thanks for this, let me try it out
this seems to be working for macos as well...
m1 arm64
Double check the man pages
11 is CLOCK_TAI on Linux.
sorry, I used the
time._realtime_nanoseconds
you're correct, the _gettime_as_nsec_unix(11)
does not work in mac
I suppose, anything that starts with an underscore should be considered more experimental than other items?It technically means private
But the alternative is writing it all yourself
Mojo doesn't have a way to enforce API boundaries yet.
This is what I ended up with for the time being.
@sazid also check out https://github.com/thatstoasty/small-time a fork of https://github.com/mojoto/morrow.mojo , those are both libraries for better time/date ergonomics on Mojo
GitHub
GitHub - thatstoasty/small-time: Datetime library forked from morro...
Datetime library forked from morrow.mojo for my projects - thatstoasty/small-time
GitHub
GitHub - mojoto/morrow.mojo: Human-friendly date & time for Mojo 🔥
Human-friendly date & time for Mojo 🔥. Contribute to mojoto/morrow.mojo development by creating an account on GitHub.
looks great!
it looks like morrow internally calls into libc - does that mean the binaries built with this will be dynamic unless we compile against musl?
what's the difference between the two? more ergonomic calling into libc and the magic based project?
Currently Morrow is not up-to-date with the latest Mojo version and small_time also has a nightly branch that's up-to-date with currently nightly. Also I think @toasty made small_time a bit smaller than Morrow in terms of functionality
can't say for sure about binaries, maybe other folks know more
i see, thanks
Functionally, they’re very much the same. I did a bit of reorganization and cleanup of the code, but they both use libc calls in the same way.
There’s also a datetime module in the forge-tools repo that @Martin Vuyk put a lot of time into!