MCPcopy
hub / github.com/apache/caldera / getHumanFriendlyTime

Function getHumanFriendlyTime

static/js/shared.js:98–175  ·  view source on GitHub ↗

* Parse timestamp into human-friendly date format * Modified from original code: https://stackoverflow.com/questions/7641791/javascript-library-for-human-friendly-relative-date-formatting * @param date {string} - i.e. '2021-08-03 19:37:08' * @returns {string} (i.e.) '5 hrs ago'; *

(date)

Source from the content-addressed store, hash-verified

96 * (i.e. if older than 2 days): 'Aug 25 10:03:23'
97 */
98function getHumanFriendlyTime(date) {
99 if (!date) return '';
100 let split = date.split('-');
101
102 let hMonth = Number(split[1]) - 1;
103 let hDate = Number(split[2].split(' ')[0]);
104 let hTime = split[2].split(' ')[1].split(':');
105
106 const givenDate = Date.UTC(split[0], hMonth, hDate, hTime[0], hTime[1], hTime[2]);
107 // Make a fuzzy time
108 let delta = Math.round((Date.now() - givenDate) / 1000);
109
110 let minute = 60,
111 hour = minute * 60,
112 day = hour * 24;
113
114 let fuzzy;
115
116 if (delta < 30) {
117 fuzzy = 'just now';
118 } else if (delta < minute) {
119 fuzzy = delta + ' seconds ago';
120 } else if (delta < 2 * minute) {
121 fuzzy = 'a minute ago'
122 } else if (delta < hour) {
123 fuzzy = Math.floor(delta / minute) + ' min ago';
124 } else if (Math.floor(delta / hour) === 1) {
125 fuzzy = '1 hr ago'
126 } else if (delta < day) {
127 fuzzy = Math.floor(delta / hour) + ' hrs ago';
128 } else if (delta < day * 2) {
129 fuzzy = 'yesterday ' + (hTime.join(':'));
130 } else {
131 switch (hMonth) {
132 case 0:
133 hMonth = 'Jan';
134 break;
135 case 1:
136 hMonth = 'Feb';
137 break;
138 case 2:
139 hMonth = 'Mar';
140 break;
141 case 3:
142 hMonth = 'Apr';
143 break;
144 case 4:
145 hMonth = 'May';
146 break;
147 case 5:
148 hMonth = 'Jun';
149 break;
150 case 6:
151 hMonth = 'Jul';
152 break;
153 case 7:
154 hMonth = 'Aug';
155 break;

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected