https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112017-09-19T23:49:50ZRuby Issue Tracking SystemRuby master - Feature #13919: Add a new method to create Time instances from unix time and nsechttps://redmine.ruby-lang.org/issues/13919?journal_id=667832017-09-19T23:49:50Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I have no objection to a new method at all.</p>
<p>I think the name "Time.of()" is strange though.</p>
<p>With Time.at(), we can say "ok, at this or that moment, we want the time".</p>
<p>With Time.of(), I am confused what that should mean. I am not a native<br>
english speaker though, so who knows.</p>
<p>Would Time.nsec be any worse? That is, it would want the time input in<br>
nanoseconds (nsec is nano seconds right?)</p>
<p>I agree that Time.at() is not very convenient with the second parameter<br>
mandating microsecond IF one wants to use nanoseconds instead. I think<br>
the reason why that was chosen, though, was because nsec may be less<br>
frequent than msec. Like hour-minutes-seconds notation, and then the<br>
milliseconds after a ',' or so.</p>
<p>Just putting some more ideas for names out there:</p>
<p>Time.at_nsec<br>
Time.at(t.to_i, nsec: t)<br>
Time.at(t.to_i, :nsec)</p>
<p>Hmm. Or perhaps be able to modify the default for Time so that you can<br>
switch it to nsec, like:</p>
<p>Time.default_to = :microseconds<br>
Time.default_to = :nanoseconds</p>
<p>Or anything like that. Just trying to find good alternatives, giving<br>
things a good name that can easily stick mentally is not always easy.</p> Ruby master - Feature #13919: Add a new method to create Time instances from unix time and nsechttps://redmine.ruby-lang.org/issues/13919?journal_id=667882017-09-20T06:50:18Ztagomoris (Satoshi Tagomori)tagomoris@gmail.com
<ul></ul><p><code>Time.at_nsec</code> is ok for me, but it seems to take just one argument of nano seconds from epoch.</p>
<ul>
<li><code>Time.at_nsec(nsec_from_epoch)</code></li>
<li><code>Time.at_nsec(seconds_from_epoch, nsec)</code></li>
</ul>
<p>Is it not confusing? Or should it get exactly two arguments?</p> Ruby master - Feature #13919: Add a new method to create Time instances from unix time and nsechttps://redmine.ruby-lang.org/issues/13919?journal_id=667962017-09-20T13:46:16Zakr (Akira Tanaka)akr@fsij.org
<ul></ul><p>I think new method is possible if we find a good method name.</p>
<p>Another idea is adding an optional argument, unit, for Time.at.<br>
Time.at(s, ns, :nanosecond) for example.</p> Ruby master - Feature #13919: Add a new method to create Time instances from unix time and nsechttps://redmine.ruby-lang.org/issues/13919?journal_id=668082017-09-20T21:31:07Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>Following is a patch based on akr's API.</p>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gh">diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb
index c92aafc149..31d245ade7 100644
</span><span class="gd">--- a/test/ruby/test_time.rb
</span><span class="gi">+++ b/test/ruby/test_time.rb
</span><span class="p">@@ -236,6 +236,17 @@</span> def test_at2
assert_equal(1, Time.at(0, 0.001).nsec)
end
<span class="gi">+ def test_at_with_unit
+ assert_equal(123456789, Time.at(0, 123456789, :nanosecond).nsec)
+ assert_equal(123456789, Time.at(0, 123456789, :nsec).nsec)
+ assert_equal(123456000, Time.at(0, 123456, :microsecond).nsec)
+ assert_equal(123456000, Time.at(0, 123456, :usec).nsec)
+ assert_equal(123456000, Time.at(0, 123456, nil).nsec)
+ assert_equal(123000000, Time.at(0, 123, :millisecond).nsec)
+ assert_raise(ArgumentError){ Time.at(0, 1, 2) }
+ assert_raise(ArgumentError){ Time.at(0, 1, :invalid) }
+ end
+
</span> def test_at_rational
assert_equal(1, Time.at(Rational(1,1) / 1000000000).nsec)
assert_equal(1, Time.at(1167609600 + Rational(1,1) / 1000000000).nsec)
<span class="gh">diff --git a/time.c b/time.c
index e55628fc1b..2431a2406a 100644
</span><span class="gd">--- a/time.c
</span><span class="gi">+++ b/time.c
</span><span class="p">@@ -36,6 +36,7 @@</span>
static ID id_divmod, id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
static ID id_quo, id_div;
<span class="gi">+static ID id_nanosecond, id_microsecond, id_millisecond, id_nsec, id_usec;
</span>
#define NDIV(x,y) (-(-((x)+1)/(y))-1)
#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
<span class="p">@@ -2347,11 +2348,32 @@</span> time_s_now(VALUE klass)
return rb_class_new_instance(0, NULL, klass);
}
<span class="gi">+static int
+get_scale(VALUE unit) {
+ if (unit == ID2SYM(id_nanosecond) || unit == ID2SYM(id_nsec)) {
+ return 1000000000;
+ }
+ else if (NIL_P(unit) || unit == ID2SYM(id_microsecond) || unit == ID2SYM(id_usec)) {
+ return 1000000;
+ }
+ else if (unit == ID2SYM(id_millisecond)) {
+ return 1000;
+ }
+ else {
+ rb_raise(rb_eArgError, "unexpected unit: %"PRIsVALUE, unit);
+ }
+}
+
</span> /*
* call-seq:
* Time.at(time) -> time
* Time.at(seconds_with_frac) -> time
* Time.at(seconds, microseconds_with_frac) -> time
<span class="gi">+ * Time.at(seconds, milliseconds, :millisecond) -> time
+ * Time.at(seconds, microseconds, :usec) -> time
+ * Time.at(seconds, microseconds, :microsecond) -> time
+ * Time.at(seconds, nanoseconds, :nsec) -> time
+ * Time.at(seconds, nanoseconds, :nanosecond) -> time
</span> *
* Creates a new Time object with the value given by +time+,
* the given number of +seconds_with_frac+, or
<span class="p">@@ -2362,24 +2384,26 @@</span> time_s_now(VALUE klass)
*
* If a numeric argument is given, the result is in local time.
*
<span class="gd">- * Time.at(0) #=> 1969-12-31 18:00:00 -0600
- * Time.at(Time.at(0)) #=> 1969-12-31 18:00:00 -0600
- * Time.at(946702800) #=> 1999-12-31 23:00:00 -0600
- * Time.at(-284061600) #=> 1960-12-31 00:00:00 -0600
- * Time.at(946684800.2).usec #=> 200000
- * Time.at(946684800, 123456.789).nsec #=> 123456789
</span><span class="gi">+ * Time.at(0) #=> 1969-12-31 18:00:00 -0600
+ * Time.at(Time.at(0)) #=> 1969-12-31 18:00:00 -0600
+ * Time.at(946702800) #=> 1999-12-31 23:00:00 -0600
+ * Time.at(-284061600) #=> 1960-12-31 00:00:00 -0600
+ * Time.at(946684800.2).usec #=> 200000
+ * Time.at(946684800, 123456.789).nsec #=> 123456789
+ * Time.at(946684800, 123456789, nsec).nsec #=> 123456789
</span> */
static VALUE
time_s_at(int argc, VALUE *argv, VALUE klass)
{
<span class="gd">- VALUE time, t;
</span><span class="gi">+ VALUE time, t, unit = Qnil;
</span> wideval_t timew;
<span class="gd">- if (rb_scan_args(argc, argv, "11", &time, &t) == 2) {
</span><span class="gi">+ if (rb_scan_args(argc, argv, "12", &time, &t, &unit) >= 2) {
+ int scale = get_scale(unit);
</span> time = num_exact(time);
t = num_exact(t);
<span class="gd">- timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, 1000000));
</span><span class="gi">+ timew = wadd(rb_time_magnify(v2w(time)), wmulquoll(v2w(t), TIME_SCALE, scale));
</span> t = time_new_timew(klass, timew);
}
else if (IsTimeval(time)) {
<span class="p">@@ -4819,6 +4843,11 @@</span> Init_Time(void)
id_nano_den = rb_intern("nano_den");
id_offset = rb_intern("offset");
id_zone = rb_intern("zone");
<span class="gi">+ id_nanosecond = rb_intern("nanosecond");
+ id_microsecond = rb_intern("microsecond");
+ id_millisecond = rb_intern("millisecond");
+ id_nsec = rb_intern("nsec");
+ id_usec = rb_intern("usec");
</span>
rb_cTime = rb_define_class("Time", rb_cObject);
rb_include_module(rb_cTime, rb_mComparable);
</code></pre> Ruby master - Feature #13919: Add a new method to create Time instances from unix time and nsechttps://redmine.ruby-lang.org/issues/13919?journal_id=668642017-09-25T05:51:28Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I agree with the new behavior (except for accepting <code>nil</code> as a unit).</p>
<p>Matz.</p> Ruby master - Feature #13919: Add a new method to create Time instances from unix time and nsechttps://redmine.ruby-lang.org/issues/13919?journal_id=668662017-09-25T06:20:22Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r60017.</p>
<hr>
<p>Time#at receives 3rd argument which specifies the unit of 2nd argument [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Add a new method to create Time instances from unix time and nsec (Closed)" href="https://redmine.ruby-lang.org/issues/13919">#13919</a>]</p>