Skip to content

Commit

Permalink
date: fix timezone issue when parsing YYYYMMDDHHmmss format
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed May 10, 2021
1 parent f294199 commit b2c20d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ export default function (bot: mwn) {
let match = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(args[0]);
let dateParts = match.slice(1).map((e) => parseInt(e));
super(
dateParts[0],
dateParts[1] - 1, // fix month
dateParts[2],
dateParts[3],
dateParts[4],
dateParts[5],
Date.UTC(
dateParts[0],
dateParts[1] - 1, // fix month
dateParts[2],
dateParts[3],
dateParts[4],
dateParts[5],
),
);
} else {
// Attempt to remove a comma and paren-wrapped timezone, to get MediaWiki
Expand Down
7 changes: 5 additions & 2 deletions tests/date.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const { bot, expect } = require('./local_wiki');
const {bot, expect} = require('./local_wiki');

// Includes some tests copied from https://github.com/wikimedia-gadgets/twinkle/blob/master/tests/morebits.date.js

describe('date', async function () {
it('date constructor', function () {
let mwTs = new bot.date('20120304050607');
expect(mwTs.getTime()).to.equal(new Date(2012, 2, 4, 5, 6, 7).getTime());
expect(mwTs.getTime()).to.equal(
// MW timestamps are UTC
new Date(Date.UTC(2012, 2, 4, 5, 6, 7)).getTime()
);

let mwSig = new bot.date('13:14, 3 August 2017 (UTC)');
expect(mwSig.getTime()).to.equal(new Date('13:14 3 August 2017 UTC').getTime());
Expand Down

0 comments on commit b2c20d3

Please sign in to comment.