M
Modular2mo ago
sazid

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
Gerald Scharitzer
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...
sazid
sazidOP2mo ago
thanks I'm utilizing this for now
Darkmatter
Darkmatter2mo ago
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.
sazid
sazidOP2mo ago
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
Darkmatter
Darkmatter2mo ago
Double check the man pages 11 is CLOCK_TAI on Linux.
sazid
sazidOP2mo ago
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?
Darkmatter
Darkmatter2mo ago
It technically means private But the alternative is writing it all yourself Mojo doesn't have a way to enforce API boundaries yet.
sazid
sazidOP2mo ago
fn time_ms() raises -> Int64:
@parameter
if os_is_linux():
time_ns = Int64(time._gettime_as_nsec_unix(11))
else:
var time = Python.import_module("time")
time_ns = Int64(time.time_ns())

var time_ms = time_ns // 1_000_000
return time_ms
fn time_ms() raises -> Int64:
@parameter
if os_is_linux():
time_ns = Int64(time._gettime_as_nsec_unix(11))
else:
var time = Python.import_module("time")
time_ns = Int64(time.time_ns())

var time_ms = time_ns // 1_000_000
return time_ms
This is what I ended up with for the time being.
a2svior
a2svior2mo ago
@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.
sazid
sazidOP2mo ago
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?
a2svior
a2svior2mo ago
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
sazid
sazidOP2mo ago
i see, thanks
toasty
toasty2mo ago
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!
Want results from more Discord servers?
Add your server