consts/time.js

62 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-12-02 15:59:54 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of consts which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, December 2020
*/
/* eslint-disable sort-keys */
'use strict';
function is_leap (year) {
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
}
function base (second = 1) {
const ms = second / 1000;
const us = ms / 1000;
const ns = us / 1000;
const minute = second * 60;
const hour = minute * 60;
const day = hour * 24;
const week = day * 7;
const year = (y) => {
const days = is_leap (y) ? 366 : 365;
return days * day;
};
const month = (m, y) => {
let days = 0;
if (m === 2)
days = is_leap (y) ? 29 : 28;
else if (m < 8)
days = 30 + (m % 2);
else
days = 31 - (m % 2);
return days * day;
};
return {
ns,
us,
ms,
second,
minute,
hour,
day,
week,
month,
year
};
}
module.exports = {
base_s: base (1),
base_ms: base (1e3),
base_us: base (1e6),
base_ns: base (1e9),
base
};