From 94690a81ee6fd651d078f8ee213bbe7eb4a76907 Mon Sep 17 00:00:00 2001 From: Alessandro Cocco Date: Wed, 11 May 2022 23:03:10 +0200 Subject: [PATCH] Add new time_utils module and move some functions from sched module to it --- apps/sched/ChangeLog | 1 + apps/sched/lib.js | 17 ------------ apps/sched/metadata.json | 2 +- apps/sched/sched.js | 4 +-- modules/time_utils.js | 57 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 modules/time_utils.js diff --git a/apps/sched/ChangeLog b/apps/sched/ChangeLog index 914bd7002..c68653a76 100644 --- a/apps/sched/ChangeLog +++ b/apps/sched/ChangeLog @@ -7,3 +7,4 @@ 0.07: Update settings Correct `decodeTime(t)` to return a more likely expected time 0.08: add day of week check to getActiveAlarms() +0.09: Move some functions to new time_utils module diff --git a/apps/sched/lib.js b/apps/sched/lib.js index ff35c94cb..fb1a7e4d6 100644 --- a/apps/sched/lib.js +++ b/apps/sched/lib.js @@ -113,20 +113,3 @@ exports.getSettings = function () { exports.setSettings = function(settings) { require("Storage").writeJSON("sched.settings.json", settings); }; - -// time in ms -> { hrs, mins } -exports.decodeTime = function(t) { - t = Math.ceil(t / 60000); // sanitise to full minutes - let hrs = 0 | (t / 60); - return { hrs: hrs, mins: t - hrs * 60 }; -} - -// time in { hrs, mins } -> ms -exports.encodeTime = function(o) { - return o.hrs * 3600000 + o.mins * 60000; -} - -exports.formatTime = function(t) { - let o = exports.decodeTime(t); - return o.hrs + ":" + ("0" + o.mins).substr(-2); -} diff --git a/apps/sched/metadata.json b/apps/sched/metadata.json index 089fffe31..b962326e3 100644 --- a/apps/sched/metadata.json +++ b/apps/sched/metadata.json @@ -1,7 +1,7 @@ { "id": "sched", "name": "Scheduler", - "version": "0.08", + "version": "0.09", "description": "Scheduling library for alarms and timers", "icon": "app.png", "type": "scheduler", diff --git a/apps/sched/sched.js b/apps/sched/sched.js index 7c97600d9..f4d1bc9ad 100644 --- a/apps/sched/sched.js +++ b/apps/sched/sched.js @@ -9,7 +9,7 @@ function showAlarm(alarm) { const settings = require("sched").getSettings(); let msg = ""; - msg += alarm.timer ? require("sched").formatTime(alarm.timer) : require("sched").formatTime(alarm.t); + msg += require("time_utils").formatTime(alarm.timer ? alarm.timer : alarm.t); if (alarm.msg) { msg += "\n"+alarm.msg; } else { @@ -26,7 +26,7 @@ function showAlarm(alarm) { E.showPrompt(msg,{ title:alarm.timer ? /*LANG*/"TIMER!" : /*LANG*/"ALARM!", - buttons : {/*LANG*/"Snooze":true,/*LANG*/"Ok":false} // default is sleep so it'll come back in 10 mins + buttons : {/*LANG*/"Snooze":true,/*LANG*/"Stop":false} // default is sleep so it'll come back in 10 mins }).then(function(sleep) { buzzCount = 0; if (sleep) { diff --git a/modules/time_utils.js b/modules/time_utils.js new file mode 100644 index 000000000..152de2fd0 --- /dev/null +++ b/modules/time_utils.js @@ -0,0 +1,57 @@ +// module "time_utils" +// +// Utility functions useful to work with time and durations. +// Functions usually receive or return a {h, m} object or a +// number of milliseconds representing a time or a duration. +// + +/** + * @param {object} time {h, m} + * @returns the milliseconds contained in the passed time object + */ +exports.encodeTime = (time) => time.h * 3600000 + time.m * 60000; + +/** + * @param {int} millis the number of milliseconds + * @returns a time object {h, m} built from the milliseconds + */ +exports.decodeTime = (millis) => { + millis = Math.ceil(millis / 60000); + var h = 0 | (millis / 60); + return { + h: h, + m: millis - h * 60 + }; +} + +/** + * @param {object|int} value {h,m} object or milliseconds + * @returns an human-readable time string like "10:25" + */ +exports.formatTime = (value) => { + var time = (value.h === undefined || value.m === undefined) ? exports.decodeTime(value) : value; + return time.h + ":" + ("0" + time.m).substr(-2); +} + +/** + * @param {object|int} value {h,m} object or milliseconds + * @returns an human-readable duration string like "1h 10m" + */ +exports.formatDuration = (value) => { + var duration; + + var time = (value.h === undefined || value.m === undefined) ? exports.decodeTime(value) : value; + + if (time.h == 0) { + duration = time.m + "m" + } else { + duration = time.h + "h" + (time.m ? (" " + time.m + "m") : "") + } + + return duration +} + +exports.getCurrentTimeMillis = () => { + var time = new Date(); + return (time.getHours() * 3600 + time.getMinutes() * 60 + time.getSeconds()) * 1000; +}