Skip to content

Commit

Permalink
Fix day-of-week handling again
Browse files Browse the repository at this point in the history
Saturday isn't 0, it's 7.

Add tests to make sure this is working now and forever!
  • Loading branch information
parnic committed Apr 17, 2022
1 parent 79bff51 commit c95c9f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
6 changes: 1 addition & 5 deletions messages/SLMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ exports.SLMessage = class SLMessage extends SmartBuffer {
writeSLDateTime(date) {
this.writeInt16LE(date.getFullYear());
this.writeInt16LE(date.getMonth() + 1);
var dayOfWeek = date.getDay() + 1;
if (dayOfWeek === 7) {
dayOfWeek = 0;
}
this.writeInt16LE(dayOfWeek);
this.writeInt16LE(date.getDay() + 1);
this.writeInt16LE(date.getDate());
this.writeInt16LE(date.getHours());
this.writeInt16LE(date.getMinutes());
Expand Down
26 changes: 26 additions & 0 deletions test/slmessage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,30 @@ describe('SLMessage utilities', function() {
assert.equal(date.getSeconds(), decodedDate.getSeconds());
assert.equal(date.getMilliseconds(), decodedDate.getMilliseconds());
});

it('writes the appropriate day of week', function() {
let handler = function(inDate) {
let msg = new SLMessage();
msg.writeSLDateTime(inDate);
let decodedMsg = new SLMessage(msg.toBuffer());
decodedMsg.readUInt16LE();
decodedMsg.readUInt16LE();
return decodedMsg.readUInt16LE();
}

let dow = handler(new Date(2022, 3, 17, 10, 3, 0));
assert.equal(dow, 1);
dow = handler(new Date(2022, 3, 18, 10, 3, 0));
assert.equal(dow, 2);
dow = handler(new Date(2022, 3, 19, 10, 3, 0));
assert.equal(dow, 3);
dow = handler(new Date(2022, 3, 20, 10, 3, 0));
assert.equal(dow, 4);
dow = handler(new Date(2022, 3, 21, 10, 3, 0));
assert.equal(dow, 5);
dow = handler(new Date(2022, 3, 22, 10, 3, 0));
assert.equal(dow, 6);
dow = handler(new Date(2022, 3, 23, 10, 3, 0));
assert.equal(dow, 7);
});
});

0 comments on commit c95c9f8

Please sign in to comment.