mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-21 05:24:06 +03:00
fix: fix handling of milliseconds
in parsing and in formatting iso duration in parsing of timestamp
This commit is contained in:
1 parent
13e929efbf
commit
ea71d16540
2 files changed
+70
-31
No files matched your search
@@ -24,6 +24,8 @@ describe('duration-calculator', () => {
|
|||||||
describe('computeDuration', () => {
|
describe('computeDuration', () => {
|
||||||
it('should compute correct sum/values', () => {
|
it('should compute correct sum/values', () => {
|
||||||
expect(computeDuration('')).to.deep.eq(zeroResult);
|
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('0s')).to.deep.eq(zeroResult);
|
||||||
expect(computeDuration('3600s')).to.deep.eq({
|
expect(computeDuration('3600s')).to.deep.eq({
|
||||||
errors: [],
|
errors: [],
|
||||||
@@ -120,7 +122,7 @@ describe('duration-calculator', () => {
|
|||||||
total: {
|
total: {
|
||||||
days: 0.11128616898148148,
|
days: 0.11128616898148148,
|
||||||
hours: 2.6708680555555557,
|
hours: 2.6708680555555557,
|
||||||
iso8601Duration: 'P0Y0M0DT2H40M15S',
|
iso8601Duration: 'P0Y0M0DT2H40M15.125S',
|
||||||
milliseconds: 9615125,
|
milliseconds: 9615125,
|
||||||
minutes: 160.25208333333333,
|
minutes: 160.25208333333333,
|
||||||
prettified: '2h 40m 15s 125ms',
|
prettified: '2h 40m 15s 125ms',
|
||||||
@@ -173,19 +175,19 @@ describe('duration-calculator', () => {
|
|||||||
expect(computeDuration('P4DT12H20M20.3S')).to.deep.eq({
|
expect(computeDuration('P4DT12H20M20.3S')).to.deep.eq({
|
||||||
errors: [],
|
errors: [],
|
||||||
total: {
|
total: {
|
||||||
days: 0.5141238425925926,
|
days: 4.514123842592593,
|
||||||
hours: 12.338972222222223,
|
hours: 108.33897222222222,
|
||||||
iso8601Duration: 'P0Y0M0DT12H20M20S',
|
iso8601Duration: 'P0Y0M4DT12H20M20.3S',
|
||||||
milliseconds: 44420300,
|
milliseconds: 390020300,
|
||||||
minutes: 740.3383333333334,
|
minutes: 6500.338333333333,
|
||||||
prettified: '12h 20m 20s 300ms',
|
prettified: '4d 12h 20m 20s 300ms',
|
||||||
prettifiedColonNotation: '12:20:20.3',
|
prettifiedColonNotation: '4:12:20:20.3',
|
||||||
prettifiedDaysColon: '12:20:20.300',
|
prettifiedDaysColon: '4d 12:20:20.300',
|
||||||
prettifiedHoursColon: '12:20:20.300',
|
prettifiedHoursColon: '108:20:20.300',
|
||||||
prettifiedVerbose: '12 hours 20 minutes 20 seconds 300 milliseconds',
|
prettifiedVerbose: '4 days 12 hours 20 minutes 20 seconds 300 milliseconds',
|
||||||
seconds: 44420.3,
|
seconds: 390020.3,
|
||||||
weeks: 0.07344626322751323,
|
weeks: 0.6448748346560846,
|
||||||
years: 0.0014085584728564182,
|
years: 0.012367462582445459,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
expect(computeDuration('25s\n+PT20H\n-10s')).to.deep.eq({
|
expect(computeDuration('25s\n+PT20H\n-10s')).to.deep.eq({
|
||||||
@@ -315,7 +317,7 @@ describe('duration-calculator', () => {
|
|||||||
total: {
|
total: {
|
||||||
days: 2.000001446759259,
|
days: 2.000001446759259,
|
||||||
hours: 48.000034722222225,
|
hours: 48.000034722222225,
|
||||||
iso8601Duration: 'P0Y0M2DT0H0M0S',
|
iso8601Duration: 'P0Y0M2DT0H0M0.125S',
|
||||||
milliseconds: 172800125,
|
milliseconds: 172800125,
|
||||||
minutes: 2880.0020833333333,
|
minutes: 2880.0020833333333,
|
||||||
prettified: '2d 125ms',
|
prettified: '2d 125ms',
|
||||||
@@ -328,6 +330,42 @@ describe('duration-calculator', () => {
|
|||||||
years: 0.005479456018518519,
|
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,
|
||||||
|
},
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -23,7 +23,7 @@ interface DurationLine {
|
|||||||
rawLine: string
|
rawLine: string
|
||||||
cleanedDuration: string
|
cleanedDuration: string
|
||||||
sign: number
|
sign: number
|
||||||
durationMS: number | undefined
|
durationMS: number | null
|
||||||
isValid: boolean
|
isValid: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,14 +33,14 @@ export function computeDuration(s: string): {
|
|||||||
} {
|
} {
|
||||||
const lines: DurationLine[] = s.split('\n').filter(l => l && !/^\s*#/.test(l)).map((l) => {
|
const lines: DurationLine[] = s.split('\n').filter(l => l && !/^\s*#/.test(l)).map((l) => {
|
||||||
const isNeg = /^\s*-/.test(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);
|
const durationMS = convertDurationMS(cleanedDuration);
|
||||||
return {
|
return {
|
||||||
rawLine: l,
|
rawLine: l,
|
||||||
cleanedDuration,
|
cleanedDuration,
|
||||||
sign: isNeg ? -1 : 1,
|
sign: isNeg ? -1 : 1,
|
||||||
durationMS,
|
durationMS,
|
||||||
isValid: typeof durationMS !== 'undefined',
|
isValid: durationMS !== null,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -60,8 +60,8 @@ export function computeDuration(s: string): {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertDurationMS(s: string): number | undefined {
|
function convertDurationMS(s: string): number | null {
|
||||||
const hoursHandled = s.replace(/\b(?:(\d+)\.)?(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?\b/g,
|
const hoursHandled = s.trim().replace(/^(?:(\d+)\.)?(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?$/g,
|
||||||
(_, d, h, m, s, ms) => {
|
(_, d, h, m, s, ms) => {
|
||||||
const timeArr: string[] = [];
|
const timeArr: string[] = [];
|
||||||
const addPart = (part: string, unit: string) => {
|
const addPart = (part: string, unit: string) => {
|
||||||
@@ -76,26 +76,27 @@ function convertDurationMS(s: string): number | undefined {
|
|||||||
addPart(h, 'h');
|
addPart(h, 'h');
|
||||||
addPart(m, 'm');
|
addPart(m, 'm');
|
||||||
addPart(s, 's');
|
addPart(s, 's');
|
||||||
addPart(ms, 'ms');
|
addPart(ms?.padEnd(3, '0'), 'ms');
|
||||||
return timeArr.join(' ');
|
return timeArr.join(' ');
|
||||||
});
|
});
|
||||||
if (!hoursHandled) {
|
if (!hoursHandled) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsedDuration = parse(hoursHandled);
|
try {
|
||||||
if (parsedDuration !== 0 && !parsedDuration) {
|
return iso8601Duration.toMilliseconds(iso8601Duration.parse(hoursHandled));
|
||||||
try {
|
}
|
||||||
parsedDuration = iso8601Duration.toMilliseconds(iso8601Duration.parse(hoursHandled));
|
catch (_) {
|
||||||
}
|
const result = parse(hoursHandled);
|
||||||
catch (_) {
|
if (typeof result === 'undefined') {
|
||||||
return undefined;
|
return null;
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return parsedDuration;
|
|
||||||
}
|
}
|
||||||
function prepareDurationResult(durationMS: any): ConvertedDuration {
|
function prepareDurationResult(durationMS: number): ConvertedDuration {
|
||||||
const dateFnsDuration = intervalToDuration({ start: 0, end: durationMS });
|
const dateFnsDuration = intervalToDuration({ start: 0, end: durationMS });
|
||||||
|
dateFnsDuration.seconds = (dateFnsDuration.seconds || 0) + (durationMS % 1000) / 1000;
|
||||||
return {
|
return {
|
||||||
prettified: prettyMilliseconds(durationMS, { formatSubMilliseconds: true }),
|
prettified: prettyMilliseconds(durationMS, { formatSubMilliseconds: true }),
|
||||||
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true, formatSubMilliseconds: true }),
|
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true, formatSubMilliseconds: true }),
|
||||||
|
|||||||
Reference in new issue
Block a user