Browse Source

util: use consistent Dates in inspect()

Fix: https://github.com/nodejs/node/issues/4314
PR-URL: https://github.com/nodejs/node/pull/4318
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
process-exit-stdio-flushing
Xotic750 9 years ago
committed by cjihrig
parent
commit
93d6b5fb68
  1. 4
      lib/util.js
  2. 10
      test/parallel/test-util-inspect.js

4
lib/util.js

@ -269,7 +269,7 @@ function formatValue(ctx, value, recurseTimes) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
} }
if (isDate(value)) { if (isDate(value)) {
return ctx.stylize(Date.prototype.toString.call(value), 'date'); return ctx.stylize(Date.prototype.toISOString.call(value), 'date');
} }
if (isError(value)) { if (isError(value)) {
return formatError(value); return formatError(value);
@ -390,7 +390,7 @@ function formatValue(ctx, value, recurseTimes) {
// Make dates with properties first say the date // Make dates with properties first say the date
if (isDate(value)) { if (isDate(value)) {
base = ' ' + Date.prototype.toUTCString.call(value); base = ' ' + Date.prototype.toISOString.call(value);
} }
// Make error with message first say the error // Make error with message first say the error

10
test/parallel/test-util-inspect.js

@ -13,7 +13,7 @@ assert.equal(util.inspect(undefined), 'undefined');
assert.equal(util.inspect(null), 'null'); assert.equal(util.inspect(null), 'null');
assert.equal(util.inspect(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi'); assert.equal(util.inspect(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi');
assert.equal(util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')), assert.equal(util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')),
new Date('2010-02-14T12:48:40+01:00').toString()); new Date('2010-02-14T12:48:40+01:00').toISOString());
assert.equal(util.inspect('\n\u0001'), "'\\n\\u0001'"); assert.equal(util.inspect('\n\u0001'), "'\\n\\u0001'");
@ -220,7 +220,7 @@ assert.equal(util.inspect(value), '{ /123/gi aprop: 42 }');
// Dates with properties // Dates with properties
value = new Date('Sun, 14 Feb 2010 11:48:40 GMT'); value = new Date('Sun, 14 Feb 2010 11:48:40 GMT');
value.aprop = 42; value.aprop = 42;
assert.equal(util.inspect(value), '{ Sun, 14 Feb 2010 11:48:40 GMT aprop: 42 }' assert.equal(util.inspect(value), '{ 2010-02-14T11:48:40.000Z aprop: 42 }'
); );
// test the internal isDate implementation // test the internal isDate implementation
@ -313,6 +313,12 @@ assert.doesNotThrow(function() {
util.inspect(d); util.inspect(d);
}); });
assert.doesNotThrow(function() {
var d = new Date();
d.toISOString = null;
util.inspect(d);
});
assert.doesNotThrow(function() { assert.doesNotThrow(function() {
var r = /regexp/; var r = /regexp/;
r.toString = null; r.toString = null;

Loading…
Cancel
Save