From fa0442cce6569fcd7987a1d697c36e4c29c6de5a Mon Sep 17 00:00:00 2001 From: Xotic750 Date: Thu, 17 Dec 2015 02:16:26 +0100 Subject: [PATCH] util,test: Use consistent Date representation for util/inspect Re: https://github.com/nodejs/node/issues/4314 `Inspect` formats dates in two different ways, depending on whether it has properties or not. No properties and it uses `toString`. https://github.com/nodejs/node/blob/master/lib/util.js#L272 > The toString() method always returns a string representation of > the date in American English. While when properties are present it uses `toUTCString`. https://github.com/nodejs/node/blob/master/lib/util.js#L393 > The format of the return value may vary according to the platform. > The most common return value is a RFC-1123 formatted date stamp, > which is a slightly updated version of RFC-822 date stamps. I am wondering why two different representations are used? And being 2015, it would seem sensible to use `toISOString`. > The toISOString() method returns a string in simplified > extended ISO format (ISO 8601), which is always 24 characters > long: YYYY-MM-DDTHH:mm:ss.sssZ. The timezone is always zero > UTC offset, as denoted by the suffix "Z". ./configure make test Total errors found: 0 --- lib/util.js | 4 ++-- test/parallel/test-util-inspect.js | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/util.js b/lib/util.js index c1735e7c7c9cca..10376ff2f5c5ee 100644 --- a/lib/util.js +++ b/lib/util.js @@ -269,7 +269,7 @@ function formatValue(ctx, value, recurseTimes) { return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); } if (isDate(value)) { - return ctx.stylize(Date.prototype.toString.call(value), 'date'); + return ctx.stylize(Date.prototype.toISOString.call(value), 'date'); } if (isError(value)) { return formatError(value); @@ -390,7 +390,7 @@ function formatValue(ctx, value, recurseTimes) { // Make dates with properties first say the date if (isDate(value)) { - base = ' ' + Date.prototype.toUTCString.call(value); + base = ' ' + Date.prototype.toISOString.call(value); } // Make error with message first say the error diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 3a033eb3aac1a9..ca642868c9aae1 100644 --- a/test/parallel/test-util-inspect.js +++ b/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(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi'); 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'"); @@ -220,7 +220,7 @@ assert.equal(util.inspect(value), '{ /123/gi aprop: 42 }'); // Dates with properties value = new Date('Sun, 14 Feb 2010 11:48:40 GMT'); 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 @@ -313,6 +313,12 @@ assert.doesNotThrow(function() { util.inspect(d); }); +assert.doesNotThrow(function() { + var d = new Date(); + d.toISOString = null; + util.inspect(d); +}); + assert.doesNotThrow(function() { var r = /regexp/; r.toString = null;