]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/duration.pipe.ts
4675fc0f6c6d6fbe5c85af7b513c160d8353139f
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / pipes / duration.pipe.ts
1 import { Pipe, PipeTransform } from '@angular/core';
2
3 @Pipe({
4 name: 'duration',
5 pure: false
6 })
7 export class DurationPipe implements PipeTransform {
8 /**
9 * Translates seconds into human readable format of seconds, minutes, hours, days, and years
10 * source: https://stackoverflow.com/a/34270811
11 *
12 * @param {number} seconds The number of seconds to be processed
13 * @return {string} The phrase describing the the amount of time
14 */
15 transform(seconds: number): string {
16 const levels = [
17 [`${Math.floor(seconds / 31536000)}`, 'years'],
18 [`${Math.floor((seconds % 31536000) / 86400)}`, 'days'],
19 [`${Math.floor((seconds % 86400) / 3600)}`, 'hours'],
20 [`${Math.floor((seconds % 3600) / 60)}`, 'minutes'],
21 [`${Math.floor(seconds % 60)}`, 'seconds']
22 ];
23 let returntext = '';
24
25 for (let i = 0, max = levels.length; i < max; i++) {
26 if (levels[i][0] === '0') {
27 continue;
28 }
29 returntext +=
30 ' ' +
31 levels[i][0] +
32 ' ' +
33 (levels[i][0] === '1' ? levels[i][1].substr(0, levels[i][1].length - 1) : levels[i][1]);
34 }
35 return returntext.trim() || '1 second';
36 }
37 }