From 6918e5dbed0abbc752968fdc8bb8da8be28df14b Mon Sep 17 00:00:00 2001 From: Lisa Milne Date: Fri, 8 Dec 2023 23:33:07 +1000 Subject: [PATCH] date command --- clite/commands.js | 206 +++++++++++++++++++++++++++++++++++++++++++++- clite/libtime.js | 4 + 2 files changed, 209 insertions(+), 1 deletion(-) diff --git a/clite/commands.js b/clite/commands.js index c721896..0f7c984 100644 --- a/clite/commands.js +++ b/clite/commands.js @@ -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 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 . + 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 . + 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