instance method
Time#inspect
() -> ::String
inspect → string
time.c
View on GitHub ↗
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 = w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE)));
if (subsec == INT2FIX(0)) {
}
else if (FIXNUM_P(subsec) && FIX2LONG(subsec) < TIME_SCALE) {
long len;
rb_str_catf(str, ".%09ld", 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(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 {
/* ?TODO: subsecond offset */
long off = NUM2LONG(rb_funcall(tobj->vtm.utc_offset, rb_intern("round"), 0));
char sign = (off < 0) ? (off = -off, '-') : '+';
int sec = off % 60;
int min = (off /= 60) % 60;
off /= 60;
rb_str_catf(str, " %c%.2d%.2d", sign, (int)off, min);
if (sec) rb_str_catf(str, "%.2d", sec);
}
return str;
}
Returns a string representation of self with subseconds:
Example
t = Time.new(2000, 12, 31, 23, 59, 59, 0.5) t.inspect # => "2000-12-31 23:59:59.5 +000001"
Related: Time#ctime, Time#to_s:
Example
t.ctime # => "Sun Dec 31 23:59:59 2000" t.to_s # => "2000-12-31 23:59:59 +0000"
Ruby version changes
-
3.2
-
Time.newnow can parse a string like generated byTime#inspectand return aTimeinstance based on the given argument. [Feature #18033]
-