Skip to content

Commit

Permalink
adding moment duration to get the units as seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Jul 8, 2020
1 parent 68da249 commit ce8ec35
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import moment, { unitOfTime as UnitOfTIme } from 'moment';

function getDurationAsSeconds(value: number, unitOfTime: UnitOfTIme.Base) {
return moment.duration(value, unitOfTime).asSeconds();
}

const units = {
ms: 0.001,
s: 1,
m: 60,
h: 3600,
d: 86400,
w: 86400 * 7, // Hum... might be wrong
M: 86400 * 30, // this too... 29,30,31?
y: 86400 * 356, // Leap year?
ms: getDurationAsSeconds(1, 'millisecond'),
s: getDurationAsSeconds(1, 'second'),
m: getDurationAsSeconds(1, 'minute'),
h: getDurationAsSeconds(1, 'hour'),
d: getDurationAsSeconds(1, 'day'),
w: getDurationAsSeconds(1, 'week'),
M: getDurationAsSeconds(1, 'month'),
y: getDurationAsSeconds(1, 'year'),
};

export function unitToSeconds(unit: string) {
Expand Down

0 comments on commit ce8ec35

Please sign in to comment.