Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
/ timeparser Public archive

Simply string time converter (en/ru). From "5d2h" to 439200000 ms

License

Notifications You must be signed in to change notification settings

eds-fw/timeparser

Repository files navigation

DEPRECATED

Use ms instead

Simply string time converter (en/ru)
From "5d2h" to 439200000 ms

API

  • parseTime (time: string): ParsedTime | null as default

    alias: timeParser

  • deparseTime (ms: number, separator?: string): string

    alias: timeDeparser

  • var limitms: number as const
  • var timeRegExp: Record<number, RegExp> as const
  • var timeRegExpNames: Record<string, RegExp> as const
  • var timeNamesMap: Record<number, string> as const
  • var limitsMap: Record<number, number> as const
  • type ParsedTime: {...}

Requirements

  • NodeJS, recommended v18 or newer

Setup

  1. Install timeparser via npm:
npm i @eds-fw/timeparser
  1. Use timeparser:
// file.js, type: CommonJS
const { parseTime } = require("@eds-fw/timeparser");
let unparsed = "1h5week";
let parsed = parseTime(unparsed);
console.log(parsed.ms); //3_027_600_000 ms
console.log(parsed.seconds); //3_027_600 sec

or...

// file.js, type: ESM
import parseTime from "@eds-fw/timeparser";
let unparsed = "1h5week";
let parsed = parseTime(unparsed);
console.log(parsed.ms); //3_027_600_000 ms
console.log(parsed.seconds); //3_027_600 sec

Benchmarks

(cjs) parseTime("1s") x 298,020 ops/sec ±3.32% (79 runs sampled)
(cjs) parseTime("1s").ms x 305,508 ops/sec ±1.69% (85 runs sampled)
(cjs) parseTime("1s").years x 318,852 ops/sec ±1.09% (88 runs sampled)
(cjs) parseTime("1s")._deparsed x 180,819 ops/sec ±0.80% (85 runs sampled)
(cjs) parseTime("1y2mth3w4d5h6s7ms") x 113,347 ops/sec ±0.68% (92 runs sampled)
(cjs) parseTime("1y2mth3w4d5h6s7ms").ms x 112,755 ops/sec ±0.94% (93 runs sampled)
(cjs) parseTime("1y2mth3w4d5h6s7ms").years x 109,757 ops/sec ±0.82% (94 runs sampled)
(cjs) parseTime("1y2mth3w4d5h6s7ms")._deparsed x 78,401 ops/sec ±2.21% (85 runs sampled)

(esm) parseTime("1s") x 288,259 ops/sec ±3.17% (82 runs sampled)
(esm) parseTime("1s").ms x 326,667 ops/sec ±1.32% (89 runs sampled)
(esm) parseTime("1s").years x 326,308 ops/sec ±0.92% (85 runs sampled)
(esm) parseTime("1s")._deparsed x 183,746 ops/sec ±0.73% (91 runs sampled)
(esm) parseTime("1y2mth3w4d5h6s7ms") x 104,508 ops/sec ±0.70% (90 runs sampled)
(esm) parseTime("1y2mth3w4d5h6s7ms").ms x 103,322 ops/sec ±0.59% (91 runs sampled)
(esm) parseTime("1y2mth3w4d5h6s7ms").years x 101,831 ops/sec ±1.13% (88 runs sampled)
(esm) parseTime("1y2mth3w4d5h6s7ms")._deparsed x 73,778 ops/sec ±2.42% (82 runs sampled)