From 6d1527bb37a20ffb6cc5ad8e56a6f5c1a8ccecf4 Mon Sep 17 00:00:00 2001 From: Rumkin Date: Sun, 1 May 2016 15:44:46 +0000 Subject: [PATCH] util: fix invalid date output with util.inspect Prevent util.inspect of throwing on date object with invalid date value. It changed to output result of toString method call. PR-URL: https://github.com/nodejs/node/pull/6504 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Evan Lucas Reviewed-By: Anna Henningsen --- lib/util.js | 6 +++++- test/parallel/test-util-inspect.js | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index d430bfd23d..aa592eb41f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -348,7 +348,11 @@ function formatValue(ctx, value, recurseTimes) { return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); } if (isDate(value)) { - return ctx.stylize(Date.prototype.toISOString.call(value), 'date'); + if (Number.isNaN(value.getTime())) { + return ctx.stylize(value.toString(), 'date'); + } else { + return ctx.stylize(Date.prototype.toISOString.call(value), 'date'); + } } if (isError(value)) { return formatError(value); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index d2af5014b2..4e1d5a2d1b 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -12,8 +12,9 @@ assert.equal(util.inspect(function() {}), '[Function]'); assert.equal(util.inspect(undefined), 'undefined'); assert.equal(util.inspect(null), 'null'); 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.strictEqual(util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')), new Date('2010-02-14T12:48:40+01:00').toISOString()); +assert.strictEqual(util.inspect(new Date('')), (new Date('')).toString()); assert.equal(util.inspect('\n\u0001'), "'\\n\\u0001'");