1
|
|
|
// Human readable file size |
2
|
|
|
function byteToHumanFileSize(bytes, si = true) { |
3
|
|
|
var thresh = si ? 1000 : 1024; |
4
|
|
|
if (Math.abs(bytes) < thresh) { |
5
|
|
|
return bytes + ' B'; |
6
|
|
|
} |
7
|
|
|
var units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; |
8
|
|
|
var u = -1; |
9
|
|
|
do { |
10
|
|
|
bytes /= thresh; |
11
|
|
|
++u; |
12
|
|
|
} while (Math.abs(bytes) >= thresh && u < units.length - 1); |
13
|
|
|
return bytes.toFixed(1) + ' ' + units[u]; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
// Convert milliseconds to String, human readable |
17
|
|
|
function msToHumanTime(duration) { |
18
|
|
|
var milliseconds = parseInt((duration % 1000) / 100), |
19
|
|
|
seconds = parseInt((duration / 1000).toFixed(6) % 60), |
20
|
|
|
minutes = parseInt((duration / (1000 * 60)).toFixed(6) % 60), |
21
|
|
|
hours = parseInt((duration / (1000 * 60 * 60)).toFixed(6) % 24), |
22
|
|
|
days = parseInt((duration / (1000 * 60 * 60 * 24)).toFixed(6) % 7), |
23
|
|
|
weeks = parseInt((duration / (1000 * 60 * 60 * 24 * 7)).toFixed(6) % 4), |
24
|
|
|
months = parseInt((duration / (1000 * 60 * 60 * 24 * 7 * 4)).toFixed(6) % 12), |
25
|
|
|
years = parseInt((duration / (1000 * 60 * 60 * 24 * 7 * 4 * 12)).toFixed(6) % 10); |
26
|
|
|
var time = ''; |
27
|
|
|
|
28
|
|
|
(years < 1) ? true : time += months + 'Y'; |
29
|
|
|
(months < 1) ? true : time += months + 'M'; |
30
|
|
|
(weeks < 1) ? true : time += weeks + 'w'; |
31
|
|
|
(days < 1) ? true : time += days + 'd'; |
32
|
|
|
(hours < 1) ? true : time += hours + 'h'; |
33
|
|
|
(minutes < 1) ? true : time += minutes + 'm'; |
34
|
|
|
(seconds < 1) ? true : time += seconds + 's'; |
35
|
|
|
(milliseconds < 125) ? true : time += milliseconds + 'ms'; |
36
|
|
|
|
37
|
|
|
(duration >= 0) ? true : time = '-'; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
return time; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Convert milliseconds to String, hum readable |
44
|
|
|
function msToHumanTimeLegacy(milliseconds) { |
45
|
|
|
|
46
|
|
|
function numberEnding(number) { |
47
|
|
|
return (number > 1) ? 's' : ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
var temp = Math.floor(milliseconds / 1000); |
51
|
|
|
var years = Math.floor(temp / 31536000); |
52
|
|
|
if (years) { |
53
|
|
|
return years + ' year' + numberEnding(years); |
54
|
|
|
} |
55
|
|
|
//TODO: Months! Maybe weeks? |
56
|
|
|
var days = Math.floor((temp %= 31536000) / 86400); |
57
|
|
|
if (days) { |
58
|
|
|
return days + ' day' + numberEnding(days); |
59
|
|
|
} |
60
|
|
|
var hours = Math.floor((temp %= 86400) / 3600); |
61
|
|
|
if (hours) { |
62
|
|
|
return hours + ' hour' + numberEnding(hours); |
63
|
|
|
} |
64
|
|
|
var minutes = Math.floor((temp %= 3600) / 60); |
65
|
|
|
if (minutes) { |
66
|
|
|
return minutes + ' minute' + numberEnding(minutes); |
67
|
|
|
} |
68
|
|
|
var seconds = temp % 60; |
69
|
|
|
if (seconds) { |
70
|
|
|
return seconds + ' second' + numberEnding(seconds); |
71
|
|
|
} |
72
|
|
|
return 'less than a second'; //'just now' //or other string you like; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Get refined date from source |
76
|
|
|
function getDate(date) { |
77
|
|
|
if (date == null || date == '' || date == false ) return; |
|
|
|
|
78
|
|
|
return new Date(Date.parse(date)).toUTCString(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
module.exports = { |
82
|
|
|
byteToHumanFileSize: byteToHumanFileSize, |
83
|
|
|
msToHumanTime: msToHumanTime, |
84
|
|
|
msToHumanTimeLegacy: msToHumanTimeLegacy, |
85
|
|
|
getDate: getDate |
86
|
|
|
}; |
87
|
|
|
|