Add new time_utils module and move some functions from sched module to it

pull/1822/head
Alessandro Cocco 2022-05-11 23:03:10 +02:00
parent ec1bed62b5
commit 94690a81ee
5 changed files with 61 additions and 20 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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",

View File

@ -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) {

57
modules/time_utils.js Normal file
View File

@ -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;
}