mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
fix: handle (.net) timespan format and submilliseconds
format: d.hh:mm:ss.fff and prettify in submilliseconds
This commit is contained in:
1 parent
982ef5f465
commit
13e929efbf
2 files changed
+44
-22
No files matched your search
@@ -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: 'P0Y0M0DT12H20M20S',
|
||||
milliseconds: 44420300,
|
||||
minutes: 740.3383333333334,
|
||||
prettified: '12h 20m 20.3s',
|
||||
prettified: '12h 20m 20s 300ms',
|
||||
prettifiedColonNotation: '12:20:20.3',
|
||||
prettifiedDaysColon: '12:20:20.300',
|
||||
prettifiedHoursColon: '12:20:20.300',
|
||||
prettifiedVerbose: '12 hours 20 minutes 20.3 seconds',
|
||||
prettifiedVerbose: '12 hours 20 minutes 20 seconds 300 milliseconds',
|
||||
seconds: 44420.3,
|
||||
weeks: 0.07344626322751323,
|
||||
years: 0.0014085584728564182,
|
||||
@@ -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 {
|
||||
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),
|
||||
|
||||
Reference in new issue
Block a user