Skip to content

Commit

Permalink
fix: fix handling of milliseconds
Browse files Browse the repository at this point in the history
in parsing and in formatting iso duration
in parsing of timestamp
  • Loading branch information
sharevb committed Jan 1, 2025
1 parent 13e929e commit ea71d16
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 30 deletions.
68 changes: 53 additions & 15 deletions src/tools/duration-calculator/duration-calculator.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('duration-calculator', () => {
describe('computeDuration', () => {
it('should compute correct sum/values', () => {
expect(computeDuration('')).to.deep.eq(zeroResult);
expect(computeDuration('00:00:00')).to.deep.eq(zeroResult);
expect(computeDuration('0h')).to.deep.eq(zeroResult);
expect(computeDuration('0s')).to.deep.eq(zeroResult);
expect(computeDuration('3600s')).to.deep.eq({
errors: [],
Expand Down Expand Up @@ -120,7 +122,7 @@ describe('duration-calculator', () => {
total: {
days: 0.11128616898148148,
hours: 2.6708680555555557,
iso8601Duration: 'P0Y0M0DT2H40M15S',
iso8601Duration: 'P0Y0M0DT2H40M15.125S',
milliseconds: 9615125,
minutes: 160.25208333333333,
prettified: '2h 40m 15s 125ms',
Expand Down Expand Up @@ -173,19 +175,19 @@ describe('duration-calculator', () => {
expect(computeDuration('P4DT12H20M20.3S')).to.deep.eq({
errors: [],
total: {
days: 0.5141238425925926,
hours: 12.338972222222223,
iso8601Duration: 'P0Y0M0DT12H20M20S',
milliseconds: 44420300,
minutes: 740.3383333333334,
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 seconds 300 milliseconds',
seconds: 44420.3,
weeks: 0.07344626322751323,
years: 0.0014085584728564182,
days: 4.514123842592593,
hours: 108.33897222222222,
iso8601Duration: 'P0Y0M4DT12H20M20.3S',
milliseconds: 390020300,
minutes: 6500.338333333333,
prettified: '4d 12h 20m 20s 300ms',
prettifiedColonNotation: '4:12:20:20.3',
prettifiedDaysColon: '4d 12:20:20.300',
prettifiedHoursColon: '108:20:20.300',
prettifiedVerbose: '4 days 12 hours 20 minutes 20 seconds 300 milliseconds',
seconds: 390020.3,
weeks: 0.6448748346560846,
years: 0.012367462582445459,
},
});
expect(computeDuration('25s\n+PT20H\n-10s')).to.deep.eq({
Expand Down Expand Up @@ -315,7 +317,7 @@ describe('duration-calculator', () => {
total: {
days: 2.000001446759259,
hours: 48.000034722222225,
iso8601Duration: 'P0Y0M2DT0H0M0S',
iso8601Duration: 'P0Y0M2DT0H0M0.125S',
milliseconds: 172800125,
minutes: 2880.0020833333333,
prettified: '2d 125ms',
Expand All @@ -328,6 +330,42 @@ describe('duration-calculator', () => {
years: 0.005479456018518519,
},
});
expect(computeDuration('12:12:12.1')).to.deep.eq({
errors: [],
total: {
days: 0.5084733796296297,
hours: 12.20336111111111,
iso8601Duration: 'P0Y0M0DT12H12M12.1S',
milliseconds: 43932100,
minutes: 732.2016666666667,
prettified: '12h 12m 12s 100ms',
prettifiedColonNotation: '12:12:12.1',
prettifiedDaysColon: '12:12:12.100',
prettifiedHoursColon: '12:12:12.100',
prettifiedVerbose: '12 hours 12 minutes 12 seconds 100 milliseconds',
seconds: 43932.1,
weeks: 0.07263905423280423,
years: 0.0013930777524099442,
},
});
expect(computeDuration('12:12:12.12')).to.deep.eq({
errors: [],
total: {
days: 0.5084736111111111,
hours: 12.203366666666666,
iso8601Duration: 'P0Y0M0DT12H12M12.12S',
milliseconds: 43932120,
minutes: 732.202,
prettified: '12h 12m 12s 120ms',
prettifiedColonNotation: '12:12:12.1',
prettifiedDaysColon: '12:12:12.120',
prettifiedHoursColon: '12:12:12.120',
prettifiedVerbose: '12 hours 12 minutes 12 seconds 120 milliseconds',
seconds: 43932.12,
weeks: 0.0726390873015873,
years: 0.001393078386605784,
},
});
});
});
});
31 changes: 16 additions & 15 deletions src/tools/duration-calculator/duration-calculator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface DurationLine {
rawLine: string
cleanedDuration: string
sign: number
durationMS: number | undefined
durationMS: number | null
isValid: boolean
}

Expand All @@ -33,14 +33,14 @@ export function computeDuration(s: string): {
} {
const lines: DurationLine[] = s.split('\n').filter(l => l && !/^\s*#/.test(l)).map((l) => {
const isNeg = /^\s*-/.test(l);
const cleanedDuration = l.replace(/^\s*[+-]\s*/, '');
const cleanedDuration = l.replace(/^\s*[+-]\s*/, '').replace(/\s*#.*$/, ''); // NOSONAR
const durationMS = convertDurationMS(cleanedDuration);
return {
rawLine: l,
cleanedDuration,
sign: isNeg ? -1 : 1,
durationMS,
isValid: typeof durationMS !== 'undefined',
isValid: durationMS !== null,
};
});

Expand All @@ -60,8 +60,8 @@ export function computeDuration(s: string): {
};
}

function convertDurationMS(s: string): number | undefined {
const hoursHandled = s.replace(/\b(?:(\d+)\.)?(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?\b/g,
function convertDurationMS(s: string): number | null {
const hoursHandled = s.trim().replace(/^(?:(\d+)\.)?(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?$/g,
(_, d, h, m, s, ms) => {
const timeArr: string[] = [];
const addPart = (part: string, unit: string) => {
Expand All @@ -76,26 +76,27 @@ function convertDurationMS(s: string): number | undefined {
addPart(h, 'h');
addPart(m, 'm');
addPart(s, 's');
addPart(ms, 'ms');
addPart(ms?.padEnd(3, '0'), 'ms');
return timeArr.join(' ');
});
if (!hoursHandled) {
return 0;
}

let parsedDuration = parse(hoursHandled);
if (parsedDuration !== 0 && !parsedDuration) {
try {
parsedDuration = iso8601Duration.toMilliseconds(iso8601Duration.parse(hoursHandled));
}
catch (_) {
return undefined;
try {
return iso8601Duration.toMilliseconds(iso8601Duration.parse(hoursHandled));
}
catch (_) {
const result = parse(hoursHandled);
if (typeof result === 'undefined') {
return null;
}
return result;
}
return parsedDuration;
}
function prepareDurationResult(durationMS: any): ConvertedDuration {
function prepareDurationResult(durationMS: number): ConvertedDuration {
const dateFnsDuration = intervalToDuration({ start: 0, end: durationMS });
dateFnsDuration.seconds = (dateFnsDuration.seconds || 0) + (durationMS % 1000) / 1000;
return {
prettified: prettyMilliseconds(durationMS, { formatSubMilliseconds: true }),
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true, formatSubMilliseconds: true }),
Expand Down

0 comments on commit ea71d16

Please sign in to comment.