From c95c9f8ce5d330c9e7cbbd96a46143f04da70425 Mon Sep 17 00:00:00 2001 From: Parnic Date: Sun, 17 Apr 2022 10:47:45 -0500 Subject: [PATCH] Fix day-of-week handling again Saturday isn't 0, it's 7. Add tests to make sure this is working now and forever! --- messages/SLMessage.js | 6 +----- test/slmessage.spec.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/messages/SLMessage.js b/messages/SLMessage.js index 4862dbc..8276e04 100644 --- a/messages/SLMessage.js +++ b/messages/SLMessage.js @@ -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()); diff --git a/test/slmessage.spec.js b/test/slmessage.spec.js index 981fdb9..88d6551 100644 --- a/test/slmessage.spec.js +++ b/test/slmessage.spec.js @@ -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); + }); });