Skip to content

Commit cd29fad

Browse files
committed
Add new built-in constant __timestamp
1 parent 1018c36 commit cd29fad

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

source/compiler/sc1.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ static int sc_reparse = 0; /* needs 3th parse because of changed prototypes
166166
static int sc_parsenum = 0; /* number of the extra parses */
167167
static int wq[wqTABSZ]; /* "while queue", internal stack for nested loops */
168168
static int *wqptr; /* pointer to next entry */
169+
static time_t now; /* current timestamp, for built-in constants "__time" and "__timestamp" */
169170
#if !defined SC_LIGHT
170171
static char sc_rootpath[_MAX_PATH];
171172
static char *sc_documentation=NULL;/* main documentation */
@@ -1554,6 +1555,8 @@ static void usage(void)
15541555
static void setconstants(void)
15551556
{
15561557
int debug;
1558+
time_t loctime;
1559+
struct tm loctm,utctm;
15571560

15581561
assert(sc_status==statIDLE);
15591562
append_constval(&tagname_tab,"_",0,0);/* "untagged" */
@@ -1601,6 +1604,13 @@ static void setconstants(void)
16011604
line_sym=add_builtin_constant("__line",0,sGLOBAL,0);
16021605
add_builtin_constant("__compat",pc_compat,sGLOBAL,0);
16031606

1607+
now=time(NULL);
1608+
loctm=*localtime(&now);
1609+
utctm=*gmtime(&now);
1610+
loctime=now+(loctm.tm_sec-utctm.tm_sec)+(loctm.tm_min-utctm.tm_min)*60
1611+
+(loctm.tm_hour-utctm.tm_hour)*60*60+(loctm.tm_mday-utctm.tm_mday)*60*60*24;
1612+
add_builtin_constant("__timestamp",(cell)loctime,sGLOBAL,0);
1613+
16041614
debug=0;
16051615
if ((sc_debug & (sCHKBOUNDS | sSYMBOLIC))==(sCHKBOUNDS | sSYMBOLIC))
16061616
debug=2;
@@ -1613,14 +1623,12 @@ static void setconstants(void)
16131623

16141624
static void setstringconstants()
16151625
{
1616-
time_t now;
16171626
char timebuf[arraysize("11:22:33")];
16181627
char datebuf[arraysize("10 Jan 2017")];
16191628

16201629
assert(sc_status!=statIDLE);
16211630
add_builtin_string_constant("__file","",sGLOBAL);
16221631

1623-
now = time(NULL);
16241632
strftime(timebuf,arraysize(timebuf),"%H:%M:%S",localtime(&now));
16251633
add_builtin_string_constant("__time",timebuf,sGLOBAL);
16261634
strftime(datebuf,arraysize(datebuf),"%d %b %Y",localtime(&now));

0 commit comments

Comments
 (0)