From 5614c18624ba0e7e0b608d53603b9914334f603e Mon Sep 17 00:00:00 2001 From: Tolterix Date: Mon, 15 Feb 2021 11:27:41 -0600 Subject: [PATCH] Timestamp UTC Standardization for Migrations (#4245) Co-authored-by: Igor Savin --- lib/migrations/util/timestamp.js | 16 +++++++--------- test/unit/migrations/util/timestamp.js | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 test/unit/migrations/util/timestamp.js diff --git a/lib/migrations/util/timestamp.js b/lib/migrations/util/timestamp.js index 637086aa8d..bc145ecd4c 100644 --- a/lib/migrations/util/timestamp.js +++ b/lib/migrations/util/timestamp.js @@ -1,15 +1,13 @@ -// Get a date object in the correct format, without requiring a full out library -// like "moment.js". function yyyymmddhhmmss() { - const d = new Date(); + const now = new Date(); return ( - d.getFullYear().toString() + - (d.getMonth() + 1).toString().padStart(2, '0') + - d.getDate().toString().padStart(2, '0') + - d.getHours().toString().padStart(2, '0') + - d.getMinutes().toString().padStart(2, '0') + - d.getSeconds().toString().padStart(2, '0') + now.getUTCFullYear().toString() + + (now.getUTCMonth() + 1).toString().padStart(2, '0') + + now.getUTCDate().toString().padStart(2, '0') + + now.getUTCHours().toString().padStart(2, '0') + + now.getUTCMinutes().toString().padStart(2, '0') + + now.getUTCSeconds().toString().padStart(2, '0') ); } diff --git a/test/unit/migrations/util/timestamp.js b/test/unit/migrations/util/timestamp.js new file mode 100644 index 0000000000..ec035991b7 --- /dev/null +++ b/test/unit/migrations/util/timestamp.js @@ -0,0 +1,24 @@ +const sinon = require('sinon'); +const { expect } = require('chai'); +const { yyyymmddhhmmss } = require('../../../../lib/migrations/util/timestamp'); + +describe('timestamp', () => { + let clock; + beforeEach(() => { + clock = sinon.useFakeTimers({ + now: new Date('2007-03-01T13:00:00Z'), + }); + }); + + afterEach(() => { + sinon.restore(); + clock.restore(); + }); + + describe('yyyymmddhhmmss', () => { + it('Returns UTC time', () => { + const timestamp = yyyymmddhhmmss(); + expect(timestamp).to.equal('20070301130000'); + }); + }); +});