libtime and the start of time and date handling

This commit is contained in:
Lisa Milne 2023-12-08 15:52:27 +10:00
parent 524e028ad6
commit de1dd33882
3 changed files with 99 additions and 4 deletions

View file

@ -1192,13 +1192,23 @@ Options:
return 0;
});
clite.commands.load('date',function(args,env,io) {
return 0;
});
if (window.location.protocol == 'file:')
clite.commands.load('test',function(args,env,io) {
var stdio = io.include('stdio');
var stdlib = io.include('stdlib');
stdio.printf('Hello world');
stdio.printf('uid:%d',stdlib.getuid());
stdio.printf('%#15x:%15o:%15.5f:%0$15.5f:%%:',10,12,8.123456789);
var time = io.include('time');
var t = time.time();
stdio.printf('time is: %d\n',t);
var tm = time.gmtime(t);
stdio.printf('gmtime gives: %d %d %d %d %d %d %d %d %d\n',tm.tm_sec,tm.tm_min,tm.tm_hour,tm.tm_mday,tm.tm_mon,tm.tm_year,tm.tm_wday,tm.tm_yday,tm.tm_isdst);
stdio.printf('Hello world\n');
stdio.printf('uid:%d\n',stdlib.getuid());
stdio.printf('%#15x:%15o:%15.5f:%0$15.5f:%%:\n',10,12,8.123456789);
function rcb(data) {
stdio.write(io.stdout,data);
var u = stdlib.getpwnam(data);

View file

@ -8,7 +8,7 @@ var clite = {
// a list of program/command files to load
prog:['commands.js','shell.js','vi.js'],
// a list of library files to load
libs:['libclite.js','libcurses.js','libstd.js','libstdio.js','libterm.js']
libs:['libclite.js','libcurses.js','libstd.js','libstdio.js','libterm.js','libtime.js']
},
core:{
execSafe:function(f) {

85
clite/libtime.js Normal file
View file

@ -0,0 +1,85 @@
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'],
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;
}
//
return Object.create({
asctime:function(timeptr) {
return data.days[timeptr.tm_wday]+' '+data.months[timeptr.tm_mon]+' '+timeptr.tm_mday+' '+timeptr.tm_hour+':'+timeptr.tm_min+':'+timeptr.tm_sec+' '+(1900+timeptr.tm_year)+'\n';
},
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);
},
time:function() {
var t = readTime();
return Math.floor(t/1000.0); // convert from milliseconds to seconds
}
});
});
}