From 066fd068a8d057cc62934f0cd3c8dee464d093e2 Mon Sep 17 00:00:00 2001 From: Stanislav Rassokhin Date: Fri, 11 Jun 2021 16:46:12 +0300 Subject: [PATCH] fix datetime util structure --- utils/misc/datetime.js | 80 ++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/utils/misc/datetime.js b/utils/misc/datetime.js index a62e7b240..acda48c5a 100644 --- a/utils/misc/datetime.js +++ b/utils/misc/datetime.js @@ -1,45 +1,47 @@ const moment = require('moment') -const datetime = { - /** - * We round the time so that all times only have 0 or 500 miliseconds - our quantum of time - * @returns {moment} quantified time - */ - quantify: function (mom) { - const miliseconds = mom.milliseconds() - - // first trident (like quadrant but for three segments? ) - if (miliseconds >= 0 && miliseconds < 250) { - mom.milliseconds(0) - } - - // second trident - if (miliseconds >= 250 && miliseconds < 750) { - mom.milliseconds(500) - } - - // third trident - if (miliseconds >= 750) { - // setting miliseconds above 999 will add the appropriate secs to the date - mom.milliseconds(1000) - mom.milliseconds(0) - } - - return mom - }, - - momentToMysqlString: function (mom) { - return mom.tz('UTC').format('YYYY-MM-DD HH:mm:ss.SSS') - }, - - gluedDateStrToMoment: function (dateStr) { - return moment(dateStr, 'YYYYMMDDTHHmmssSSSZ').utc() - }, - - gluedDateStrToISO: function (dateStr) { - return this.gluedDateStrToMoment(dateStr).toISOString() +/** + * We round the time so that all times only have 0 or 500 miliseconds - our quantum of time + * @returns {moment} quantified time + */ +function quantify (mom) { + const miliseconds = mom.milliseconds() + + // first trident (like quadrant but for three segments? ) + if (miliseconds >= 0 && miliseconds < 250) { + mom.milliseconds(0) } + // second trident + if (miliseconds >= 250 && miliseconds < 750) { + mom.milliseconds(500) + } + + // third trident + if (miliseconds >= 750) { + // setting miliseconds above 999 will add the appropriate secs to the date + mom.milliseconds(1000) + mom.milliseconds(0) + } + + return mom +} + +function momentToMysqlString (mom) { + return mom.tz('UTC').format('YYYY-MM-DD HH:mm:ss.SSS') } -module.exports = datetime +function gluedDateStrToMoment (dateStr) { + return moment(dateStr, 'YYYYMMDDTHHmmssSSSZ').utc() +} + +function gluedDateStrToISO (dateStr) { + return gluedDateStrToMoment(dateStr).toISOString() +} + +module.exports = { + quantify, + momentToMysqlString, + gluedDateStrToMoment, + gluedDateStrToISO +}