Project

General

Profile

Feature #10544 ยป patch.diff

Time#to_i(:millisecond) implementation - Glass_saga (Masaki Matsushita), 11/26/2014 01:46 AM

View differences:

test/ruby/test_time.rb
1032 1032
    }
1033 1033
  end
1034 1034

  
1035
  def test_to_i_unit
1036
    t = Time.utc(2040, 1, 1, 0, 0, 0)
1037
    assert_equal t.to_i, t.to_i(:second)
1038

  
1039
    milliseconds = (t.to_i * 1000) + (t.usec / 1000)
1040
    assert_equal milliseconds, t.to_i(:millisecond)
1041

  
1042
    assert_raise(ArgumentError) do
1043
      t.to_i(:hoge)
1044
    end
1045
  end
1035 1046
end
time.c
33 33

  
34 34
static ID id_divmod, id_mul, id_submicro, id_nano_num, id_nano_den, id_offset, id_zone;
35 35
static ID id_eq, id_ne, id_quo, id_div, id_cmp;
36
static VALUE sym_SECOND, sym_MILLISECOND;
36 37

  
37 38
#define NDIV(x,y) (-(-((x)+1)/(y))-1)
38 39
#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
......
3159 3160
 */
3160 3161

  
3161 3162
static VALUE
3162
time_to_i(VALUE time)
3163
time_to_i(int argc, VALUE *argv, VALUE time)
3163 3164
{
3164 3165
    struct time_object *tobj;
3165

  
3166 3166
    GetTimeval(time, tobj);
3167

  
3168
    if (argc > 0) {
3169
	VALUE unit = argv[0];
3170

  
3171
	if (unit == sym_SECOND) {
3172
	    goto second;
3173
	}
3174
	else if (unit == sym_MILLISECOND) {
3175
	    return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE / 1000)));
3176
	}
3177
	else {
3178
	    rb_raise(rb_eArgError, "invalid unit specified");
3179
	}
3180
    }
3181

  
3182
  second:
3167 3183
    return w2v(wdiv(tobj->timew, WINT2FIXWV(TIME_SCALE)));
3168 3184
}
3169 3185

  
3186
static VALUE
3187
time_tv_sec(VALUE time)
3188
{
3189
    return time_to_i(0, NULL, time);
3190
}
3191

  
3170 3192
/*
3171 3193
 *  call-seq:
3172 3194
 *     time.to_f -> float
......
4967 4989
    rb_define_singleton_method(rb_cTime, "local", time_s_mktime, -1);
4968 4990
    rb_define_singleton_method(rb_cTime, "mktime", time_s_mktime, -1);
4969 4991

  
4970
    rb_define_method(rb_cTime, "to_i", time_to_i, 0);
4992
    rb_define_method(rb_cTime, "to_i", time_to_i, -1);
4971 4993
    rb_define_method(rb_cTime, "to_f", time_to_f, 0);
4972 4994
    rb_define_method(rb_cTime, "to_r", time_to_r, 0);
4973 4995
    rb_define_method(rb_cTime, "<=>", time_cmp, 1);
......
5023 5045
    rb_define_method(rb_cTime, "friday?", time_friday, 0);
5024 5046
    rb_define_method(rb_cTime, "saturday?", time_saturday, 0);
5025 5047

  
5026
    rb_define_method(rb_cTime, "tv_sec", time_to_i, 0);
5048
    rb_define_method(rb_cTime, "tv_sec", time_tv_sec, 0);
5027 5049
    rb_define_method(rb_cTime, "tv_usec", time_usec, 0);
5028 5050
    rb_define_method(rb_cTime, "usec", time_usec, 0);
5029 5051
    rb_define_method(rb_cTime, "tv_nsec", time_nsec, 0);
......
5044 5066
#ifdef DEBUG_FIND_TIME_NUMGUESS
5045 5067
    rb_define_virtual_variable("$find_time_numguess", find_time_numguess_getter, NULL);
5046 5068
#endif
5069

  
5070
    sym_SECOND = ID2SYM(rb_intern("second"));
5071
    sym_MILLISECOND = ID2SYM(rb_intern("millisecond"));
5047 5072
}