UNPKG

dot-beat-time

Version:

Convert legacy time to decimal internet time, or beats.

33 lines (25 loc) 843 B
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); // Math for calculating beats taken from the `beats` rust crate: https://docs.rs/beats function format(beats, long) { const beatsFixed = long ? beats.toFixed(2) : Math.floor(beats).toFixed(0); if (beats < 10) { return `@00${beatsFixed}`; } else if (beats < 100) { return `@0${beatsFixed}`; } return `@${beatsFixed}`; } function wrap(beats) { return beats >= 1000 ? Math.abs(beats - 1000) : beats; } function fromDate(date, long) { const seconds = date.getUTCSeconds() + (date.getUTCMinutes() * 60 + (date.getUTCHours() + 1) * 3600); const beats = Math.round(seconds / 86.4 * 100) / 100; return format(wrap(beats), !!long); } function now(long) { return fromDate(new Date(), long); } exports.fromDate = fromDate; exports.now = now;