date command

This commit is contained in:
Lisa Milne 2023-12-08 23:33:07 +10:00
parent 8eb92af201
commit 6918e5dbed
2 changed files with 209 additions and 1 deletions

View file

@ -1193,8 +1193,212 @@ Options:
});
clite.commands.load('date',function(args,env,io) {
var stdio = io.include('stdio');
var time = io.include('time');
return 0;
function help() {
stdio.printf(`
date - print system time and date
Usage: date [OPTION] [FORMAT]
Options:
-u use UTC instead of local time
-? Print this help information
`);
}
//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
function getDT(fmt,tm) {
var parts = fmt.split('%');
var out = '';
var skip = false;
var data = {
d_s:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
m_s:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
d_l:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
m_l:['January','February','March','April','May','June','July','August','September','October','November','December'],
}
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.d_s[tm.tm_wday];
break;
case 'A': //Locale's full weekday name.
out += data.d_l[tm.tm_wday];
break;
case 'h':
case 'b': //Locale's abbreviated month name.
out += data.m_s[tm.tm_mon];
break;
case 'B': //Locale's full month name.
out += data.m_l[tm.tm_mon];
break;
case 'c': //Locale's appropriate date and time representation.
out += getDT('%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+1).toFixed(0).padStart(2,'0');
break;
case 'D': //Date in the format mm/dd/yy.
out += getDT('%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+1).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 += getDT('%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 += getDT('%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 += getDT('%d/%m/%Y',tm);
break;
case 'X': //Locale's appropriate time representation.
out += getDT('%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;
}
function main(args) {
var utc = false;
var format = '%a %d %b %Y %T %Z';
for (var i=1; i<args.length; i++) {
if (args[i][0] == '-') {
for (var j=1; j<args[i].length; j++) {
switch (args[i][j]) {
case '?':
help();
return 0;
break;
case 'u':
utc = true;
break;
default:
stdio.write(io.stderr,'unknown argument: -'+args[i][j]);
}
}
}else if (args[i][0] == '+') {
format = args[i].substring(1);
}else{
// TODO: set time
stdio.write(io.stderr,'unknown argument: '+args[i]);
}
}
var t = time.time();
var tm = null;
if (utc) {
tm = time.gmtime(t);
}else{
tm = time.localtime(t);
}
if (!t)
return 1;
var out = getDT(format,tm);
if (!out)
return 1;
stdio.printf('%s\n',out);
return 0;
}
return main(args);
});
if (window.location.protocol == 'file:')

View file

@ -73,6 +73,10 @@ return Object.create({
var tz = 0;
return genTime(time,tz);
},
mktime:function(timeptr) {
var t = 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);
return (new Date(t)).getTime();
},
time:function() {
var t = readTime();
return Math.floor(t/1000.0); // convert from milliseconds to seconds