mirror of
https://codeberg.org/TicklishHoneyBee/CLIte.git
synced 2026-03-11 09:04:37 +00:00
226 lines
6.8 KiB
JavaScript
226 lines
6.8 KiB
JavaScript
clite.libs.data = function() {
|
|
|
|
clite.libs.load('libtime','time',function(io,env) {
|
|
var data = {
|
|
days:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
|
|
months:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
|
|
days_l:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
|
|
months_l:['January','February','March','April','May','June','July','August','September','October','November','December'],
|
|
mdays:[31,28,31,30,31,30,31,31,30,31,30,31]
|
|
}
|
|
function getyDay(mon,mday,year) {
|
|
var yday = 0;
|
|
for (var i = 0; i<mon; i++) {
|
|
yday += data.mdays[i];
|
|
}
|
|
year += 1900;
|
|
// plus one for a leap year
|
|
if (yday > 59 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
|
|
yday++;
|
|
return yday+mday;
|
|
}
|
|
function readTime() {
|
|
var fd = clite.io.open(io.pid,'/dev/time',clite.io.flags.O_RDONLY|clite.io.flags.O_SYNC);
|
|
if (!fd)
|
|
return 0;
|
|
var t = clite.io.readAll(io.pid,fd);
|
|
if (!t) {
|
|
t = '0';
|
|
}else{
|
|
t = t.toString();
|
|
}
|
|
clite.io.close(io.pid,fd);
|
|
return parseInt(t,10);
|
|
}
|
|
function genTime(time,tz) {
|
|
var t = time+tz;
|
|
var tm = Object.create({
|
|
tm_sec:0, // seconds (0-60)
|
|
tm_min:0, // minutes (0-59)
|
|
tm_hour:0, // hour (0-23)
|
|
tm_mday:0, // day of month (0-31)
|
|
tm_mon:0, // month of year (0-11)
|
|
tm_year:0, // years since 1900
|
|
tm_wday:0, // day of week (0-6, sunday = 0)
|
|
tm_yday:0, // day of year (0-365)
|
|
tm_isdst:0 // 1 if daylight savings
|
|
});
|
|
|
|
var td = new Date(t*1000);
|
|
|
|
tm.tm_sec = td.getUTCSeconds();
|
|
tm.tm_min = td.getUTCMinutes();
|
|
tm.tm_hour = td.getUTCHours();
|
|
tm.tm_mday = td.getUTCDate();
|
|
tm.tm_mon = td.getUTCMonth();
|
|
tm.tm_year = td.getUTCFullYear()-1900;
|
|
tm.tm_wday = td.getUTCDay();
|
|
tm.tm_yday = getyDay(tm.tm_mon,tm.tm_mday,tm.tm_year);
|
|
|
|
return tm;
|
|
}
|
|
function getDTStr(fmt,tm) {
|
|
var parts = fmt.split('%');
|
|
var out = '';
|
|
var skip = false;
|
|
parts.forEach(function(part,i) {
|
|
if (part == '') {
|
|
if (i != 0) {
|
|
out += '%';
|
|
skip = true;
|
|
}
|
|
return;
|
|
}
|
|
if (skip || i == 0) {
|
|
out += part;
|
|
skip = false;
|
|
return;
|
|
}
|
|
var o = part.substring(0,1);
|
|
var e = part.substring(1);
|
|
switch (o) {
|
|
case 'a': //Locale's abbreviated weekday name.
|
|
out += data.days[tm.tm_wday];
|
|
break;
|
|
case 'A': //Locale's full weekday name.
|
|
out += data.days_l[tm.tm_wday];
|
|
break;
|
|
case 'h':
|
|
case 'b': //Locale's abbreviated month name.
|
|
out += data.months[tm.tm_mon];
|
|
break;
|
|
case 'B': //Locale's full month name.
|
|
out += data.months_l[tm.tm_mon];
|
|
break;
|
|
case 'c': //Locale's appropriate date and time representation.
|
|
out += getDTStr('%a %d %b %Y %H:%M:%S',tm);
|
|
break;
|
|
case 'C': //Century (a year divided by 100 and truncated to an integer) as a decimal number [00,99].
|
|
var y = tm.tm_year+1900;
|
|
out += (y/100).toFixed(0);
|
|
break;
|
|
case 'd': //Day of the month as a decimal number [01,31].
|
|
out += (tm.tm_mday).toFixed(0).padStart(2,'0');
|
|
break;
|
|
case 'D': //Date in the format mm/dd/yy.
|
|
out += getDTStr('%M/%D/%y',tm);
|
|
break;
|
|
case 'e': //Day of the month as a decimal number [1,31] in a two-digit field with leading <space> character fill.
|
|
out += (tm.tm_mday).toFixed(0).padStart(2,' ');
|
|
break;
|
|
case 'H': //Hour (24-hour clock) as a decimal number [00,23].
|
|
out += (tm.tm_hour).toFixed(0).padStart(2,'0');
|
|
break;
|
|
case 'I': //Hour (12-hour clock) as a decimal number [01,12].
|
|
out += (tm.tm_hour%12).toFixed(0).padStart(2,'0');
|
|
break;
|
|
case 'j': //Day of the year as a decimal number [001,366].
|
|
out += (tm.tm_yday).toFixed(0).padStart(3,'0');
|
|
break;
|
|
case 'm': //Month as a decimal number [01,12].
|
|
out += (tm.tm_mon+1).toFixed(0).padStart(2,'0');
|
|
break;
|
|
case 'M': //Minute as a decimal number [00,59].
|
|
out += (tm.tm_min).toFixed(0).padStart(2,'0');
|
|
break;
|
|
case 'n': //A <newline>.
|
|
out += '\n';
|
|
break;
|
|
case 'p': //Locale's equivalent of either AM or PM.
|
|
if (tm.tm_hour >11) {
|
|
out += 'PM';
|
|
}else{
|
|
out += 'AM';
|
|
}
|
|
break;
|
|
case 'r': //12-hour clock time [01,12] using the AM/PM notation; in the POSIX locale, this shall be equivalent to %I : %M : %S %p.
|
|
out += getDTStr('%I:%M:%S%p',tm);
|
|
break;
|
|
case 's':
|
|
out += time.mktime(tm).toFixed(0);
|
|
break;
|
|
case 'S': //Seconds as a decimal number [00,60].
|
|
out += tm.tm_sec.toFixed(0).padStart(2,'0');
|
|
break;
|
|
case 't': //A <tab>.
|
|
out += '\t';
|
|
break;
|
|
case 'T': //24-hour clock time [00,23] in the format HH:MM:SS.
|
|
out += getDTStr('%H:%M:%S',tm);
|
|
break;
|
|
case 'u': //Weekday as a decimal number [1,7] (1=Monday).
|
|
if (tm.tm_wday < 1) {
|
|
out += '7';
|
|
}else{
|
|
out += tm.tm_wday.toFixed(0);
|
|
}
|
|
break;
|
|
case 'U': //Week of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday shall be considered to be in week 0.
|
|
// TODO: this
|
|
break;
|
|
case 'V': //Week of the year (Monday as the first day of the week) as a decimal number [01,53]. If the week containing January 1 has four or more days in the new year, then it shall be considered week 1; otherwise, it shall be the last week of the previous year, and the next week shall be week 1.
|
|
// TODO: this
|
|
break;
|
|
case 'w': //Weekday as a decimal number [0,6] (0=Sunday).
|
|
out += tm.tm_wday.toFixed(0);
|
|
break;
|
|
case 'W': //Week of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday shall be considered to be in week 0.
|
|
// TODO: this
|
|
break;
|
|
case 'x': //Locale's appropriate date representation.
|
|
out += getDTStr('%d/%m/%Y',tm);
|
|
break;
|
|
case 'X': //Locale's appropriate time representation.
|
|
out += getDTStr('%H:%M:%S',tm);
|
|
break;
|
|
case 'y': //Year within century [00,99].
|
|
out += (tm.tm_year+1900).toFixed(0).substring(2);
|
|
break;
|
|
case 'Y': //Year with century as a decimal number.
|
|
out += (tm.tm_year+1900).toFixed(0);
|
|
break;
|
|
case 'Z': //Timezone name, or no characters if no timezone is determinable.
|
|
break;
|
|
default:;
|
|
}
|
|
out += e;
|
|
});
|
|
return out;
|
|
}
|
|
|
|
//
|
|
|
|
return Object.create({
|
|
|
|
asctime:function(timeptr) {
|
|
return getDTStr('%a %d %b %T %Y',timeptr);
|
|
},
|
|
ctime:function(time) {
|
|
var tm = this.localtime(time);
|
|
return this.asctime(tm);
|
|
},
|
|
gmtime:function(time) {
|
|
return genTime(time,0);
|
|
},
|
|
localtime:function(time) {
|
|
// TODO: fill in tz with seconds offset for current timezone
|
|
var tz = 0;
|
|
return genTime(time,tz);
|
|
},
|
|
mktime:function(timeptr) {
|
|
var t = getDTStr('%a %d %b %T %Y %Z',timeptr);
|
|
return parseInt(Math.floor((new Date(t)).getTime()/1000.0).toFixed(0));
|
|
},
|
|
strftime:function(fmt,timeptr) {
|
|
return getDTStr(fmt,timeptr);
|
|
},
|
|
time:function() {
|
|
var t = readTime();
|
|
return parseInt(Math.floor(t/1000.0).toFixed(0)); // convert from milliseconds to seconds
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|