Skip to content

Commit

Permalink
Timestamp UTC Standardization for Migrations (#4245)
Browse files Browse the repository at this point in the history
Co-authored-by: Igor Savin <iselwin@gmail.com>
  • Loading branch information
Tolterix and kibertoad committed Feb 15, 2021
1 parent 4899346 commit 5614c18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
16 changes: 7 additions & 9 deletions 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')
);
}

Expand Down
24 changes: 24 additions & 0 deletions 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');
});
});
});

0 comments on commit 5614c18

Please sign in to comment.