Unverified Commit 6d530184 authored by picklesrus's avatar picklesrus Committed by GitHub

Merge pull request #4938 from picklesrus/zero-minutes-fix

Change time library to default to 1 minute when the time remaining is less than 30 seconds.
parents 26d3bb73 e6f056b1
// IMPORTANT: any changes to the time algorithm also need to be made in the corresponding
// scratchr2 file 'lib/format-time.js'
/** /**
Given a timestamp in the future, calculate the largest, closest unit to show. Given a timestamp in the future, calculate the largest, closest unit to show.
On the high end we stop at hours. e.g. 15 days is still counted in hours not days or weeks. On the high end we stop at hours. e.g. 15 days is still counted in hours not days or weeks.
...@@ -18,8 +21,10 @@ const getTimeUnitAndDuration = timeStamp => { ...@@ -18,8 +21,10 @@ const getTimeUnitAndDuration = timeStamp => {
unit = 'hour'; unit = 'hour';
duration = diff / oneHourInMs; duration = diff / oneHourInMs;
} }
// Round to nearest hour or minute. // Round to nearest hour or minute, but always have at least 1
duration = Math.round(duration); // so we don't show something like "0 minutes". Hours isn't
// affected by the math.max because we choose minutes up to 2 hours.
duration = Math.max(1, Math.round(duration));
return { return {
unit: unit, unit: unit,
duration: duration duration: duration
......
...@@ -26,6 +26,13 @@ describe('unit test lib/format-time.js', () => { ...@@ -26,6 +26,13 @@ describe('unit test lib/format-time.js', () => {
expect(mockFormatExpression.format).toHaveBeenCalledWith(2, 'minute'); expect(mockFormatExpression.format).toHaveBeenCalledWith(2, 'minute');
}); });
test('test timestamp that is 15 seconds in the future displays 1', () => {
const fifteenSec = 15 * 1000;
mockFormatExpression.format.mockReturnValue('in 1 minute');
format.formatRelativeTime(fifteenSec, 'en');
expect(mockFormatExpression.format).toHaveBeenCalledWith(1, 'minute');
});
test('test rounding timestamp that is 4.4 minutes rounds to 4', () => { test('test rounding timestamp that is 4.4 minutes rounds to 4', () => {
const fourPlusMin = 4.4 * 60 * 1000; const fourPlusMin = 4.4 * 60 * 1000;
mockFormatExpression.format.mockReturnValue('in 4 minutes'); mockFormatExpression.format.mockReturnValue('in 4 minutes');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment