File size: 2,309 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import memoize from "lodash.memoize";

/**
 * Given to timestamps, produces a string that describes the time between the
 * two in seconds.
 *
 * @param {number} timestamp1  The first timestamp.
 * @param {number} timestamp2  The second timestamp.
 * @returns {string}  The time between the two timestamps.
 */
export function timeDiffDescription(
    timestamp1: number,
    timestamp2: number
): string {
    const diffSecs = (timestamp2 - timestamp1) / 1000;
    const roundedSecs = Math.round(diffSecs * 10) / 10;
    const secs = roundedSecs == 1 ? "sec" : "secs";
    return ` (${roundedSecs.toFixed(1)} ${secs})`;
}

/**
 * Given a number of milliseconds since the Unix Epoch, produces a string that
 * describes the time.
 *
 * @param  {number} timestamp  The timestamp.
 * @returns {string}  A description of the time.
 */
export const formatTimestamp = memoize(function (timestamp: number): string {
    if (timestamp === 0) {
        return "";
    }
    // Format string like MM/DD HH:MM
    const date = new Date(timestamp);
    // Get month as 3-letter string
    const monthString = date.toLocaleString("default", { month: "short" });
    const day = date.getDate();
    const hour = date.getHours();
    // minutes are padded with 0s
    const minute = date.getMinutes().toString().padStart(2, "0");
    return `${monthString} ${day}, ${hour}:${minute}`;
});
/**
 * Given a number of seconds, produces a string that describes the time.
 *
 * @param {number} secs  The number of seconds.
 * @returns {string}  A description of the time.
 */
export function secsToTime(secs: number): string {
    const days = Math.floor(secs / 86400);
    const hours = Math.floor(secs / 3600);
    const minutes = Math.floor(secs / 60);
    const seconds = secs % 60;

    let timeStr = "";
    if (days == 1) {
        timeStr += "1 day, ";
    } else if (days > 1) {
        timeStr += `${days} days, `;
    }

    if (hours == 1) {
        timeStr += "1 hr, ";
    } else if (hours > 1) {
        timeStr += `${hours} hrs, `;
    }

    if (minutes == 1) {
        timeStr += "1 min, ";
    } else if (minutes > 1) {
        timeStr += `${minutes} mins, `;
    }

    if (seconds == 1) {
        timeStr += "1 sec";
    } else {
        timeStr += `${seconds.toFixed(1)} secs`;
    }

    return timeStr;
}