Skip to content

Commit

Permalink
rounding bug (#716)
Browse files Browse the repository at this point in the history
* rounding bug

* whoops
  • Loading branch information
PatrickCleary authored Jul 6, 2023
1 parent 618a18b commit ca38984
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions common/utils/time.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import dayjs from 'dayjs';
import type { DurationUnitType } from 'dayjs/plugin/duration';
import { WidgetText } from '../components/widgets/internal/WidgetText';
import { UnitText } from '../components/widgets/internal/UnitText';

Expand Down Expand Up @@ -46,28 +45,16 @@ export const getFormattedTimeValue = (value: number) => {
}
};

export const getFormattedTimeString = (value: number, unit: DurationUnitType = 'seconds') => {
if (unit === 'seconds') {
const absValue = Math.round(Math.abs(value));
const duration = dayjs.duration(absValue, unit);
switch (true) {
case absValue < 100:
return `${absValue}s`;
case absValue < 3600:
return `${duration.format('m')}m ${duration.format('s').padStart(2, '0')}s`;
default:
return `${duration.format('H')}h ${duration.format('m').padStart(2, '0')}m`;
}
} else if (unit === 'minutes') {
const absValue = Math.abs(value);
const duration = dayjs.duration(absValue, unit);
switch (true) {
case absValue < 1:
return `${duration.format('s')}s`;
case absValue < 60:
return `${duration.format('m')}m ${duration.format('s').padStart(2, '0')}s`;
default:
return `${duration.format('H')}h ${duration.format('m').padStart(2, '0')}m`;
}
export const getFormattedTimeString = (value: number, unit: 'minutes' | 'seconds' = 'seconds') => {
const secondsValue = unit === 'seconds' ? value : value * 60;
const absValue = Math.round(Math.abs(secondsValue));
const duration = dayjs.duration(absValue, 'seconds');
switch (true) {
case absValue < 100:
return `${absValue}s`;
case absValue < 3600:
return `${duration.format('m')}m ${duration.format('s').padStart(2, '0')}s`;
default:
return `${duration.format('H')}h ${duration.format('m').padStart(2, '0')}m`;
}
};

0 comments on commit ca38984

Please sign in to comment.