mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +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', () => {
|
||||
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: [],
|
||||
@@ -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',
|
||||
@@ -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({
|
||||
@@ -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',
|
||||
@@ -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,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -23,7 +23,7 @@ interface DurationLine {
|
||||
rawLine: string
|
||||
cleanedDuration: string
|
||||
sign: number
|
||||
durationMS: number | undefined
|
||||
durationMS: number | null
|
||||
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 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,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -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) => {
|
||||
@@ -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 }),
|
||||
|
||||
Reference in new issue
Block a user