Forum: Ruby Ruby and monotonic time

Df0876ee40fb4c3e6b825983471de259?d=identicon&s=25 Saku Ytti (Guest)
on 2014-08-15 08:23
(Received via mailing list)
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]%
4131d2f57a0db2a2b4d9a62bd389fd44?d=identicon&s=25 Tony Arcieri (Guest)
on 2014-08-15 08:58
(Received via mailing list)
Df0876ee40fb4c3e6b825983471de259?d=identicon&s=25 Saku Ytti (Guest)
on 2014-08-15 09:27
(Received via mailing list)
On 15 August 2014 09:57, Tony Arcieri <bascule@gmail.com> wrote:

> https://github.com/copiousfreetime/hitimes
> https://github.com/celluloid/timers

But should this require external library? Or is monotonic time
fundamental requirement which should exist in core?
18813f71506ebad74179bf8c5a136696?d=identicon&s=25 Eric Wong (Guest)
on 2014-08-15 11:49
(Received via mailing list)
Saku Ytti <saku@ytti.fi> 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.
Df0876ee40fb4c3e6b825983471de259?d=identicon&s=25 Saku Ytti (Guest)
on 2014-08-15 13:09
(Received via mailing list)
On 15 August 2014 12:48, Eric Wong <normalperson@yhbt.net> 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/...
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.
3933dc21a448bdfebdba4b769e643706?d=identicon&s=25 Brandon Ericsson (Guest)
on 2014-08-15 17:18
(Received via mailing list)
Use Codemonkey.  Tdk is 3rd BIOS with the OSXT.
________________________________________
From: ruby-talk <ruby-talk-bounces@ruby-lang.org> on behalf of Saku Ytti
<saku@ytti.fi>
Sent: Friday, August 15, 2014 5:09 AM
To: Ruby users
Subject: Re: Ruby and monotonic time

On 15 August 2014 12:48, Eric Wong <normalperson@yhbt.net> 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/...
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.