Separate Time#inspect from to_s and show subsec [Feature #15958]

This commit is contained in:
NARUSE, Yui 2019-09-19 19:49:12 +09:00
Родитель 2698f13a1f
Коммит 5208c431be
3 изменённых файлов: 77 добавлений и 3 удалений

5
NEWS
Просмотреть файл

@ -202,6 +202,11 @@ Time::
* Added Time#floor method. [Feature #15653]
Modified method::
* Time#inspect is separated from Time#to_s and it shows its sub second.
[Feature #15958]
UnboundMethod::
New methods::

Просмотреть файл

@ -553,6 +553,30 @@ class TestTime < Test::Unit::TestCase
assert_equal(Time.at(946684800).getlocal.to_s, Time.at(946684800).to_s)
end
def test_inspect
t2000 = get_t2000
assert_equal("2000-01-01 00:00:00 UTC", t2000.inspect)
assert_equal(Encoding::US_ASCII, t2000.inspect.encoding)
assert_kind_of(String, Time.at(946684800).getlocal.inspect)
assert_equal(Time.at(946684800).getlocal.inspect, Time.at(946684800).inspect)
t2000 = get_t2000 + 1/10r
assert_equal("2000-01-01 00:00:00.1 UTC", t2000.inspect)
t2000 = get_t2000 + 1/1000000000r
assert_equal("2000-01-01 00:00:00.000000001 UTC", t2000.inspect)
t2000 = get_t2000 + 1/10000000000r
assert_equal("2000-01-01 00:00:00 1/10000000000 UTC", t2000.inspect)
t2000 = get_t2000 + 0.1
assert_equal("2000-01-01 00:00:00 3602879701896397/36028797018963968 UTC", t2000.inspect)
t2000 = get_t2000
t2000 = t2000.localtime(9*3600)
assert_equal("2000-01-01 09:00:00 +0900", t2000.inspect)
t2000 = get_t2000.localtime(9*3600) + 1/10r
assert_equal("2000-01-01 09:00:00.1 +0900", t2000.inspect)
end
def assert_zone_encoding(time)
zone = time.zone
assert_predicate(zone, :valid_encoding?)

51
time.c
Просмотреть файл

@ -4056,7 +4056,6 @@ time_asctime(VALUE time)
/*
* call-seq:
* time.inspect -> string
* time.to_s -> string
*
* Returns a string representing _time_. Equivalent to calling
@ -4082,6 +4081,52 @@ time_to_s(VALUE time)
return strftimev("%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding());
}
/*
* call-seq:
* time.inspect -> string
*
* Returns a detailed string representing _time_.
*
* t = Time.now
* t.to_s #=> "2012-11-10 18:16:12 +0100"
* t.strftime "%Y-%m-%d %H:%M:%S %z" #=> "2012-11-10 18:16:12 +0100"
*
* t.utc.to_s #=> "2012-11-10 17:16:12 UTC"
* t.strftime "%Y-%m-%d %H:%M:%S UTC" #=> "2012-11-10 17:16:12 UTC"
*/
static VALUE
time_inspect(VALUE time)
{
struct time_object *tobj;
VALUE str, subsec;
GetTimeval(time, tobj);
str = strftimev("%Y-%m-%d %H:%M:%S", time, rb_usascii_encoding());
subsec = wmod(tobj->timew, WINT2FIXWV(TIME_SCALE));
if (FIXNUM_P(subsec) && FIX2LONG(subsec) == 0) {
}
else if (FIXNUM_P(subsec) && FIX2LONG(subsec) < TIME_SCALE) {
long len;
str = rb_sprintf("%"PRIsVALUE".%09ld", str, FIX2LONG(subsec));
for (len=RSTRING_LEN(str); RSTRING_PTR(str)[len-1] == '0' && len > 0; len--)
;
rb_str_resize(str, len);
}
else {
rb_str_cat_cstr(str, " ");
subsec = quov(w2v(subsec), INT2FIX(TIME_SCALE));
rb_str_concat(str, rb_obj_as_string(subsec));
}
if (TZMODE_UTC_P(tobj)) {
rb_str_cat_cstr(str, " UTC");
}
else {
rb_str_concat(str, strftimev(" %z", time, rb_usascii_encoding()));
}
return str;
}
static VALUE
time_add0(VALUE klass, const struct time_object *tobj, VALUE torig, VALUE offset, int sign)
{
@ -5560,7 +5605,7 @@ Init_tm(VALUE outer, const char *name)
rb_define_method(tm, "utc?", time_utc_p, 0);
rb_define_method(tm, "gmt?", time_utc_p, 0);
rb_define_method(tm, "to_s", time_to_s, 0);
rb_define_method(tm, "inspect", time_to_s, 0);
rb_define_method(tm, "inspect", time_inspect, 0);
rb_define_method(tm, "to_a", time_to_a, 0);
rb_define_method(tm, "tv_sec", time_to_i, 0);
rb_define_method(tm, "tv_usec", time_usec, 0);
@ -5811,7 +5856,7 @@ Init_Time(void)
rb_define_method(rb_cTime, "ctime", time_asctime, 0);
rb_define_method(rb_cTime, "asctime", time_asctime, 0);
rb_define_method(rb_cTime, "to_s", time_to_s, 0);
rb_define_method(rb_cTime, "inspect", time_to_s, 0);
rb_define_method(rb_cTime, "inspect", time_inspect, 0);
rb_define_method(rb_cTime, "to_a", time_to_a, 0);
rb_define_method(rb_cTime, "+", time_plus, 1);