Skip to content

Commit

Permalink
fix datetime util structure
Browse files Browse the repository at this point in the history
  • Loading branch information
rassokhin-s committed Jun 11, 2021
1 parent a896a20 commit 066fd06
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions utils/misc/datetime.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 066fd06

Please sign in to comment.