logo
0
0
WeChat Login
fix: 更正文档地址

English | 简体中文

badge badge badge badge

Introduction

Convert time duration strings in the format of 1h2m3s4ms to a specified unit

timelong.ms1h2m3s4ms => 3723004 (ms) timelong.s1h2m3s4ms => 3723.004 (s)

Usage

import timelong from "timelong"; console.log(timelong.ms1d);
const timelong = require('timelong').default; console.log(timelong.ms1d)

The first part of the property name is the unit:

UnitMeaning
wWeek
dDay
hHour
mMinute
sSecond
msMillisecond

The following part is the specific time duration, with numbers followed by specific units: 1d4m means 1 day and 4 minutes.

For example, timelong.m1d4m will convert to the specific number of minutes: 24*60+4=1444

You can also use Chinese, but cannot mix Chinese and English.

Chinese units include:

星期 (week), 周 (week), 天 (day), 日 (day), 小时 (hour), 时 (hour), 分钟 (minute), 分 (minute), 秒 (second), 毫秒 (millisecond)

For example:

timelong.时1天

Background

Constants defining time durations are common in code, for example:

// axios default timeout export const AXIOS_TIMEOUT_DEFAULT = 5000; // ms

Simple ones are fine, but those requiring calculations need to be processed mentally, for example:

export const TASK_TIMEOUT_PERIOD = 20 * 60 * 1000; // ms

Time units might be minutes, seconds, or other non-millisecond units. Without comments, when mixed with millisecond times, they can be confusing, for example:

// redis expiration time export const REDIS_EXPIRE_TIME = 7 * 24 * 60 * 60; // s

If there's a tool that can automatically generate the desired duration in a certain format, we can save some effort and improve readability.

The examples above would become:

export const AXIOS_TIMEOUT_DEFAULT = timelong.ms5s; export const TASK_TIMEOUT_PERIOD = timelong.ms20m; export const REDIS_EXPIRE_TIME = timelong.s7d;

Examples

expect(timelong.ms1ms).toBe(1); expect(timelong.ms1s).toBe(1 * 1000); expect(timelong.ms1m).toBe(1 * 60 * 1000); expect(timelong.ms1h).toBe(1 * 60 * 60 * 1000); expect(timelong.ms1d).toBe(1 * 24 * 60 * 60 * 1000); expect(timelong.ms1w).toBe(1 * 7 * 24 * 60 * 60 * 1000); expect(timelong.ms12w34d56h78m9s10ms).toBe( 12 * timelong.ms1w + 34 * timelong.ms1d + 56 * timelong.ms1h + 78 * timelong.ms1m + 9 * timelong.ms1s + 10 );
expect(timelong.m1ms).toBeCloseTo(1 / 1000 / 60); expect(timelong.m1s).toBeCloseTo(1 / 60); expect(timelong.m1m).toBe(1); expect(timelong.m1h).toBe(1 * 60); expect(timelong.m1d).toBe(1 * 24 * 60); expect(timelong.m1w).toBe(1 * 7 * 24 * 60); expect(timelong.m12w34d56h78m9s10ms).toBeCloseTo( 12 * timelong.m1w + 34 * timelong.m1d + 56 * timelong.m1h + 78 * timelong.m1m + 9 * timelong.m1s + 10 * timelong.m1ms );
expect(timelong.时1日).toBe(1 * 24); expect(timelong.小时1日).toBe(1 * 24); expect(timelong.时1天).toBe(1 * 24); expect(timelong.小时1天).toBe(1 * 24);

And so on.

Implementation Principle

Proxy parses the property name into specific units, weeks, days, hours, minutes, seconds, and milliseconds, then accumulates and divides them accordingly.