format uptime

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
Jianhui Zhao
2018-02-11 17:48:36 +08:00
parent 159a58166b
commit 840dba2c6c
4 changed files with 179 additions and 4 deletions
+15 -3
View File
@@ -55,10 +55,22 @@ export default {
password: '',
devId: '',
devlistTitle: [
{ title: 'ID', key: 'id', sortType: 'asc', sortable: true },
{ title: 'Uptime', key: 'uptime', sortable: true },
{ title: 'Description', key: 'description' },
{
title: 'ID',
key: 'id',
sortType: 'asc',
sortable: true
}, {
title: 'Uptime',
key: 'uptime',
sortable: true,
render: (h, params) => {
return '%t'.format(params.row.uptime);
}
}, {
title: 'Description',
key: 'description'
}, {
width: 150,
align: 'center',
render: (h, params) => {
+1
View File
@@ -6,6 +6,7 @@ import iView from 'iview'
import 'iview/dist/styles/iview.css'
import locale from 'iview/dist/locale/en-US';
import VueContextMenu from '@xunlei/vue-context-menu'
import './string.js'
Vue.config.productionTip = false
+162
View File
@@ -0,0 +1,162 @@
String.prototype.format = function()
{
var html_esc = [/&/g, '&#38;', /"/g, '&#34;', /'/g, '&#39;', /</g, '&#60;', />/g, '&#62;'];
var quot_esc = [/"/g, '&#34;', /'/g, '&#39;'];
function esc(s, r) {
for( var i = 0; i < r.length; i += 2 )
s = s.replace(r[i], r[i+1]);
return s;
}
var str = this;
var out = '';
var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
var a = b = [], numSubstitutions = 0, numMatches = 0;
while ((a = re.exec(str)) != null)
{
var m = a[1];
var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
var pPrecision = a[6], pType = a[7];
numMatches++;
if (pType == '%')
{
subst = '%';
}
else
{
if (numSubstitutions < arguments.length)
{
var param = arguments[numSubstitutions++];
var pad = '';
if (pPad && pPad.substr(0,1) == "'")
pad = leftpart.substr(1,1);
else if (pPad)
pad = pPad;
var justifyRight = true;
if (pJustify && pJustify === "-")
justifyRight = false;
var minLength = -1;
if (pMinLength)
minLength = parseInt(pMinLength);
var precision = -1;
if (pPrecision && pType == 'f')
precision = parseInt(pPrecision.substring(1));
var subst = param;
switch(pType)
{
case 'b':
subst = (parseInt(param) || 0).toString(2);
break;
case 'c':
subst = String.fromCharCode(parseInt(param) || 0);
break;
case 'd':
subst = (parseInt(param) || 0);
break;
case 'u':
subst = Math.abs(parseInt(param) || 0);
break;
case 'f':
subst = (precision > -1)
? ((parseFloat(param) || 0.0)).toFixed(precision)
: (parseFloat(param) || 0.0);
break;
case 'o':
subst = (parseInt(param) || 0).toString(8);
break;
case 's':
subst = param;
break;
case 'x':
subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
break;
case 'X':
subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
break;
case 'h':
subst = esc(param, html_esc);
break;
case 'q':
subst = esc(param, quot_esc);
break;
case 'j':
subst = String.serialize(param);
break;
case 't':
var td = 0;
var th = 0;
var tm = 0;
var ts = (param || 0);
if (ts > 60) {
tm = Math.floor(ts / 60);
ts = (ts % 60);
}
if (tm > 60) {
th = Math.floor(tm / 60);
tm = (tm % 60);
}
if (th > 24) {
td = Math.floor(th / 24);
th = (th % 24);
}
subst = (td > 0)
? '%dd %dh %dm %ds'.format(td, th, tm, ts)
: '%dh %dm %ds'.format(th, tm, ts);
break;
case 'm':
var mf = pMinLength ? parseInt(pMinLength) : 1000;
var pr = pPrecision ? Math.floor(10*parseFloat('0'+pPrecision)) : 2;
var i = 0;
var val = parseFloat(param || 0);
var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ];
for (i = 0; (i < units.length) && (val > mf); i++)
val /= mf;
subst = val.toFixed(pr) + ' ' + units[i];
break;
}
subst = (typeof(subst) == 'undefined') ? '' : subst.toString();
if (minLength > 0 && pad.length > 0)
for (var i = 0; i < (minLength - subst.length); i++)
subst = justifyRight ? (pad + subst) : (subst + pad);
}
}
out += leftpart + subst;
str = str.substr(m.length);
}
return out + str;
}