Refactor formatTime into util class (#3073)

While looking at https://github.com/MichMich/MagicMirror/pull/3070 I
noticed that the weather and clock module do some formatTime stuff, so
why not use a common function for that?

---------

Co-authored-by: veeck <michael@veeck.de>
This commit is contained in:
Veeck
2023-04-07 23:11:54 +02:00
committed by GitHub
parent 2c7beeaaaf
commit 32192d1698
8 changed files with 129 additions and 40 deletions

View File

@@ -141,7 +141,37 @@ const getHeadersFromResponse = (expectedResponseHeaders, response) => {
return responseHeaders;
};
/**
* Format the time according to the config
*
* @param {object} config The config of the module
* @param {object} time time to format
* @returns {string} The formatted time string
*/
const formatTime = (config, time) => {
let date = moment(time);
if (config.timezone) {
date = moment.tz(config.timezone);
}
if (config.timeFormat !== 24) {
if (config.showPeriod) {
if (config.showPeriodUpper) {
return date.format("h:mm A");
} else {
return date.format("h:mm a");
}
} else {
return date.format("h:mm");
}
}
return date.format("HH:mm");
};
if (typeof module !== "undefined")
module.exports = {
performWebRequest
performWebRequest,
formatTime
};