likeopera-frontend/Util.js

62 lines
1.8 KiB
JavaScript

export default class Util
{
static WeekDays = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
static Months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
static formatBytes(s)
{
if (!s) return '';
if (s < 1024) return s+' B';
else if (s < 1024*1024) return (Math.round(s*10/1024)/10)+' KB';
else if (s < 1024*1024*1024) return (Math.round(s*10/1024/1024)/10)+' MB';
return (Math.round(s*10/1024/1024/1024)/10)+' GB';
}
static formatDate(dt)
{
if (!(dt instanceof Date))
dt = new Date(dt.replace(' ', 'T'));
let tod = new Date();
tod.setHours(0);
tod.setMinutes(0);
tod.setSeconds(0);
tod.setMilliseconds(0);
let prevweek = tod;
prevweek = prevweek.getTime() - (7 + (prevweek.getDay()+6)%7)*86400000;
if (dt.getTime() < prevweek)
{
let d = dt.getDate();
let m = dt.getMonth()+1;
return (d < 10 ? '0' : '')+d+'.'+(m < 10 ? '0' : '')+m+'.'+dt.getFullYear();
}
else if (dt.getTime() < tod.getTime())
{
return Util.WeekDays[dt.getDay()]+' '+dt.getDate()+' '+Util.Months[dt.getMonth()];
}
let h = dt.getHours();
let m = dt.getMinutes();
return (h < 10 ? '0' : '')+h+':'+(m < 10 ? '0' : '')+m;
}
static getGroupName(k)
{
if (k == 't')
{
return 'Today';
}
else if (k[0] == 'd')
{
return Util.WeekDays[k.substr(1)%7];
}
else if (k == 'pw')
{
return 'Last Week';
}
else if (k[0] == 'm')
{
return Util.Months[k.substr(1)-1];
}
return k;
}
}