Fix duration string
`getMilliseconds()` does not do what we thought is was doing. It does
not return the milliseconds since the epoch, but just a value between
0 and 999. Fixing this and adding tests.
Release-Notes: skip
Google-Bug-Id: b/216743635
Change-Id: I74b5ab5d412e31f1cdc759e371e86ca9f62079be
diff --git a/web/util.ts b/web/util.ts
index 1754a10..2f463aa 100644
--- a/web/util.ts
+++ b/web/util.ts
@@ -22,8 +22,8 @@
}
export function generateDurationString(startTime: Date, endTime: Date) {
- const secondsAgo = Math.round(
- (endTime.getMilliseconds() - startTime.getMilliseconds()) / 1000
+ const secondsAgo = Math.floor(
+ (endTime.valueOf() - startTime.valueOf()) / 1000
);
if (secondsAgo === 0) {
return '0 sec';
diff --git a/web/util_test.ts b/web/util_test.ts
index 466d9c6..94baa03 100644
--- a/web/util_test.ts
+++ b/web/util_test.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
import './test/test-setup';
-import {pluralize} from './util';
+import {pluralize, generateDurationString} from './util';
suite('util tests', () => {
test('pluralize', () => {
@@ -23,4 +23,28 @@
assert.equal(pluralize(1, 'bag'), '1 bag');
assert.equal(pluralize(2, 'bag'), '2 bags');
});
+
+ test('generateDurationString', () => {
+ assert.equal(
+ generateDurationString(
+ new Date('1995-12-17T03:24:00'),
+ new Date('1995-12-17T03:24:01')
+ ),
+ '1 sec'
+ );
+ assert.equal(
+ generateDurationString(
+ new Date('1995-12-17T03:24:00'),
+ new Date('1995-12-17T03:25:00')
+ ),
+ '1 min'
+ );
+ assert.equal(
+ generateDurationString(
+ new Date('1995-12-17T03:24:00'),
+ new Date('1995-12-17T04:24:00')
+ ),
+ '1 hour'
+ );
+ });
});