UNPKG

dot-beat-time

Version:

Convert legacy time to decimal internet time, or beats.

24 lines (23 loc) 767 B
// 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; } export 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); } export function now(long) { return fromDate(new Date(), long); }