npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rwf-projects/time-formatter

v2.1.5

Published

Converts a number representing a time interval into a nice human-readable string

Downloads

26

Readme

time-formatter

The time-formatter package converts a number representing elapsed time into a human readable string.

Examples

import { formatTime } from '@rwf-projects/time-formatter';

const formattedTime = formatTime(4.256128);
// 4 seconds and 256.128 milliseconds
import { formatTime } from '@rwf-projects/time-formatter';

const options = {
  precision: 2,
  inputUnit: 'millisecond',
  minUnit: 'microsecond',
  maxUnit: 'hour'
};
const formattedTime = formatTime(187696032.064, options);
// 52 hours, 8 minutes, 16 seconds, 32 milliseconds and 64 microseconds
import { formatTime } from '@rwf-projects/time-formatter';

const formattedTime: string = formatTime(4.256128);
// 4 seconds and 256.128 milliseconds
import { formatTime, Options, Units } from '@rwf-projects/time-formatter';

const options: Partial<Options> = { minUnit: Units.SECOND, maxUnit: Units.HOUR };
const formattedTime: string = formatTime(94592, options);
// 26 hours, 16 minutes and 32 seconds
import { formatTime, Options, Units } from '@rwf-projects/time-formatter';

const options: Options = {
  precision: 2,
  inputUnit: Units.MILLISECOND,
  minUnit: Units.MICROSECOND,
  maxUnit: Units.HOUR
};
const formattedTime: string = formatTime(187696032.064, options);
// 52 hours, 8 minutes, 16 seconds, 32 milliseconds and 64 microseconds

Features

  • Outputs a nice human-readable string representig a time interval;
  • Output units from weeks down to microsecond;
  • Input unit is configurable;
  • Minimum and maximum units are configurable;
  • Arbitrary precision in the lower unit;
  • Translatable;
  • Works with require and import;
  • Zero dependencies;
  • 100% test coverage.

Available units

The available units are week, day, hour, minute, second, millisecond and microsecond.
In TypeScript these units are available through the Units enum.

Why not bigger than week?

  • Because bigger units are not precise enough. A week is exactly 7 days. A month can have any duration between 28 and 31 days.

Why not smaller than microsecond?

  • Because the way JavaScript handles floating point numbers introduces errors at very small ranges;
  • It is also the maximum precision performance.now returns.

Installation

npm

npm install --save @rwf-projects/time-formatter

yarn

yarn add @rwf-projects/time-formatter

Usage

Start by importing.

import { formatTime } from '@rwf-projects/time-formatter';

... or requiring.

const { formatTime } = require('@rwf-projects/time-formatter');

Then

const formattedTime = formatTime(time, [options], [i18n]);
import { formatTime, Options, I18n } from '@rwf-projects/time-formatter';

const formattedTime: string = formatTime(time, [options], [i18n]);

Options

An object with the following keys:

| Option | Type | Default value in JS | Default value in TS | Description | |-----------|--------|---------------------|---------------------|-----------------------------------------------------------------| | precision | number | 3 | 3 | The amount of decimal places the smallest unit will have | | inputUnit | number | 'second | Units.SECOND | The input unit. One of the available units | | minUnit | string | 'millisecond' | Units.MILLISECOND | The smallest unit that will be used. One of the available units | | maxUnit | string | 'week' | Units.WEEK | The biggest unit that will be used. One of the available units |

I18n

The generated string does not have to be in English. You can specify any language, as long as it follows the format unit1, unit2 [...] and unitN.
To do so, pass the time formatter funtion an i18n object with the translations.

If you don't need to change the default options, then pass any falsy value.

I18n keys

Each i18n unit key has a mandatory singular key and an optional plural key.
The and key, used in [...] and unitN does not use singular or plural.

| Key in JS | Key in TS | Default singular value | Default plural value | |-------------|---------------------|------------------------|----------------------| | week | Units.WEEK | 'week' | Singular value + 's' | | day | Units.DAY | 'day' | Singular value + 's' | | hour | Units.HOUR | 'hour' | Singular value + 's' | | minute | Units.MINUTE | 'minute' | Singular value + 's' | | second | Units.SECOND | 'second' | Singular value + 's' | | millisecond | Units.MILLISECOND | 'millisecond' | Singular value + 's' | | microsecond | Units.MICROSECOND | 'microsecond' | Singular value + 's' | | and | and | 'and' | N/A |

Examples

Full translation

You can translate all the keys.

const fullI18n = {
  week: {
    singular: 'semana'
  },
  day: {
    singular: 'dia'
  },
  hour: {
    singular: 'hora'
  },
  minute: {
    singular: 'minuto'
  },
  second: {
    singular: 'segundo'
  },
  millisecond: {
    singular: 'milissegundo'
  },
  microsecond: {
    singular: 'microssegundo'
  },
  and: 'e'
};

const formattedTime = formatTime(694861.001001, null, fullI18n);
// 1 semana, 1 dia, 1 hora, 1 minuto, 1 segundo e 1.001 milissegundos
const fullI18n: I18n = {
  [Units.WEEK]: {
    singular: 'semana'
  },
  [Units.DAY]: {
    singular: 'dia'
  },
  [Units.HOUR]: {
    singular: 'hora'
  },
  [Units.MINUTE]: {
    singular: 'minuto'
  },
  [Units.SECOND]: {
    singular: 'segundo'
  },
  [Units.MILLISECOND]: {
    singular: 'milissegundo'
  },
  [Units.MICROSECOND]: {
    singular: 'microssegundo'
  },
  and: 'e'
};

const formattedTime: string = formatTime(694861.001001, null, fullI18n);
// 1 semana, 1 dia, 1 hora, 1 minuto, 1 segundo e 1.001 milissegundos

Partial translation

You can just specify shorter units, for example.

const options = { minUnit: 'microsecond' };

const partialI18n = {
  millisecond: {
    singular: 'ms',
    plural: 'ms'
  },
  microsecond: {
    singular: 'μs',
    plural: 'μs'
  }
};

const formattedTime = formatTime(694861.001001, options, partialI18n);
// 1 week, 1 day, 1 hour, 1 minute, 1 second, 1 ms and 1 μs
const options: Partial<Options> = { minUnit: 'microsecond' };

const partialI18n: Partial<I18n> = {
  [Units.MILLISECOND]: {
    singular: 'ms',
    plural: 'ms'
  },
  [Units.MICROSECOND]: {
    singular: 'μs',
    plural: 'μs'
  }
};

const formattedTime: string = formatTime(694861.001001, options, partialI18n);
// 1 week, 1 day, 1 hour, 1 minute, 1 second, 1 ms and 1 μs