-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a896a20
commit 066fd06
Showing
1 changed file
with
41 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |