UNPKG

domotz-remote-pawn

Version:

Domotz Agent

83 lines (71 loc) 2.62 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ /** * Created by Tommaso Latini <tommaso@domotz.com> on 27/01/16. */ const SECOND = 1000; const MINUTE = 60 * SECOND; const HOUR = 60 * MINUTE; const DAY = 24 * HOUR; module.exports.SECOND = SECOND; module.exports.MINUTE = MINUTE; module.exports.HOUR = HOUR; module.exports.DAY = DAY; module.exports.getDeltaToThreeOClock = function (resourceLocator) { var domotzAgent = resourceLocator.entities.agent.get(); var moment = resourceLocator.moment(); var now = moment.tz(domotzAgent.timezone); var hours = (26 - now.hours()) % 24; var minutes = 59 - now.minutes(); var seconds = 59 - now.seconds(); var milliseconds = 1000 - now.milliseconds(); return milliseconds + 1000 * (seconds + 60 * (minutes + 60 * hours)); }; module.exports.msecs2Human = function msecs2Human(x) { var now = new Date(); var time = x ? new Date(x) : now; return ("0" + time.getHours()).slice(-2) + ':' + ("0" + time.getMinutes()).slice(-2) + ':' + ("0" + time.getSeconds()).slice(-2) + '.' + ("00" + time.getMilliseconds()).slice(-3); }; module.exports.getNextStartTime = function (start, freq) { var now = new Date(), h, m, s, u; var times = DAY / freq; h = Math.floor(start / HOUR); start -= h * HOUR; m = Math.floor(start / MINUTE); start -= m * MINUTE; s = Math.floor(start / SECOND); u = start - s*SECOND; start = (new Date()).setUTCHours(h, m, s, u); for (var i=0; i< times; i++) { var next = new Date(start + i*freq); if (next > now) { return (next - now); } } }; module.exports.toMs = function(hours, minutes, seconds) { "use strict"; var m = (hours || 0) * 60 + (minutes || 0); var s = m * 60 + (seconds || 0); return s * 1000; }; module.exports.getDiff = function (stats) { return stats.end.getTime() - stats.start.getTime(); };