Project

General

Profile

Feature #8640 ยป elapsed.patch

tenderlovemaking (Aaron Patterson), 07/16/2013 03:39 AM

View differences:

test/ruby/test_time.rb
40 40
    assert_equal([2001,2,28,23,59,30,-43200], [t.year, t.month, t.mday, t.hour, t.min, t.sec, t.gmt_offset], bug4090)
41 41
  end
42 42

  
43
  def test_elapsed
44
    assert_operator Time.now.elapsed, :>, 0
45
    now = Time.now
46
    lap1 = now.elapsed
47
    lap2 = now.elapsed
48
    assert_operator lap2, :>, lap1
49
  end
50

  
43 51
  def test_time_add()
44 52
    assert_equal(Time.utc(2000, 3, 21, 3, 30) + 3 * 3600,
45 53
                 Time.utc(2000, 3, 21, 6, 30))
time.c
3726 3726
}
3727 3727

  
3728 3728
/*
3729
 *  call-seq:
3730
 *     time.elapsed  -> integer
3731
 *
3732
 *  Elapsed amount of time (in nanoseconds) since _time_ was created.
3733
 *
3734
 *     t = Time.now       #=> 2007-11-19 08:23:10 -0600
3735
 *     t.elapsed          #=> 3022418000
3736
 */
3737

  
3738
static VALUE
3739
time_elapsed(VALUE self)
3740
{
3741
    struct time_object *tobj;
3742
    struct timespec ts;
3743
    wideval_t timew;
3744

  
3745
    GetTimeval(self, tobj);
3746

  
3747
#ifdef HAVE_CLOCK_GETTIME
3748
    if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
3749
	rb_sys_fail("clock_gettime");
3750
    }
3751
#else
3752
    {
3753
        struct timeval tv;
3754
        if (gettimeofday(&tv, 0) < 0) {
3755
            rb_sys_fail("gettimeofday");
3756
        }
3757
        ts.tv_sec = tv.tv_sec;
3758
        ts.tv_nsec = tv.tv_usec * 1000;
3759
    }
3760
#endif
3761

  
3762
    timew = timespec2timew(&ts);
3763

  
3764
    return rb_to_int(w2v(wsub(timew, tobj->timew)));
3765
}
3766

  
3767
/*
3729 3768
 * call-seq:
3730 3769
 *   time.succ   -> new_time
3731 3770
 *
......
4947 4986

  
4948 4987
    rb_define_method(rb_cTime, "+", time_plus, 1);
4949 4988
    rb_define_method(rb_cTime, "-", time_minus, 1);
4989
    rb_define_method(rb_cTime, "elapsed", time_elapsed, 0);
4950 4990

  
4951 4991
    rb_define_method(rb_cTime, "succ", time_succ, 0);
4952 4992
    rb_define_method(rb_cTime, "round", time_round, -1);