Implement system and monotonic clocks in Inko, and pass separately as seconds and nanoseconds #749
Labels
accepting contributions
Issues that are suitable to be worked on by anybody, not just maintainers
feature
New things to add to Inko, such as a new standard library module
runtime
Changes related to the Rust-based runtime library
std
Changes related to the standard library
DateTime
andInstant
use some Rust functions to get the system time and a monotonic clock time respectively. We should instead use Inko's C FFI and use the underlying C functions directly, allowing us to remove this logic from the Rust runtime library.In addition,
DateTime.from_timestamp
expects the timestamp as a single value: either anInt
without nano/sub seconds, or aFloat
that includes them. We should adjust this such that the seconds and nanoseconds are passed as two separateInt
values, ensuring there's no loss of precision.As part of this we might also have to change the implementation of
Instant
: right now it expresses the monotonic time as a singleInt
in nanoseconds, giving us a range of 292 years. Since this value is relative to when the program started, rather than some arbitrary point in time (e.g. the time at which the OS started), this isn't likely to overflow. However, if we use C directly we may not have a guarantee as to what the starting point is, and I don't want to implicitly depend on that being "recent enough". This means we might also have to changeInstant
to store both seconds and nanoseconds separately.Steps to take
First we'll port over the existing Rust code to the equivalent Inko code:
tzset()
when the runtime library initializes itself ininko_runtime_new
, so we don't need to call this every timeinko_time_system
with Inko/FFI code, usingclock_gettime
and initially returning the value as aFloat
(so basically what we do now)inko_time_system_offset
with a similar approachinko_time_monotonic
with a similar setup, returning a singleInt
(as we do now)Then we can refactor
DateTime
:fn pub static from_timestamp[T: ToFloat](time: ref T, utc_offset: Int) -> DateTime
tofn pub static from_timestamp(secs: Int, nanos: Int, utc_offset: Int) -> DateTime
and change the implementation accordinglyDateTime.sub_second
to store its value as anInt
(in nanoseconds) instead of aFloat
DateTime.to_float
, convertsub_second
to the number of seconds as aFloat
, then add that to the timestamp as a floatInt
instead of aFloat
Finally, we can do essentially the same for
Instant
.Note to potential contributors
While this change is open to contributions, it might be a bit of work especially for first-time contributors. I highly recommend submitting the changes in smaller chunks (e.g. only 1-2 steps at a time) instead of all at once. This makes it easier to review the code and give proper feedback.
The text was updated successfully, but these errors were encountered: