Ruby and monotonic time

Is it unheard of that class singleton method does not return class in
core?
Do we need monotonic time?
Can we reasonably provide it to multiple OS?
In which class should it appear?

I feel pretty much 100% of timers in every code are broken, because
UTC and consequently unixtime is not monotonic today, it might be in
few years time.
Some languages offer nice timestamp function to monotonic time, ruby
does not.

Ghetto example of implementation:

[ytti@lintukoto ~/tmp/ruby-2.1.2]% git diff|cat
diff --git a/time.c b/time.c
index 878b7ed…2e5443b 100644
— a/time.c
+++ b/time.c
@@ -3132,6 +3132,13 @@ time_s_mktime(int argc, VALUE *argv, VALUE klass)
return time_utc_or_local(argc, argv, FALSE, klass);
}

+static VALUE
+time_s_monotonic(VALUE klass) {

  • struct timespec ts;
  • clock_gettime(CLOCK_MONOTONIC, &ts);
  • return UINT64toNUM(((long long unsigned) ts.tv_sec*1000000000) +
    (long long unsigned) ts.tv_nsec);
    +}

/*

  • call-seq:
  • time.to_i   -> int
    

@@ -4939,6 +4946,7 @@ Init_Time(void)
rb_define_singleton_method(rb_cTime, “gm”, time_s_mkutc, -1);
rb_define_singleton_method(rb_cTime, “local”, time_s_mktime, -1);
rb_define_singleton_method(rb_cTime, “mktime”, time_s_mktime, -1);

  • rb_define_singleton_method(rb_cTime, “monotonic”, time_s_monotonic,
    -1);

    rb_define_method(rb_cTime, “to_i”, time_to_i, 0);
    rb_define_method(rb_cTime, “to_f”, time_to_f, 0);
    [ytti@lintukoto ~/tmp/ruby-2.1.2]% ./miniruby -e ‘p
    Time.monotonic’;uptime
    1602029246189
    09:19:26 up 26 min, 6 users, load average: 0,22, 0,31, 0,23
    [ytti@lintukoto ~/tmp/ruby-2.1.2]%

On 15 August 2014 09:57, Tony A. [email protected] wrote:

GitHub - copiousfreetime/hitimes: a fast, high resolution timer library for recording performance metrics
GitHub - socketry/timers: Pure Ruby timers collections suitable for use with event loops

But should this require external library? Or is monotonic time
fundamental requirement which should exist in core?

Saku Y. [email protected] wrote:

Some languages offer nice timestamp function to monotonic time, ruby does not.

Yes, we have Process.clock_gettime in Ruby 2.1+
It supports POSIX and (from reading the code) OSX, at least.

On 15 August 2014 12:48, Eric W. [email protected] wrote:

Yes, we have Process.clock_gettime in Ruby 2.1+
It supports POSIX and (from reading the code) OSX, at least.

Thanks it does work in OSX, just tried. But really it’s not very
portable, I think it’s pretty crucial that language offers function
like this
https://github.com/rust-lang/rust/blob/master/src/libtime/lib.rs#L146
but granted personally I’m never on Windows, so for me
Process.clock_gettime is good enough.

For general use, I think the need for this is so common, it really
should be core’s responsibility to offer portable solution for typical
problem.

Use Codemonkey. Tdk is 3rd BIOS with the OSXT.


From: ruby-talk [email protected] on behalf of Saku Y.
[email protected]
Sent: Friday, August 15, 2014 5:09 AM
To: Ruby users
Subject: Re: Ruby and monotonic time

On 15 August 2014 12:48, Eric W. [email protected] wrote:

Yes, we have Process.clock_gettime in Ruby 2.1+
It supports POSIX and (from reading the code) OSX, at least.

Thanks it does work in OSX, just tried. But really it’s not very
portable, I think it’s pretty crucial that language offers function
like this
https://github.com/rust-lang/rust/blob/master/src/libtime/lib.rs#L146
but granted personally I’m never on Windows, so for me
Process.clock_gettime is good enough.

For general use, I think the need for this is so common, it really
should be core’s responsibility to offer portable solution for typical
problem.