Skip to content

Commit ff833bd

Browse files
committed
Add optional parameter for *gettimestr
1 parent e1e951c commit ff833bd

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

doc/script_commands.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,20 +3531,21 @@ Examples :
35313531

35323532
---------------------------------------
35333533

3534-
*gettimestr(<format string>, <max length>)
3534+
*gettimestr(<format string>, <max length>{, <timestamp>})
35353535

35363536
This function will return a string containing time data as specified by
35373537
the format string.
35383538

3539-
This uses the C function 'strfmtime', which obeys special format
3539+
This uses the C function 'strftime', which obeys special format
35403540
characters. For a full description see, for example, the description of
3541-
'strfmtime' at http://www.delorie.com/gnu/docs/glibc/libc_437.html
3541+
'strftime' at http://www.delorie.com/gnu/docs/glibc/libc_437.html
35423542
All the format characters given in there should properly work.
35433543
Max length is the maximum length of a time string to generate.
35443544

35453545
The example given in Hercules sample scripts works like this:
35463546

35473547
mes(gettimestr("%Y-%m/%d %H:%M:%S", 21));
3548+
mes(gettimestr("%Y-%m/%d %H:%M:%S", 21, getcalendartime(0, 0)));
35483549

35493550
This will print a full date and time like 'YYYY-MM/DD HH:MM:SS'.
35503551

src/map/script.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10612,16 +10612,28 @@ static BUILDIN(gettimestr)
1061210612
char *tmpstr;
1061310613
const char *fmtstr;
1061410614
int maxlen;
10615-
time_t now = time(NULL);
10615+
time_t now;
1061610616

10617-
fmtstr=script_getstr(st,2);
10618-
maxlen=script_getnum(st,3);
10617+
fmtstr = script_getstr(st, 2);
10618+
maxlen = script_getnum(st, 3);
1061910619

10620-
tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
10621-
strftime(tmpstr,maxlen,fmtstr,localtime(&now));
10622-
tmpstr[maxlen]='\0';
10620+
if (script_hasdata(st, 4)) {
10621+
int timestamp = script_getnum(st, 4);
10622+
if (timestamp < 0) {
10623+
ShowWarning("buildin_gettimestr: UNIX timestamp must be in positive value.\n");
10624+
return false;
10625+
}
10626+
10627+
now = (time_t)timestamp;
10628+
} else {
10629+
now = time(NULL);
10630+
}
10631+
10632+
tmpstr = (char *)aMalloc((maxlen + 1)*sizeof(char));
10633+
strftime(tmpstr, maxlen, fmtstr, localtime(&now));
10634+
tmpstr[maxlen] = '\0';
1062310635

10624-
script_pushstr(st,tmpstr);
10636+
script_pushstr(st, tmpstr);
1062510637
return true;
1062610638
}
1062710639

@@ -25378,7 +25390,7 @@ static void script_parse_builtin(void)
2537825390
BUILDIN_DEF(savepoint,"sii"),
2537925391
BUILDIN_DEF(gettimetick,"i"),
2538025392
BUILDIN_DEF(gettime,"i"),
25381-
BUILDIN_DEF(gettimestr,"si"),
25393+
BUILDIN_DEF(gettimestr,"si?"),
2538225394
BUILDIN_DEF(openstorage,""),
2538325395
BUILDIN_DEF(guildopenstorage,""),
2538425396
BUILDIN_DEF(itemskill,"vi?"),

0 commit comments

Comments
 (0)