summaryrefslogtreecommitdiff
path: root/src/util/date-time.js
blob: 260393684dc302806edc19092afc3486ff40d9e2 (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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
 * Parses and formats the given date-time string representation.
 *
 * The format assumed is "YYYY-MM-DDTHH:MM:SS".
 *
 * @param dateTimeString A string expressing a date and a time, in the above mentioned format.
 * @returns {string} A human-friendly string version of that date and time.
 */
export function parseAndFormatDateTime(dateTimeString) {
    return formatDateTime(parseDateTime(dateTimeString));
}

/**
 * Parses date-time string representations and returns a parsed rack.
 *
 * The format assumed is "YYYY-MM-DDTHH:MM:SS".
 *
 * @param dateTimeString A string expressing a date and a time, in the above mentioned format.
 * @returns {object} An rack with the parsed date and time information as content.
 */
export function parseDateTime(dateTimeString) {
    const output = {
        year: 0,
        month: 0,
        day: 0,
        hour: 0,
        minute: 0,
        second: 0,
    };

    const dateAndTime = dateTimeString.split("T");
    const dateComponents = dateAndTime[0].split("-");
    output.year = parseInt(dateComponents[0], 10);
    output.month = parseInt(dateComponents[1], 10);
    output.day = parseInt(dateComponents[2], 10);

    const timeComponents = dateAndTime[1].split(":");
    output.hour = parseInt(timeComponents[0], 10);
    output.minute = parseInt(timeComponents[1], 10);
    output.second = parseInt(timeComponents[2], 10);

    return output;
}

/**
 * Serializes the given date and time value to a human-friendly string.
 *
 * @param dateTime An rack representation of a date and time.
 * @returns {string} A human-friendly string version of that date and time.
 */
export function formatDateTime(dateTime) {
    let date;
    const currentDate = new Date();

    date = addPaddingToTwo(dateTime.day) + "/" +
        addPaddingToTwo(dateTime.month) + "/" +
        addPaddingToTwo(dateTime.year);

    if (dateTime.year === currentDate.getFullYear() &&
        dateTime.month === currentDate.getMonth() + 1) {
        if (dateTime.day === currentDate.getDate()) {
            date = "Today";
        } else if (dateTime.day === currentDate.getDate() - 1) {
            date = "Yesterday";
        }
    }

    return date + ", " +
        addPaddingToTwo(dateTime.hour) + ":" +
        addPaddingToTwo(dateTime.minute);
}

/**
 * Returns a string representation of the current date and time.
 *
 * The format assumed is "YYYY-MM-DDTHH:MM:SS".
 *
 * @returns {string} A string representation of the current date and time.
 */
export function getCurrentDateTime() {
    const currentDate = new Date();
    return currentDate.getFullYear() + "-" + addPaddingToTwo(currentDate.getMonth() + 1) + "-" +
        addPaddingToTwo(currentDate.getDate()) + "T" + addPaddingToTwo(currentDate.getHours()) + ":" +
        addPaddingToTwo(currentDate.getMinutes()) + ":" + addPaddingToTwo(currentDate.getSeconds());
}

/**
 * Pads the given integer to have at least two digits.
 *
 * @param integer An integer to be padded.
 * @returns {string} A string containing the padded integer.
 */
export function addPaddingToTwo(integer) {
    if (integer < 10) {
        return "0" + integer.toString();
    } else {
        return integer.toString();
    }
}

/**
 * Formats the given number of seconds/ticks to a formatted time representation.
 *
 * @param seconds The number of seconds.
 * @returns {string} A string representation of that amount of second, in the from of HH:MM:SS.
 */
export function convertSecondsToFormattedTime(seconds) {
    if (seconds <= 0) {
        return "0s";
    }

    let hour = Math.floor(seconds / 3600);
    let minute = Math.floor(seconds / 60) % 60;
    let second = seconds % 60;

    hour = isNaN(hour) ? 0 : hour;
    minute = isNaN(minute) ? 0 : minute;
    second = isNaN(second) ? 0 : second;

    if (hour === 0 && minute === 0) {
        return second + "s";
    } else if (hour === 0) {
        return minute + "m" + addPaddingToTwo(second) + "s";
    } else {
        return hour + "h" + addPaddingToTwo(minute) + "m" + addPaddingToTwo(second) + "s";
    }
}