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