mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-07 14:57:12 -04:00
fix: handle (.net) timespan format and submilliseconds
format: d.hh:mm:ss.fff and prettify in submilliseconds
This commit is contained in:
parent
dec2e31654
commit
5116d183e1
2 changed files with 44 additions and 22 deletions
|
@ -123,11 +123,11 @@ describe('duration-calculator', () => {
|
||||||
iso8601Duration: 'P0Y0M0DT2H40M15S',
|
iso8601Duration: 'P0Y0M0DT2H40M15S',
|
||||||
milliseconds: 9615125,
|
milliseconds: 9615125,
|
||||||
minutes: 160.25208333333333,
|
minutes: 160.25208333333333,
|
||||||
prettified: '2h 40m 15.1s',
|
prettified: '2h 40m 15s 125ms',
|
||||||
prettifiedColonNotation: '2:40:15.1',
|
prettifiedColonNotation: '2:40:15.1',
|
||||||
prettifiedDaysColon: '02:40:15.125',
|
prettifiedDaysColon: '02:40:15.125',
|
||||||
prettifiedHoursColon: '02:40:15.125',
|
prettifiedHoursColon: '02:40:15.125',
|
||||||
prettifiedVerbose: '2 hours 40 minutes 15.1 seconds',
|
prettifiedVerbose: '2 hours 40 minutes 15 seconds 125 milliseconds',
|
||||||
seconds: 9615.125,
|
seconds: 9615.125,
|
||||||
weeks: 0.01589802414021164,
|
weeks: 0.01589802414021164,
|
||||||
years: 0.00030489361364789447,
|
years: 0.00030489361364789447,
|
||||||
|
@ -178,11 +178,11 @@ describe('duration-calculator', () => {
|
||||||
iso8601Duration: 'P0Y0M0DT12H20M0S',
|
iso8601Duration: 'P0Y0M0DT12H20M0S',
|
||||||
milliseconds: 44400020.3,
|
milliseconds: 44400020.3,
|
||||||
minutes: 740.0003383333333,
|
minutes: 740.0003383333333,
|
||||||
prettified: '12h 20m',
|
prettified: '12h 20m 20ms 300µs',
|
||||||
prettifiedColonNotation: '12:20:00',
|
prettifiedColonNotation: '12:20:00',
|
||||||
prettifiedDaysColon: '12:20:00.20.299999997019768',
|
prettifiedDaysColon: '12:20:00.20.299999997019768',
|
||||||
prettifiedHoursColon: '12:20:00.20.299999997019768',
|
prettifiedHoursColon: '12:20:00.20.299999997019768',
|
||||||
prettifiedVerbose: '12 hours 20 minutes',
|
prettifiedVerbose: '12 hours 20 minutes 20 milliseconds 300 microseconds',
|
||||||
seconds: 44400.0203,
|
seconds: 44400.0203,
|
||||||
weeks: 0.07341273197751322,
|
weeks: 0.07341273197751322,
|
||||||
years: 0.0014079154077879248,
|
years: 0.0014079154077879248,
|
||||||
|
@ -309,5 +309,25 @@ describe('duration-calculator', () => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it('support timespan format d.hh:mm:ss.fff', () => {
|
||||||
|
expect(computeDuration('3.12:12:12\n-1.12:12:12\n+0:0:0.125')).to.deep.eq({
|
||||||
|
errors: [],
|
||||||
|
total: {
|
||||||
|
days: 2.000001446759259,
|
||||||
|
hours: 48.000034722222225,
|
||||||
|
iso8601Duration: 'P0Y0M2DT0H0M0S',
|
||||||
|
milliseconds: 172800125,
|
||||||
|
minutes: 2880.0020833333333,
|
||||||
|
prettified: '2d 125ms',
|
||||||
|
prettifiedColonNotation: '2:00:00:00.1',
|
||||||
|
prettifiedDaysColon: '2d 00:00:00.125',
|
||||||
|
prettifiedHoursColon: '48:00:00.125',
|
||||||
|
prettifiedVerbose: '2 days 125 milliseconds',
|
||||||
|
seconds: 172800.125,
|
||||||
|
weeks: 0.2857144923941799,
|
||||||
|
years: 0.005479456018518519,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -61,22 +61,24 @@ export function computeDuration(s: string): {
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertDurationMS(s: string): number | undefined {
|
function convertDurationMS(s: string): number | undefined {
|
||||||
const hoursHandled = s.replace(/\b(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?/g, (_, h, m, s, ms) => {
|
const hoursHandled = s.replace(/\b(?:(\d+)\.)?(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?\b/g,
|
||||||
const timeArr: string[] = [];
|
(_, d, h, m, s, ms) => {
|
||||||
const addPart = (part: string, unit: string) => {
|
const timeArr: string[] = [];
|
||||||
const num = Number.parseInt(part, 10);
|
const addPart = (part: string, unit: string) => {
|
||||||
if (Number.isNaN(num)) {
|
const num = Number.parseInt(part, 10);
|
||||||
return;
|
if (Number.isNaN(num)) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
timeArr.push(`${num}${unit}`);
|
timeArr.push(`${num}${unit}`);
|
||||||
};
|
};
|
||||||
addPart(h, 'h');
|
addPart(d, 'd');
|
||||||
addPart(m, 'm');
|
addPart(h, 'h');
|
||||||
addPart(s, 's');
|
addPart(m, 'm');
|
||||||
addPart(ms, 'ms');
|
addPart(s, 's');
|
||||||
return timeArr.join(' ');
|
addPart(ms, 'ms');
|
||||||
});
|
return timeArr.join(' ');
|
||||||
|
});
|
||||||
if (!hoursHandled) {
|
if (!hoursHandled) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -95,9 +97,9 @@ function convertDurationMS(s: string): number | undefined {
|
||||||
function prepareDurationResult(durationMS: any): ConvertedDuration {
|
function prepareDurationResult(durationMS: any): ConvertedDuration {
|
||||||
const dateFnsDuration = intervalToDuration({ start: 0, end: durationMS });
|
const dateFnsDuration = intervalToDuration({ start: 0, end: durationMS });
|
||||||
return {
|
return {
|
||||||
prettified: prettyMilliseconds(durationMS),
|
prettified: prettyMilliseconds(durationMS, { formatSubMilliseconds: true }),
|
||||||
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true }),
|
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true, formatSubMilliseconds: true }),
|
||||||
prettifiedColonNotation: prettyMilliseconds(durationMS, { colonNotation: true }),
|
prettifiedColonNotation: prettyMilliseconds(durationMS, { colonNotation: true, formatSubMilliseconds: true }),
|
||||||
prettifiedDaysColon: hhmmss(durationMS, true),
|
prettifiedDaysColon: hhmmss(durationMS, true),
|
||||||
prettifiedHoursColon: hhmmss(durationMS, false),
|
prettifiedHoursColon: hhmmss(durationMS, false),
|
||||||
iso8601Duration: formatISODuration(dateFnsDuration),
|
iso8601Duration: formatISODuration(dateFnsDuration),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue