summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/time-diff.service.ts
blob: 37477658ce9603d790940e3e5f0fcf31d15a07da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Injectable } from '@angular/core';

import _ from 'lodash';

@Injectable({
  providedIn: 'root'
})
export class TimeDiffService {
  calculateDuration(startDate: Date, endDate: Date): string {
    const startTime = +startDate;
    const endTime = +endDate;
    const duration = this.getDuration(Math.abs(startTime - endTime));
    if (startTime > endTime) {
      return '-' + duration;
    }
    return duration;
  }

  /**
   * Get the duration in the format '[Nd] [Nh] [Nm]', e.g. '2d 1h 15m'.
   * @param ms The time in milliseconds.
   * @return The duration. An empty string is returned if the duration is
   *   less than a minute.
   */
  private getDuration(ms: number): string {
    const date = new Date(ms);
    const h = date.getUTCHours();
    const m = date.getUTCMinutes();
    const d = Math.floor(ms / (24 * 3600 * 1000));

    const format = (n: number, s: string) => (n ? n + s : n);
    return [format(d, 'd'), format(h, 'h'), format(m, 'm')].filter((x) => x).join(' ');
  }

  calculateDate(date: Date, duration: string, reverse?: boolean): Date {
    const time = +date;
    if (_.isNaN(time)) {
      return undefined;
    }
    const diff = this.getDurationMs(duration) * (reverse ? -1 : 1);
    return new Date(time + diff);
  }

  private getDurationMs(duration: string): number {
    const d = this.getNumbersFromString(duration, 'd');
    const h = this.getNumbersFromString(duration, 'h');
    const m = this.getNumbersFromString(duration, 'm');
    return ((d * 24 + h) * 60 + m) * 60000;
  }

  private getNumbersFromString(duration: string, prefix: string): number {
    const match = duration.match(new RegExp(`[0-9 ]+${prefix}`, 'i'));
    return match ? parseInt(match[0], 10) : 0;
  }
}