Skip to content

Commit

Permalink
add tests for LONGDATETIME parsing/encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Connum committed Oct 30, 2023
1 parent 8fbc2ba commit 7a1b1ac
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Parser.prototype.parseTag = function() {
// JavaScript and unix timestamps traditionally use 32 bits, so we
// only take the last 32 bits.
// + Since until 2038 those bits will be filled by zeros we can ignore them.
// FIXME: at some point we need to support dates >2038 using the full 64bit
Parser.prototype.parseLongDateTime = function() {
let v = getULong(this.data, this.offset + this.relativeOffset + 4);
// Subtract seconds between 01/01/1904 and 01/01/1970
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ sizeOf.F2DOT14 = sizeOf.USHORT;
* @returns {Array}
*/
encode.LONGDATETIME = function(v) {
// FIXME: at some point we need to support dates >2038 using the full 64bit
return [0, 0, 0, 0, (v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF];
};

Expand Down
18 changes: 18 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,22 @@ describe('parse.js', function() {
assert.equal(p.relativeOffset, 28);
});
});

describe('parseLongDateTime', function() {
it('should parse LONGDATETIME values', function() {
// FIXME: test dates > 2038 once all 64bit are supported
const date1 = new Date('1904-01-01T00:00:00.000Z').getTime();
const date2 = new Date('1970-01-01T00:00:00.000Z').getTime();
const date3 = new Date('2038-12-31T23:59:59.000Z').getTime();
const hex1 = '00 00 00 00 00 00 00 00';
const hex2 = '00 00 00 00 7C 25 B0 80';
const hex3 = '00 00 00 00 FD EE FB 7F';
const p1 = new Parser(unhex(hex1), 0);
const p2 = new Parser(unhex(hex2), 0);
const p3 = new Parser(unhex(hex3), 0);
assert.equal(new Date(p1.parseLongDateTime() * 1000).getTime(), date1);
assert.equal(new Date(p2.parseLongDateTime() * 1000).getTime(), date2);
assert.equal(new Date(p3.parseLongDateTime() * 1000).getTime(), date3);
});
});
});
14 changes: 13 additions & 1 deletion test/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,19 @@ describe('types.js', function() {
assert.equal(sizeOf.UFWORD(0xDEED), 2);
});

// FIXME: Test LONGDATETIME when it gets implemented.
it('can handle LONGDATETIME', function() {
// FIXME: test dates > 2038 once all 64bit are supported
const date1 = Math.round(new Date('1904-01-01T00:00:00.000Z').getTime() / 1000) + 2082844800;
const date2 = Math.round(new Date('1970-01-01T00:00:00.000Z').getTime() / 1000) + 2082844800;
const date3 = Math.round(new Date('2038-12-31T23:59:59.000Z').getTime() / 1000) + 2082844800;
const hex1 = '00 00 00 00 00 00 00 00';
const hex2 = '00 00 00 00 7C 25 B0 80';
const hex3 = '00 00 00 00 FD EE FB 7F';
assert.equal(hex(encode.LONGDATETIME(date1)), hex1);
assert.equal(hex(encode.LONGDATETIME(date2)), hex2);
assert.equal(hex(encode.LONGDATETIME(date3)), hex3);
assert.equal(sizeOf.LONGDATETIME(date1), 8);
});

it('can handle TAG', function() {
assert.equal(hex(encode.TAG('Font')), '46 6F 6E 74');
Expand Down

0 comments on commit 7a1b1ac

Please sign in to comment.