Skip to content

Commit

Permalink
changed fields to be UTC and duration to be more descriptive
Browse files Browse the repository at this point in the history
  • Loading branch information
rsun19 committed Jan 15, 2025
1 parent 0ce1fbb commit 5eb55a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ export const asTimestamp = (date: Date): React.ReactNode => (
</Icon>
</FlexItem>
<FlexItem>
<Timestamp date={date} dateFormat={TimestampFormat.full} timeFormat={TimestampFormat.full} />
<Timestamp
shouldDisplayUTC
date={date}
dateFormat={TimestampFormat.full}
timeFormat={TimestampFormat.full}
displaySuffix="UTC"
/>
</FlexItem>
</Flex>
);
Expand Down
10 changes: 7 additions & 3 deletions frontend/src/utilities/__tests__/time.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {

describe('relativeDuration', () => {
it('should convert milliseconds to minutes and seconds', () => {
expect(relativeDuration(123456)).toBe('2:03');
expect(relativeDuration(123456)).toBe('2 minutes, 3 seconds');
});

it('should calculate values if minutes is less than 0', () => {
expect(relativeDuration(-123456)).toBe('0:0-124');
expect(relativeDuration(-123456)).toBe('-124 seconds');
});
});

Expand Down Expand Up @@ -117,7 +117,7 @@ describe('ensureTimeFormat', () => {

describe('printSeconds', () => {
it('should print seconds', () => {
expect(printSeconds(3661)).toBe('1 second, 1 minute, 1 hour');
expect(printSeconds(3661)).toBe('1 hour, 1 minute, 1 second');
});

it('should handle a single unit', () => {
Expand All @@ -127,6 +127,10 @@ describe('printSeconds', () => {
it('should handle a single unit at max', () => {
expect(printSeconds(60)).toBe('1 minute');
});

it('should handle zero seconds', () => {
expect(printSeconds(0)).toBe('0 seconds');
});
});

describe('relativeTime', () => {
Expand Down
19 changes: 6 additions & 13 deletions frontend/src/utilities/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ const printAgo = (time: number, unit: string) => `${time} ${unit}${time > 1 ? 's
const printIn = (time: number, unit: string) => `in ${time} ${unit}${time > 1 ? 's' : ''}`;
const leadZero = (v: number) => (v < 10 ? `0${v}` : `${v}`);

export const relativeDuration = (valueInMs: number): string => {
let seconds = Math.floor(valueInMs / 1000);

let minutes = 0;
if (seconds > 60) {
minutes = Math.floor(seconds / 60);
seconds %= 60;
}

return `${minutes}:${leadZero(seconds)}`;
};
export const relativeDuration = (valueInMs: number): string =>
printSeconds(Math.floor(valueInMs / 1000));

/** As YYYY-MM-DD */
export const convertDateToSimpleDateString = (date?: Date): string | null => {
Expand Down Expand Up @@ -73,6 +64,9 @@ export const ensureTimeFormat = (time: string): string | null => {
};

export const printSeconds = (seconds: number): string => {
if (seconds === 0) {
return `${0} seconds`;
}
const timeBlocks = [
{ unit: 'second', maxPer: 60 },
{ unit: 'minute', maxPer: 60 },
Expand Down Expand Up @@ -107,11 +101,10 @@ export const printSeconds = (seconds: number): string => {
return [thisText, newUnit];
}

return [`${currentText}, ${thisText}`, newUnit];
return [`${thisText}, ${currentText}`, newUnit];
},
['', seconds],
);

return text;
};

Expand Down

0 comments on commit 5eb55a8

Please sign in to comment.