UNPKG

@technobuddha/library

Version:
210 lines (209 loc) 6.35 kB
/* eslint-disable @typescript-eslint/unified-signatures */ import isString from 'lodash/isString'; import { ticksPerDay, ticksPerHour, ticksPerMinute, ticksPerSecond, hoursPerDay, minutesPerHour, secondsPerMinute } from '../constants'; /** * Store and manipulate a duration of time */ export class TimeSpan { constructor(...args) { let sign = 1; let d = 0; let h = 0; let m = 0; let s = 0; let ms = 0; switch (args.length) { case 0: { d = h = m = s = ms = 0; break; } case 1: { if (isString(args[0])) { let text = args[0]; if (text.startsWith('-')) { sign = -1; text = text.slice(1); } const matches = /^(\d{1,2})(?::(\d\d)(?::(\d\d)(?::(\d\d))?)?)?(?:\.(\d{1,3}))?$/u.exec(text); if (matches) { d = Number(matches[1]); h = Number(matches[2]); m = Number(matches[3]); s = Number(matches[4]); ms = matches[5] ? Math.floor(Number(`0.${matches[5]}`) * 1000) : Number.NaN; while (Number.isNaN(s)) { s = m; m = h; h = d; d = 0; } } else { d = h = m = s = ms = 0; } } else { ms = args[0]; d = h = m = s = 0; } break; } case 3: { d = 0; h = args[0]; m = args[1]; s = args[2]; ms = 0; break; } default: { d = args[0]; h = args[1]; m = args[2]; s = args[3]; ms = args[4]; } } this.clicks = sign * ((d ? d * ticksPerDay : 0) + (h ? h * ticksPerHour : 0) + (m ? m * ticksPerMinute : 0) + (s ? s * ticksPerSecond : 0) + (ms ? ms : 0)); } clicks; /** * Get the days portion */ get days() { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks) / ticksPerDay); } /** * Get the hours portion */ get hours() { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks) / ticksPerHour) % hoursPerDay; } /** * Get the minutes portion */ get minutes() { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks) / ticksPerMinute) % minutesPerHour; } /** * Get the seconds portion */ get seconds() { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks) / ticksPerSecond) % secondsPerMinute; } /** * Get the milliseconds portion */ get milliseconds() { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks)) % ticksPerSecond; } /** * Get the total number of ticks (milliseconds) */ get ticks() { return this.clicks; } /** * Get the total number of days */ get totalDays() { return this.clicks / ticksPerDay; } /** * Get the total number of hours */ get totalHours() { return this.clicks / ticksPerHour; } /** * Get the total number of minutes */ get totalMinutes() { return this.clicks / ticksPerMinute; } /** * Get the total number of seconds */ get totalSeconds() { return this.clicks / ticksPerSecond; } /** * Get the total number of milliseconds */ get totalMilliseconds() { return this.clicks; } /** * Format the timespan using a mask * * @param mask The mask * @returns the formatted TimeSpan */ format(mask) { if (mask) { const D = this.days; const S = this.seconds; const M = this.minutes; const H = this.hours; const F = this.milliseconds; const flags = { d: D.toString(), dd: D.toString().padStart(2, '0'), m: M.toString(), mm: M.toString().padStart(2, '0'), h: H.toString(), hh: H.toString().padStart(2, '0'), s: S.toString(), ss: S.toString().padStart(2, '0'), f: F.toString().padStart(3, '0'), ff: F.toString().padStart(3, '0'), }; return mask.replace(/[dmhsf]{1,2}|"[^"]*"|'[^']*'/ug, $0 => { return ($0 in flags) ? flags[$0] : $0.slice(1, -1); }); } const D = this.days; const H = this.hours; const M = this.minutes; const S = this.seconds; const F = this.milliseconds; let str; if (D !== 0) str = `${D}d${H.toString().padStart(2, '0')}:${M.toString().padStart(2, '0')}:${S.toString().padStart(2, '0')}`; else if (H !== 0) str = `${H}:${M.toString().padStart(2, '0')}:${S.toString().padStart(2, '0')}`; else str = `${M}:${S.toString().padStart(2, '0')}`; if (F) str += `.${F.toString().padStart(3, '0')}`; return str; } /** * Convert the TimeSpan to a string * * @returns formatted string */ toString() { return this.format(); } /** * Add two timespans * * @param other TimeSpan to add to this * @returns a TimeSpan that is the sum of two timespans */ add(other) { return new TimeSpan(this.ticks + other.ticks); } /** * Compare two TimeSpans * * @param t1 First TimeSpan * @param t2 Second TimeSpan * @returns -1 if the first time span is less then the second, 0 if they are equal, 1 if the first is greater */ static compare(t1, t2) { return t1.ticks === t2.ticks ? 0 : Math.abs(t1.ticks) > Math.abs(t2.ticks) ? 1 : -1; } } export default TimeSpan;