Add [time] to interpolate dates and times using strftime().
This commit is contained in:
parent
b3b0206107
commit
ddcd1a0294
1 changed files with 56 additions and 23 deletions
33
tools/tp.c
33
tools/tp.c
|
@ -24,6 +24,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <Args.h>
|
||||
#include <Memory.h>
|
||||
|
@ -47,6 +48,18 @@ static char runtime[] =
|
|||
"[def (); str; [(][str][)]]"
|
||||
"[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;
|
||||
|
@ -294,6 +307,26 @@ EvalExpr(char *expr, Array * stack)
|
|||
|
||||
res = StrDuplicate(val);
|
||||
}
|
||||
else if (StrEquals(op, "time"))
|
||||
{
|
||||
char *fmt = Eval(&argv, stack);
|
||||
|
||||
time_t currentTime = time(NULL);
|
||||
struct tm *timeInfo = localtime(¤tTime);
|
||||
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"))
|
||||
{
|
||||
char *expr = Eval(&argv, stack);
|
||||
|
|
Loading…
Reference in a new issue