diff --git a/README.md b/README.md index 2205a6c..3a690fa 100755 --- a/README.md +++ b/README.md @@ -779,16 +779,16 @@ Contains information about the system's current time and date. Passed as an argu #### Properties +* `date` - `Date` instance representing the current system datetime (preferred, the other properties are derived from this one and provided for backward compatibility purposes) * `year` - short representing current system year * `month` - short representing current system month (where 1 is January, 2 is February, etc.) * `day` - short representing current system day of the month -* `dayOfWeek` - short representing current system day of the week (where 0 is Monday and 6 is Sunday) +* `dayOfWeek` - short representing current system day of the week (where 0 is Sunday and 6 is Sunday) * `hour` - short representing current system hour (24-hour time where 0 is midnight, 13 is 1PM, etc.) * `minute` - short representing current system minute * `second` - short representing current system second * `millisecond` - short representing current system millisecond * `adjustForDST` - bool indicating whether the system should adjust for daylight saving time or not -* `date` - `Date` instance representing the current system datetime (convenience, constructed from the above properties) ### SLSetSystemTime diff --git a/messages/SLGetSystemTime.js b/messages/SLGetSystemTime.js index a1597ea..6ad8fd4 100644 --- a/messages/SLGetSystemTime.js +++ b/messages/SLGetSystemTime.js @@ -17,18 +17,18 @@ exports.SLGetSystemTime = class SLGetSystemTime extends SLMessage { decode() { super.decode(); - this.year = this.readUInt16LE(); - this.month = this.readUInt16LE(); - this.dayOfWeek = this.readUInt16LE(); - this.day = this.readUInt16LE(); - this.hour = this.readUInt16LE(); - this.minute = this.readUInt16LE(); - this.second = this.readUInt16LE(); - this.millisecond = this.readUInt16LE(); + this.date = this.readSLDateTime(); + this.year = this.date.getFullYear(); + this.month = this.date.getMonth() + 1; // + 1 is for backward compatibility, SLTime represents months as 1-based + this.dayOfWeek = this.date.getDay(); // should probably be tweaked to adjust what days are 0-6 as SLTime and Javascript start on different days of the week + this.day = this.date.getDate(); + this.hour = this.date.getHours(); + this.minute = this.date.getMinutes(); + this.second = this.date.getSeconds(); + this.millisecond = this.date.getMilliseconds(); + var adjustForDST = this.readInt32LE(); this.adjustForDST = adjustForDST === 1; - - this.date = new Date(this.year, this.month - 1, this.day, this.hour, this.minute, this.second); } static getResponseId() { diff --git a/messages/SLMessage.js b/messages/SLMessage.js index 5fe3718..89dab34 100644 --- a/messages/SLMessage.js +++ b/messages/SLMessage.js @@ -165,6 +165,20 @@ exports.SLMessage = class SLMessage extends SmartBuffer { this.writeInt16LE(date.getMilliseconds()); } + readSLDateTime() { + let date = new Date(); + date.setFullYear(this.readInt16LE()); + date.setMonth(this.readInt16LE() - 1); + this.readInt16LE(); + date.setDate(this.readInt16LE()); + date.setHours(this.readInt16LE()); + date.setMinutes(this.readInt16LE()); + date.setSeconds(this.readInt16LE()); + date.setMilliseconds(this.readInt16LE()); + + return date; + } + static slackForAlignment(val) { return (4 - val % 4) % 4; } diff --git a/test/slmessage.spec.js b/test/slmessage.spec.js index 7fc5737..ee67215 100644 --- a/test/slmessage.spec.js +++ b/test/slmessage.spec.js @@ -189,4 +189,19 @@ describe('SLMessage utilities', function() { assert.equal(decodedMsg.readUInt16LE(), 5); assert.equal(decodedMsg.readUInt16LE(), 0); }); + + it('decodes SLTime as Date', function() { + let msg = new SLMessage(); + let date = new Date(2021, 8, 6, 22, 8, 5); + msg.writeSLDateTime(date); + let decodedMsg = new SLMessage(msg.toBuffer()); + let decodedDate = decodedMsg.readSLDateTime(); + assert.equal(date.getFullYear(), decodedDate.getFullYear()); + assert.equal(date.getMonth(), decodedDate.getMonth()); + assert.equal(date.getDate(), decodedDate.getDate()); + assert.equal(date.getHours(), decodedDate.getHours()); + assert.equal(date.getMinutes(), decodedDate.getMinutes()); + assert.equal(date.getSeconds(), decodedDate.getSeconds()); + assert.equal(date.getMilliseconds(), decodedDate.getMilliseconds()); + }); });