Skip to content

Commit 13e929e

Browse files
committed
fix: handle (.net) timespan format and submilliseconds
format: d.hh:mm:ss.fff and prettify in submilliseconds
1 parent 982ef5f commit 13e929e

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

src/tools/duration-calculator/duration-calculator.service.test.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ describe('duration-calculator', () => {
123123
iso8601Duration: 'P0Y0M0DT2H40M15S',
124124
milliseconds: 9615125,
125125
minutes: 160.25208333333333,
126-
prettified: '2h 40m 15.1s',
126+
prettified: '2h 40m 15s 125ms',
127127
prettifiedColonNotation: '2:40:15.1',
128128
prettifiedDaysColon: '02:40:15.125',
129129
prettifiedHoursColon: '02:40:15.125',
130-
prettifiedVerbose: '2 hours 40 minutes 15.1 seconds',
130+
prettifiedVerbose: '2 hours 40 minutes 15 seconds 125 milliseconds',
131131
seconds: 9615.125,
132132
weeks: 0.01589802414021164,
133133
years: 0.00030489361364789447,
@@ -178,11 +178,11 @@ describe('duration-calculator', () => {
178178
iso8601Duration: 'P0Y0M0DT12H20M20S',
179179
milliseconds: 44420300,
180180
minutes: 740.3383333333334,
181-
prettified: '12h 20m 20.3s',
181+
prettified: '12h 20m 20s 300ms',
182182
prettifiedColonNotation: '12:20:20.3',
183183
prettifiedDaysColon: '12:20:20.300',
184184
prettifiedHoursColon: '12:20:20.300',
185-
prettifiedVerbose: '12 hours 20 minutes 20.3 seconds',
185+
prettifiedVerbose: '12 hours 20 minutes 20 seconds 300 milliseconds',
186186
seconds: 44420.3,
187187
weeks: 0.07344626322751323,
188188
years: 0.0014085584728564182,
@@ -309,5 +309,25 @@ describe('duration-calculator', () => {
309309
},
310310
});
311311
});
312+
it('support timespan format d.hh:mm:ss.fff', () => {
313+
expect(computeDuration('3.12:12:12\n-1.12:12:12\n+0:0:0.125')).to.deep.eq({
314+
errors: [],
315+
total: {
316+
days: 2.000001446759259,
317+
hours: 48.000034722222225,
318+
iso8601Duration: 'P0Y0M2DT0H0M0S',
319+
milliseconds: 172800125,
320+
minutes: 2880.0020833333333,
321+
prettified: '2d 125ms',
322+
prettifiedColonNotation: '2:00:00:00.1',
323+
prettifiedDaysColon: '2d 00:00:00.125',
324+
prettifiedHoursColon: '48:00:00.125',
325+
prettifiedVerbose: '2 days 125 milliseconds',
326+
seconds: 172800.125,
327+
weeks: 0.2857144923941799,
328+
years: 0.005479456018518519,
329+
},
330+
});
331+
});
312332
});
313333
});

src/tools/duration-calculator/duration-calculator.service.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,24 @@ export function computeDuration(s: string): {
6161
}
6262

6363
function convertDurationMS(s: string): number | undefined {
64-
const hoursHandled = s.replace(/\b(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?/g, (_, h, m, s, ms) => {
65-
const timeArr: string[] = [];
66-
const addPart = (part: string, unit: string) => {
67-
const num = Number.parseInt(part, 10);
68-
if (Number.isNaN(num)) {
69-
return;
70-
}
64+
const hoursHandled = s.replace(/\b(?:(\d+)\.)?(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?\b/g,
65+
(_, d, h, m, s, ms) => {
66+
const timeArr: string[] = [];
67+
const addPart = (part: string, unit: string) => {
68+
const num = Number.parseInt(part, 10);
69+
if (Number.isNaN(num)) {
70+
return;
71+
}
7172

72-
timeArr.push(`${num}${unit}`);
73-
};
74-
addPart(h, 'h');
75-
addPart(m, 'm');
76-
addPart(s, 's');
77-
addPart(ms, 'ms');
78-
return timeArr.join(' ');
79-
});
73+
timeArr.push(`${num}${unit}`);
74+
};
75+
addPart(d, 'd');
76+
addPart(h, 'h');
77+
addPart(m, 'm');
78+
addPart(s, 's');
79+
addPart(ms, 'ms');
80+
return timeArr.join(' ');
81+
});
8082
if (!hoursHandled) {
8183
return 0;
8284
}
@@ -95,9 +97,9 @@ function convertDurationMS(s: string): number | undefined {
9597
function prepareDurationResult(durationMS: any): ConvertedDuration {
9698
const dateFnsDuration = intervalToDuration({ start: 0, end: durationMS });
9799
return {
98-
prettified: prettyMilliseconds(durationMS),
99-
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true }),
100-
prettifiedColonNotation: prettyMilliseconds(durationMS, { colonNotation: true }),
100+
prettified: prettyMilliseconds(durationMS, { formatSubMilliseconds: true }),
101+
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true, formatSubMilliseconds: true }),
102+
prettifiedColonNotation: prettyMilliseconds(durationMS, { colonNotation: true, formatSubMilliseconds: true }),
101103
prettifiedDaysColon: hhmmss(durationMS, true),
102104
prettifiedHoursColon: hhmmss(durationMS, false),
103105
iso8601Duration: formatISODuration(dateFnsDuration),

0 commit comments

Comments
 (0)