Add [time] to interpolate dates and times using strftime().

This commit is contained in:
Jordan Bancino 2023-07-29 18:27:35 +00:00
parent b3b0206107
commit ddcd1a0294

View file

@ -24,6 +24,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <Args.h> #include <Args.h>
#include <Memory.h> #include <Memory.h>
@ -47,6 +48,18 @@ static char runtime[] =
"[def (); str; [(][str][)]]" "[def (); str; [(][str][)]]"
"[def #;;]" "[def #;;]"
"[def //;;]" "[def //;;]"
"[def day;; [time %d]]"
"[def month;; [time %m]]"
"[def year;; [time %Y]]"
"[def day-name;; [time %A]]"
"[def month-name;; [time %B]]"
"[def hour;; [time %I]]"
"[def minute;; [time %M]]"
"[def second;; [time %S]]"
"[def am/pm;; [time %p]]"
"[def timezone;; [time %Z]]"
"[def date;; [day-name], [month-name] [day], [year]]"
"[def timestamp;; [hour]:[minute]:[second] [am/pm] [timezone]]"
; ;
static Stream *err; static Stream *err;
@ -294,6 +307,26 @@ EvalExpr(char *expr, Array * stack)
res = StrDuplicate(val); res = StrDuplicate(val);
} }
else if (StrEquals(op, "time"))
{
char *fmt = Eval(&argv, stack);
time_t currentTime = time(NULL);
struct tm *timeInfo = localtime(&currentTime);
char *timestamp = Malloc(128 * sizeof(char));
if (strftime(timestamp, 128 * sizeof(char), fmt, timeInfo))
{
res = timestamp;
}
else
{
res = StrDuplicate("");
Free(timestamp);
}
Free(fmt);
}
else if (StrEquals(op, "eval")) else if (StrEquals(op, "eval"))
{ {
char *expr = Eval(&argv, stack); char *expr = Eval(&argv, stack);
@ -626,9 +659,9 @@ Main(Array * args)
char *rt; char *rt;
Stream *in; Stream *in;
Stream *out; Stream *out;
Array *eval = ArrayCreate(); Array *eval = ArrayCreate();
debug = 0; debug = 0;
in = StreamStdin(); in = StreamStdin();
@ -667,9 +700,9 @@ Main(Array * args)
goto finish; goto finish;
} }
break; break;
case 'e': case 'e':
ArrayAdd(eval, StrDuplicate(arg.optArg)); ArrayAdd(eval, StrDuplicate(arg.optArg));
break; break;
case 'd': case 'd':
debug = 1; debug = 1;
break; break;
@ -689,19 +722,19 @@ Main(Array * args)
rt = runtime; rt = runtime;
Free(Eval(&rt, stack)); Free(Eval(&rt, stack));
/* /*
* Evaluate any expressions specified on the command line, * Evaluate any expressions specified on the command line,
* interpolating them in the output if they produce anything * interpolating them in the output if they produce anything
* because that is the expected behavior. * because that is the expected behavior.
*/ */
for (i = 0; i < ArraySize(eval); i++) for (i = 0; i < ArraySize(eval); i++)
{ {
char *expr = ArrayGet(eval, i); char *expr = ArrayGet(eval, i);
char *res = Eval(&expr, stack); char *res = Eval(&expr, stack);
StreamPuts(out, res); StreamPuts(out, res);
Free(res); Free(res);
} }
Process(in, out, stack); Process(in, out, stack);
@ -734,11 +767,11 @@ Main(Array * args)
ret = EXIT_SUCCESS; ret = EXIT_SUCCESS;
finish: finish:
for (i = 0; i < ArraySize(eval); i++) for (i = 0; i < ArraySize(eval); i++)
{ {
Free(ArrayGet(eval, i)); Free(ArrayGet(eval, i));
} }
ArrayFree(eval); ArrayFree(eval);
if (in != StreamStdin()) if (in != StreamStdin())
{ {