Fix day-of-week handling again

Saturday isn't 0, it's 7.

Add tests to make sure this is working now and forever!
This commit is contained in:
2022-04-17 10:47:45 -05:00
parent 79bff51c17
commit c95c9f8ce5
2 changed files with 27 additions and 5 deletions

View File

@ -153,11 +153,7 @@ exports.SLMessage = class SLMessage extends SmartBuffer {
writeSLDateTime(date) { writeSLDateTime(date) {
this.writeInt16LE(date.getFullYear()); this.writeInt16LE(date.getFullYear());
this.writeInt16LE(date.getMonth() + 1); this.writeInt16LE(date.getMonth() + 1);
var dayOfWeek = date.getDay() + 1; this.writeInt16LE(date.getDay() + 1);
if (dayOfWeek === 7) {
dayOfWeek = 0;
}
this.writeInt16LE(dayOfWeek);
this.writeInt16LE(date.getDate()); this.writeInt16LE(date.getDate());
this.writeInt16LE(date.getHours()); this.writeInt16LE(date.getHours());
this.writeInt16LE(date.getMinutes()); this.writeInt16LE(date.getMinutes());

View File

@ -204,4 +204,30 @@ describe('SLMessage utilities', function() {
assert.equal(date.getSeconds(), decodedDate.getSeconds()); assert.equal(date.getSeconds(), decodedDate.getSeconds());
assert.equal(date.getMilliseconds(), decodedDate.getMilliseconds()); 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);
});
}); });