fix: handle (.net) timespan format and submilliseconds

format: d.hh:mm:ss.fff
and prettify in submilliseconds
This commit is contained in:
ShareVB 2025-01-01 19:16:43 +01:00
parent dec2e31654
commit 5116d183e1
2 changed files with 44 additions and 22 deletions

View file

@ -123,11 +123,11 @@ describe('duration-calculator', () => {
iso8601Duration: 'P0Y0M0DT2H40M15S',
milliseconds: 9615125,
minutes: 160.25208333333333,
prettified: '2h 40m 15.1s',
prettified: '2h 40m 15s 125ms',
prettifiedColonNotation: '2:40:15.1',
prettifiedDaysColon: '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,
weeks: 0.01589802414021164,
years: 0.00030489361364789447,
@ -178,11 +178,11 @@ describe('duration-calculator', () => {
iso8601Duration: 'P0Y0M0DT12H20M0S',
milliseconds: 44400020.3,
minutes: 740.0003383333333,
prettified: '12h 20m',
prettified: '12h 20m 20ms 300µs',
prettifiedColonNotation: '12:20:00',
prettifiedDaysColon: '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,
weeks: 0.07341273197751322,
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,
},
});
});
});
});

View file

@ -61,22 +61,24 @@ export function computeDuration(s: string): {
}
function convertDurationMS(s: string): number | undefined {
const hoursHandled = s.replace(/\b(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?/g, (_, h, m, s, ms) => {
const timeArr: string[] = [];
const addPart = (part: string, unit: string) => {
const num = Number.parseInt(part, 10);
if (Number.isNaN(num)) {
return;
}
const hoursHandled = s.replace(/\b(?:(\d+)\.)?(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?\b/g,
(_, d, h, m, s, ms) => {
const timeArr: string[] = [];
const addPart = (part: string, unit: string) => {
const num = Number.parseInt(part, 10);
if (Number.isNaN(num)) {
return;
}
timeArr.push(`${num}${unit}`);
};
addPart(h, 'h');
addPart(m, 'm');
addPart(s, 's');
addPart(ms, 'ms');
return timeArr.join(' ');
});
timeArr.push(`${num}${unit}`);
};
addPart(d, 'd');
addPart(h, 'h');
addPart(m, 'm');
addPart(s, 's');
addPart(ms, 'ms');
return timeArr.join(' ');
});
if (!hoursHandled) {
return 0;
}
@ -95,9 +97,9 @@ function convertDurationMS(s: string): number | undefined {
function prepareDurationResult(durationMS: any): ConvertedDuration {
const dateFnsDuration = intervalToDuration({ start: 0, end: durationMS });
return {
prettified: prettyMilliseconds(durationMS),
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true }),
prettifiedColonNotation: prettyMilliseconds(durationMS, { colonNotation: true }),
prettified: prettyMilliseconds(durationMS, { formatSubMilliseconds: true }),
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true, formatSubMilliseconds: true }),
prettifiedColonNotation: prettyMilliseconds(durationMS, { colonNotation: true, formatSubMilliseconds: true }),
prettifiedDaysColon: hhmmss(durationMS, true),
prettifiedHoursColon: hhmmss(durationMS, false),
iso8601Duration: formatISODuration(dateFnsDuration),