likeopera-frontend/Util.js

62 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-12-02 19:27:22 +03:00
export default class Util
{
2018-12-02 19:27:22 +03:00
static WeekDays = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
static Months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
2018-12-02 19:27:22 +03:00
static formatBytes(s)
{
2018-12-02 19:27:22 +03:00
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';
}
2016-10-07 14:38:47 +03:00
2018-12-02 19:27:22 +03:00
static formatDate(dt)
2016-10-07 14:38:47 +03:00
{
2018-12-02 19:27:22 +03:00
if (!(dt instanceof Date))
dt = new Date(dt.replace(' ', 'T'));
let tod = new Date();
2018-12-02 19:27:22 +03:00
tod.setHours(0);
tod.setMinutes(0);
tod.setSeconds(0);
tod.setMilliseconds(0);
let prevweek = tod;
2018-12-02 19:27:22 +03:00
prevweek = prevweek.getTime() - (7 + (prevweek.getDay()+6)%7)*86400000;
if (dt.getTime() < prevweek)
{
let d = dt.getDate();
let m = dt.getMonth()+1;
2018-12-02 19:27:22 +03:00
return (d < 10 ? '0' : '')+d+'.'+(m < 10 ? '0' : '')+m+'.'+dt.getFullYear();
}
else if (dt.getTime() < tod.getTime())
{
2019-05-14 13:57:17 +03:00
return Util.WeekDays[dt.getDay()]+' '+dt.getDate()+' '+Util.Months[dt.getMonth()];
2018-12-02 19:27:22 +03:00
}
let h = dt.getHours();
let m = dt.getMinutes();
2018-12-02 19:27:22 +03:00
return (h < 10 ? '0' : '')+h+':'+(m < 10 ? '0' : '')+m;
2016-10-07 14:38:47 +03:00
}
2018-12-02 19:27:22 +03:00
static getGroupName(k)
2016-10-07 14:38:47 +03:00
{
2018-12-02 19:27:22 +03:00
if (k == 't')
{
return 'Today';
}
else if (k[0] == 'd')
{
2019-05-14 13:57:17 +03:00
return Util.WeekDays[k.substr(1)%7];
2018-12-02 19:27:22 +03:00
}
else if (k == 'pw')
{
return 'Last Week';
}
else if (k[0] == 'm')
{
2019-05-14 13:57:17 +03:00
return Util.Months[k.substr(1)-1];
2018-12-02 19:27:22 +03:00
}
return k;
2016-10-07 14:38:47 +03:00
}
}